demarches-normaliennes/app/uploaders/procedure_logo_uploader.rb

48 lines
1.1 KiB
Ruby
Raw Normal View History

class ProcedureLogoUploader < BaseUploader
2016-05-24 18:35:25 +02:00
def root
2018-03-06 13:44:29 +01:00
Rails.root.join("public")
2016-05-24 18:35:25 +02:00
end
# Choose what kind of storage to use for this uploader:
2018-04-18 12:24:37 +02:00
if Flipflop.remote_storage?
storage :fog
else
storage :file
end
# Override the directory where uploaded files will be stored.
# This is a sensible default for uploaders that are meant to be mounted:
def store_dir
2018-04-18 12:24:37 +02:00
if !Flipflop.remote_storage?
"uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end
end
# Add a white list of extensions which are allowed to be uploaded.
# For images you might use something like this:
def extension_white_list
%w(jpg jpeg png)
end
def filename
2016-05-18 11:43:32 +02:00
if original_filename.present? || model.logo_secure_token
2018-04-18 12:24:37 +02:00
if Flipflop.remote_storage?
2016-05-24 17:34:25 +02:00
filename = "#{model.class.to_s.underscore}-#{secure_token}.#{file.extension.downcase}"
else
filename = "logo-#{secure_token}.#{file.extension.downcase}"
end
end
2016-05-18 11:43:32 +02:00
filename
end
private
def secure_token
model.logo_secure_token ||= generate_secure_token
end
def generate_secure_token
SecureRandom.uuid
end
end