【问题标题】:How to not save original image using Fog and Carrierwave如何不使用 Fog 和 Carrierwave 保存原始图像
【发布时间】:2013-06-20 06:22:27
【问题描述】:

我整天都在寻找如何不将原始文件保存在 Amazon S3 的服务器上。每次,我的代码都会生成相同图像的 3 个版本。 no_version.png、thumb.png 和 original.png。

我查看过 StackOverflow 并尝试使用此代码:

  after :store, :unlink_original

  def unlink_original(file)
    File.delete if version_name.blank?
  end

但是,它仍然产生 3 个版本。

我也尝试过使用路径的变体:

  after :store, :unlink_original

  def unlink_original(file)
    File.delete path if version_name.blank?
  end

但是,这会产生错误:No such file or directory - uploads/L4l8n.jpg

这是我的 ImageUploader。非常感谢任何帮助。

# encoding: utf-8

class ImageUploader < CarrierWave::Uploader::Base
  # include CarrierWaveDirect::Uploader

  # Include RMagick or MiniMagick support:
  include CarrierWave::RMagick
  # include CarrierWave::MiniMagick

  # Include the Sprockets helpers for Rails 3.1+ asset pipeline compatibility:
  include Sprockets::Helpers::RailsHelper
  include Sprockets::Helpers::IsolatedHelper

  # Choose what kind of storage to use for this uploader:
  # storage :file
  storage :fog

  include CarrierWave::MimeTypes
  process :set_content_type

  after :store, :unlink_original

  def unlink_original(file)
    File.delete if version_name.blank?
  end

  # Override the directory where uploaded files will be stored.
  # This is a sensible default for uploaders that are meant to be mounted:
  # def store_dir
  #   "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
  # end

  # Provide a default URL as a default if there hasn't been a file uploaded:
  def default_url
    # For Rails 3.1+ asset pipeline compatibility:
    asset_path("fallback/" + [version_name, "default_link.png"].compact.join('_'))

    # "/images/fallback/" + [version_name, "default.png"].compact.join('_')
  end

  # Process files as they are uploaded:
  # process :scale => [200, 300]
  #
  # def scale(width, height)
  #   # do something
  # end

  # Create different versions of your uploaded files:
  version :original, :if => :is_file_upload?
  # process :resize_to_limit => [200, 200], :if => :is_not_file_upload?

  # Default version always saves thumbs
  version :thumb do 
    process :resize_to_limit => [200, 200]
    process :convert => 'png'
  end

  # Add a white list of extensions which are allowed to be uploaded.
  # For images you might use something like this:
  def extension_white_list
    %w(jpg jpeg gif png)
  end

  # Override the filename of the uploaded files:
  # Avoid using model.id or version_name here, see uploader/store.rb for details.
  # def filename
  #   "something.jpg" if original_filename
  # end

  protected
    def is_file_upload? picture
      model.remote_image_url.blank?
    end

    # def is_not_file_upload? picture
    #   not model.remote_image_url.blank?
    # end
end

【问题讨论】:

  • 可能与这个 SO 问题有关:stackoverflow.com/questions/8579425/…
  • @Arkan 嘿,我尝试了建议的解决方案,但我得到了错误 undefined method `to_file' for #<:storage::fog::file:0x00000004f24870>

标签: ruby-on-rails-3 carrierwave fog


【解决方案1】:

我找到了解决办法

after :store, :remove_original_file

  def remove_original_file(p)
    if self.version_name.nil?
      self.file.delete if self.file.exists?
    end
  end

这将从 aws 中删除图像

【讨论】:

    【解决方案2】:

    有点不清楚你在追求什么。是这样的场景:用户上传图片A,你将图片A加工成版本B和C,并且想丢弃图片A并保留版本B和C?如果是这样,请尝试类似

    class ImageUploader < CarrierWave::Uploader::Base
      # include CarrierWaveDirect::Uploader
    
      # Include RMagick or MiniMagick support:
      include CarrierWave::RMagick
      # include CarrierWave::MiniMagick
    
      # Include the Sprockets helpers for Rails 3.1+ asset pipeline compatibility:
      include Sprockets::Helpers::RailsHelper
      include Sprockets::Helpers::IsolatedHelper
    
      # Choose what kind of storage to use for this uploader:
      # storage :file
      storage :fog
    
      process :set_content_type
      process :resize_to_limit => [1000, 1000]
      process :convert => 'png'
    
      version :C do 
        process :resize_to_limit => [200, 200]
        process :convert => 'png'
      end
    

    在这种情况下,您将存储两个版本的原始上传图像:第一个是 1000xwhatever,第二个是 200xwhatever。原始图像未保存。第一个版本可以直接从 .remote_image 访问,而第二个版本可以从 .remote_image.C 访问。有意义吗?

    【讨论】:

    • 这是有道理的,但这并不是我所追求的。我想知道是否有办法只保留版本 C。
    • 然后将流程语句放在上传器的顶层(不嵌套在版本中)。例如,如果您有一个没有版本的上传器,并且这些要处理 process :resize_to_limit =&gt; [200, 200]process :convert =&gt; 'png',您将不会保存原始版本,而只会保存一个适合 200x200 正方形的副本。
    猜你喜欢
    • 2014-06-28
    • 2012-07-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-11
    • 1970-01-01
    • 2012-12-20
    相关资源
    最近更新 更多