0
votes

I am attempting to customize the original filename for an image uploaded through CarrierWave by appending some sort of unique UUID. The problem is that I do not want this UUID to also be appended onto the subsequent version names.

The objective here is I want the original file to be secured by obscurity. So a user cannot just take "thumb_image.jpg" and access the original "image.jpg" simply by removing "thumb_". Something like this would be the goal:

  • "00000001-image.jpg" (original)
  • "thumb-image.jpg" (version 1)
  • "small-thumb-image.jpg" (version 2)
  • etc.

I cannot seem to find a way to get this integrated quite right using the following wiki docs:

Any help would be appreciated. Thanks!

1

1 Answers

0
votes

Ok, it seems i was able to get this working with the following approach:

def filename
  "#{secure_token}_#{original_filename.chomp(File.extname(super))}.#{file.extension}" if original_filename.present?
end

version :thumb do
  process convert: 'jpg'

  def full_filename(for_file = file)
    "#{model.id}_thumb.jpg"
  end
end

protected

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