Created
November 23, 2014 01:58
-
-
Save ncherro/b86003998a0a02ade1c4 to your computer and use it in GitHub Desktop.
CarrierWave base class - appends digest hash to the original filename
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class BaseUploader < CarrierWave::Uploader::Base | |
include ::CarrierWave::MimeTypes | |
storage Rails.env.development? ? :file : :fog | |
process :set_content_type | |
# append digests to the original filename so we can validate file uniqueness | |
def md5 | |
@md5 ||= Digest::MD5.hexdigest(model.send(mounted_as).read.to_s) | |
end | |
def filename | |
if original_filename.present? && super.present? && !super.include?("-#{md5}") | |
fname = super.split('.') | |
ext = fname.pop | |
@name ||= "#{fname.join('.')}-#{md5}.#{ext}" | |
else | |
super | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment