如何使用Fog和Carrierwave保存原始图像

时间:2013-06-20 06:22:28

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

我一整天都在搜索如何在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

然而,这会产生错误:没有这样的文件或目录 - 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

2 个答案:

答案 0 :(得分: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中删除图像

答案 1 :(得分:0)

有点不清楚你在追求什么。方案是:用户将上传图像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访问。有意义吗?