Renaming uploaded files with Carrierwave

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.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)