0
votes

I'm using Carrierwave to upload some images and generate different versions of them (size, color...). Every image is given a unique name when uploaded and I'm now trying to process the images in the background in order to avoid blocking web processes. I use carrierwave_backgrounder for that.

The problem is that when my image is processed, a new name is given to it. So the model doesn't know this new name and the original image is now twice on the server.

I end up with this kind of files:

f9f97657-eaab-40ce-b965-31bb128066ee.jpg // First uploaded images
e4244551-7f43-4c03-8747-e8f2f2e57156.jpg // Copy of the original image created while processed in the background
thumb_e4244551-7f43-4c03-8747-e8f2f2e57156.jpg

And event.image returns f9f97657-eaab-40ce-b965-31bb128066ee.jpg.

How can I avoid to generate a new filename if we are just processing the different versions in the background and not uploading it?

My model:

class Event < ActiveRecord::Base

  mount_uploader :cover_image, CoverImageUploader
  process_in_background :cover_image

end

My uploader:

class CoverImageUploader < CarrierWave::Uploader::Base
  include CarrierWave::RMagick
  include CarrierWave::MimeTypes
  include Sprockets::Helpers::RailsHelper
  include Sprockets::Helpers::IsolatedHelper

  include ::CarrierWave::Backgrounder::Delay

  def filename
    "#{secure_token}.#{file.extension}" if original_filename
  end

  process :set_content_type

  version :thumb do
    process resize_to_fill: [200, 200]
  end

  # ...
  # Some other versions processing...
  # ...

  protected

  def secure_token
    var = :"@#{mounted_as}_secure_token"
    model.instance_variable_get(var) || model.instance_variable_set(var, SecureRandom.uuid)
  end
end
1

1 Answers

2
votes

can try this:

def filename
    if original_filename.present?
        if model.respond_to?("#{mounted_as}_processing") && model.send("#{mounted_as}_processing") 
            @name ||= model.send("#{mounted_as}_identifier")
        else
            @name ||= "#{secure_token}.#{file.extension}"
        end
    end
end

only works if you have a "processing" column in your table