2019-09-24 16:38:16 +02:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
# CarrierWave uploader for project CAO attachments.
|
|
|
|
# This file defines the parameters for these uploads
|
2015-05-05 03:10:25 +02:00
|
|
|
class ProjectCaoUploader < CarrierWave::Uploader::Base
|
|
|
|
include UploadHelper
|
|
|
|
|
|
|
|
# Choose what kind of storage to use for this uploader:
|
|
|
|
storage :file
|
|
|
|
after :remove, :delete_empty_dirs
|
|
|
|
|
|
|
|
# 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
|
|
|
|
"#{base_store_dir}/#{model.id}"
|
|
|
|
end
|
|
|
|
|
|
|
|
def base_store_dir
|
|
|
|
"uploads/#{model.class.to_s.underscore}"
|
|
|
|
end
|
|
|
|
|
|
|
|
# Add a white list of extensions which are allowed to be uploaded.
|
|
|
|
# For images you might use something like this:
|
2020-06-02 17:57:24 +02:00
|
|
|
def extension_whitelist
|
2016-09-27 14:08:04 +02:00
|
|
|
ENV['ALLOWED_EXTENSIONS'].split(' ')
|
2015-05-05 03:10:25 +02:00
|
|
|
end
|
2020-06-02 19:18:57 +02:00
|
|
|
|
|
|
|
def content_type_whitelist
|
|
|
|
ENV['ALLOWED_MIME_TYPES'].split(' ')
|
|
|
|
end
|
2020-06-03 12:28:04 +02:00
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def check_content_type_whitelist!(new_file)
|
2020-06-03 16:25:13 +02:00
|
|
|
content_type = Marcel::MimeType.for Pathname.new(new_file.file)
|
2020-06-03 12:28:04 +02:00
|
|
|
|
2020-06-03 16:25:13 +02:00
|
|
|
if content_type_whitelist && content_type && !whitelisted_content_type?(content_type)
|
2020-06-03 12:28:04 +02:00
|
|
|
raise CarrierWave::IntegrityError,
|
|
|
|
I18n.translate(:'errors.messages.content_type_whitelist_error',
|
|
|
|
content_type: content_type,
|
|
|
|
allowed_types: Array(content_type_whitelist).join(', '))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def whitelisted_content_type?(content_type)
|
|
|
|
Array(content_type_whitelist).any? do |item|
|
|
|
|
item = Regexp.quote(item) if item.class != Regexp
|
|
|
|
content_type =~ /#{item}/
|
|
|
|
end
|
|
|
|
end
|
2015-05-05 03:10:25 +02:00
|
|
|
end
|