demarches-normaliennes/app/uploaders/piece_justificative_uploader.rb

52 lines
1.4 KiB
Ruby
Raw Normal View History

class PieceJustificativeUploader < BaseUploader
2016-05-18 11:43:32 +02:00
before :cache, :set_original_filename
2015-08-10 11:05:06 +02:00
# 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
2015-08-10 11:05:06 +02:00
# 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?
2016-05-24 18:35:25 +02:00
"./uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end
2015-08-10 11:05:06 +02:00
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
2018-10-01 13:55:12 +02:00
['pdf', 'doc', 'docx', 'xls', 'xlsx', 'ppt', 'pptx', 'odt', 'ods', 'odp', 'jpg', 'jpeg', 'png']
2015-08-10 11:05:06 +02:00
end
def filename
2016-05-18 11:43:32 +02:00
if original_filename.present? || model.content_secure_token
2018-04-18 12:24:37 +02:00
if Flipflop.remote_storage?
filename = "#{model.class.to_s.underscore}-#{secure_token}.#{file.extension.downcase}"
else
filename = "#{model.class.to_s.underscore}.#{file.extension.downcase}"
end
end
2016-05-18 11:43:32 +02:00
filename
end
private
def secure_token
model.content_secure_token ||= generate_secure_token
end
2015-08-10 11:05:06 +02:00
def generate_secure_token
SecureRandom.uuid
end
2016-05-18 11:43:32 +02:00
def set_original_filename(file)
2018-10-01 13:24:37 +02:00
if file.respond_to?(:original_filename)
model.original_filename ||= file.original_filename
end
end
2015-08-10 11:05:06 +02:00
end