CarrierWave:上传并保存原始文件...我不想让它



我正在使用carrierwave_backgrounder在Sidekiq的后台进程中将图像上传到S3。

这是我的 background_uploader.rb 类...

class BackgroundUploader < CarrierWave::Uploader::Base
  include ::CarrierWave::Backgrounder::Delay
  include CarrierWave::RMagick
  include CarrierWave::MimeTypes
  process :set_content_type
  # Include the Sprockets helpers for Rails 3.1+ asset pipeline compatibility:
  include Sprockets::Helpers::RailsHelper
  include Sprockets::Helpers::IsolatedHelper
  storage :fog
  def store_dir
    "uploads/backgrounds/#{model.id}"
  end
  def default_url
    "/assets/default.jpg"
  end
  process :resize_to_fit => [1024, 1024]
  process :convert => 'jpg'
  process :fix_exif_rotation
  def extension_white_list
    %w(jpg jpeg png)
  end
  def filename
    @name ||= Digest::MD5.hexdigest(File.dirname(current_path.to_s))
    "#{@name}.#{file.extension}" if original_filename
  end
  # Rotates the image based on the EXIF Orientation & applies gaussian blur
  def fix_exif_rotation
    manipulate! do |img|
      img.auto_orient!
      img = yield(img) if block_given?
      img = img.gaussian_blur(0.0, 20.0)
      img
    end
  end
end

carrierwave_backgrounder.rb

CarrierWave::Backgrounder.configure do |c|
  c.backend :sidekiq, queue: :carrierwave
end

background.rb 包含:

mount_uploader :image, BackgroundUploader
process_in_background :image

然后我运行sidekiq -q carrierwave来启动后台工作人员。一切都很好!上传文件,我看到队列接受它并开始工作...

如果我立即打开我的 AWS S3 控制台,我会看到其中的原始文件。不调整大小和不模糊。作业完成后...我刷新 S3,有调整大小/模糊的版本。现在两个图像都在那里,但我只希望模糊的图像在那里。在我看来,我使用...

 <%= image_tag(@background.image.to_s) %>

它显示原始文件。如果我选中复选框以删除文件,它会按预期方式删除(从 S3 中删除原始文件),但模糊版本保留在那里。

什么上传到 S3...

  • 原.jpg(立即...我根本不想要上传这个)
  • 修改.jpg(作业完成后)

长话短说:我不希望原始文件上传到 S3。

我认为您的问题是filename方法,carrierwave可能会依靠该方法来查找(并删除)原始文件。当您使用的文件名在最初存储和处理它之间没有变化时,问题会消失吗?

相关内容

最新更新