I am trying to change the basename (filename) of photos:
In my model I have:
attr_accessor :image_url, :basename
has_attached_file :image,
Paperclip now allows you to pass in a FilenameCleaner object when setting up has_attached_file
.
Your FilenameCleaner object must respond to call
with filename as the only parameter. The default FilenameCleaner removes invalid characters if restricted_characters
option is supplied when setting up has_attached_file
.
So it'll look something like:
has_attached_file :image,
filename_cleaner: MyRandomFilenameCleaner.new
styles: { thumbnail: '100x100' }
And MyRandomFilenameCleaner will be:
class MyRandomFilenameCleaner
def call(filename)
extension = File.extname(filename).downcase
"#{Digest::SHA1.hexdigest(filename + Time.current.to_s).slice(0..10)}#{extension}"
end
end
You could get away with passing in a class that has a self.call
method rather than an object but this conforms to Paperclip's documentation in Attachment.rb.