Search code examples
ruby-on-railsimage-processingamazon-s3carrierwavesidekiq

CarrierWave: Uploads and saves original file...I don't want it to


I am using carrierwave_backgrounder to upload images to S3 in a background process with Sidekiq.

Here's my background_uploader.rb class...

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 contains:

mount_uploader :image, BackgroundUploader
process_in_background :image

I then run sidekiq -q carrierwave to fire up the background workers. All is working good! Upload a file, I see the queue accept it and start working...

If I immediately open up my AWS S3 Console I see the original file in there. Non-resized and un-blurred. Once the job is complete...I refresh S3 and there's the resized/blurred version. Now both images are in there, but I only want the blurred image to be there. In my view I use...

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

It displays the original file. If I check the checkbox to remove the file, it does so as it should (deletes the original from S3), but the blurred version stays there.

What's getting uploaded to S3...

  • original.jpg (immediate...I don't want this uploaded at all)
  • modified.jpg (after job completes)

Long story short: I don't want the original file to upload to S3.


Solution

  • I think your problem is the filename method, which carrierwave may rely on to find (and delete) the original file. Does the problem go away when you use a filename that doesn't change between when it's initially stored and when you process it?