Renaming uploaded files with Carrierwave

24,363

Solution 1

Well, another problem with your random filename generator is that it's possible to have collisions isn't it? You could possibly generate a filename that was already generated. One way to go about it would be to somehow generate a hash based on unique properties of the image, like file path. An example, from the carrierwave group:

def filename 
  if original_filename 
    @name ||= Digest::MD5.hexdigest(File.dirname(current_path))
    "#{@name}.#{file.extension}"
  end
end

This will create an MD5 hash based on the current path and then append the original file's extension to it.

Edit: The carrierwave wiki added an entry with a few methods on how to create random and unique filenames for all versioned files.

Solution 2

To have a real unique filename (not almost unique) I recommend to use the uuid gem.

in Gemfile add:

gem 'uuid'

in file_uploader.rb:

def filename
  if original_filename
    if model && model.read_attribute(mounted_as).present?
      model.read_attribute(mounted_as)
    else
      @name ||= "#{mounted_as}-#{uuid}.#{file.extension}"
    end
  end
end

protected

def uuid
  UUID.state_file = false
  uuid = UUID.new
  uuid.generate
end

Solution 3

To just make the record.id prefix the filename you can do the following:

class MyUploader < CarrierWave::Uploader::Base

  storage :file

  def store_dir
    model.class.to_s.underscore.pluralize
  end

  def filename
    model.id ? "#{model.id}-#{original_filename}" : original_filename
  end

  def url
    "/#{store_dir}/#{model.id}-#{model.file_before_type_cast}"
  end
end

Solution 4

From the Google Group:

def filename
  @name ||= "#{secure_token}.#{file.extension}" if original_filename
end

private

def secure_token
  ivar = "@#{mounted_as}_secure_token"
  token = model.instance_variable_get(ivar)
  token ||= model.instance_variable_set(ivar, ActiveSupport::SecureRandom.hex(4))  
end

Solution 5

The other solution looks good, but how I did it then was to have a hook that created a random string for a new name on instance creation, then:

 def filename
    "#{model.randomstring}.#{model.image.file.extension}"
 end

in the uploader.

That worked, putting the random name generation as part of the model, then having carrierwave use that.

I am curious which is faster, more effective, reasonable, sound, etc.

Share:
24,363
Admin
Author by

Admin

Updated on June 24, 2020

Comments

  • Admin
    Admin almost 4 years

    I'm using Carrierwave to upload files, and I have it working.

    My issue is attempting to change the name of the uploaded file.

    In the generated uploader.rb there is a method I think I should be using

    def filename
       "something.jpg" if original_filename
       basename = "what"+orginal_filename if original_filename, works
       basename = (0...8).map{65.+(rand(25)).chr}.join if original_filename  # will create a random name for each version, e.g. the orginal, the thumb, and the filename in the db, useless
     end
    

    I can't seem to access items like 'extension' or 'content_type' in sanitized_file.rb, so this is a bit beyond my current skill level right now.

    Any suggestions or exercises for doing this, i.e. generate filename for an uploaded file that works as well as the carrierwave default (do nothing, but does carry on to each version)? Seems like it should be simple enough but I've stumbled over this.