Skip to content

Instantly share code, notes, and snippets.

@ncherro
Created November 23, 2014 01:58
Show Gist options
  • Save ncherro/b86003998a0a02ade1c4 to your computer and use it in GitHub Desktop.
Save ncherro/b86003998a0a02ade1c4 to your computer and use it in GitHub Desktop.
CarrierWave base class - appends digest hash to the original filename
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