1

我整天都在搜索如何不将原始文件保存在 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
4

2 回答 2

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 中删除图像

于 2016-03-31T08:31:43.903 回答
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 访问。说得通?

于 2013-06-26T03:09:21.367 回答