Reputation: 12399
I looked at the carrierwave wiki on github, and used the method they describe to generate unique file names:
def filename
@filename ||= "#{secure_token}.#{file.extension}" if original_filename.present?
end
private
def secure_token
var = :"@#{mounted_as}_secure_token"
random_token = Digest::SHA2.hexadigest("#{Time.now.utc}--#{model.id.to_s}").first(20)
model.instance_variable_get(var) or model.instance_variable_set(var, random_token)
end
(Using hashes though)
The problem I am having is that the filename is still being set to the original file's name. It's as if the filename
mehod is being ignored. Not really sure what is going on. I reset the server and everything, still getting the original filename on the uploaded file and thumbnail version.
Upvotes: 1
Views: 738
Reputation: 6213
I'm not sure why the docs used ||=
operator in filename
method, but this way the unique file name will not be set unless @filename
is nil
, which doesn't seem to be the usual case. Using =
instead of ||=
(or not using assignment at all) seems to solve the issue.
def filename
@filename = "#{secure_token}.#{file.extension}" if original_filename.present?
# The following line works too
#"#{secure_token}.#{file.extension}" if original_filename.present?
end
Upvotes: 2