2016-12-04 20:07:02 +02:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-01-08 20:12:54 +02:00
|
|
|
Paperclip.options[:read_timeout] = 60
|
2017-01-07 16:43:56 +02:00
|
|
|
|
2017-03-06 00:03:49 +02:00
|
|
|
Paperclip.interpolates :filename do |attachment, style|
|
|
|
|
return attachment.original_filename if style == :original
|
2017-05-05 22:32:14 +03:00
|
|
|
[basename(attachment, style), extension(attachment, style)].delete_if(&:blank?).join('.')
|
2017-03-06 00:03:49 +02:00
|
|
|
end
|
|
|
|
|
2017-11-07 15:30:31 +02:00
|
|
|
Paperclip::Attachment.default_options.merge!(
|
|
|
|
use_timestamp: false,
|
|
|
|
path: ':class/:attachment/:id_partition/:style/:filename',
|
|
|
|
storage: :fog
|
|
|
|
)
|
2017-10-09 18:52:02 +03:00
|
|
|
|
2016-11-06 19:35:46 +02:00
|
|
|
if ENV['S3_ENABLED'] == 'true'
|
2017-11-07 15:30:31 +02:00
|
|
|
require 'fog/aws'
|
2016-11-29 15:20:15 +02:00
|
|
|
|
2017-11-07 15:30:31 +02:00
|
|
|
s3_protocol = ENV.fetch('S3_PROTOCOL') { 'https' }
|
|
|
|
s3_hostname = ENV.fetch('S3_HOSTNAME') { "s3-#{ENV['S3_REGION']}.amazonaws.com" }
|
|
|
|
aws_signature_version = ENV['S3_SIGNATURE_VERSION'] == 's3' ? 2 : ENV['S3_SIGNATURE_VERSION'].to_i
|
|
|
|
aws_signature_version = 4 if aws_signature_version.zero?
|
2016-11-06 19:35:46 +02:00
|
|
|
|
2017-11-07 15:30:31 +02:00
|
|
|
Paperclip::Attachment.default_options.merge!(
|
|
|
|
fog_credentials: {
|
|
|
|
provider: 'AWS',
|
|
|
|
aws_access_key_id: ENV['AWS_ACCESS_KEY_ID'],
|
|
|
|
aws_secret_access_key: ENV['AWS_SECRET_ACCESS_KEY'],
|
|
|
|
aws_signature_version: aws_signature_version,
|
|
|
|
region: ENV.fetch('S3_REGION') { 'us-east-1' },
|
|
|
|
scheme: s3_protocol,
|
|
|
|
host: s3_hostname
|
|
|
|
},
|
|
|
|
fog_directory: ENV['S3_BUCKET'],
|
|
|
|
fog_options: {
|
|
|
|
acl: ENV.fetch('S3_PERMISSION') { 'public-read' },
|
|
|
|
cache_control: 'max-age=315576000',
|
2016-12-07 17:59:18 +02:00
|
|
|
}
|
2017-11-07 15:30:31 +02:00
|
|
|
)
|
2016-12-07 17:59:18 +02:00
|
|
|
|
2017-11-07 15:30:31 +02:00
|
|
|
if ENV.has_key?('S3_ENDPOINT')
|
|
|
|
Paperclip::Attachment.default_options[:fog_credentials].merge!(
|
|
|
|
endpoint: ENV['S3_ENDPOINT'],
|
|
|
|
path_style: true
|
|
|
|
)
|
|
|
|
Paperclip::Attachment.default_options[:fog_host] = "#{s3_protocol}://#{s3_hostname}/#{ENV['S3_BUCKET']}"
|
2016-12-07 17:59:18 +02:00
|
|
|
end
|
2016-12-07 18:19:29 +02:00
|
|
|
|
2017-11-07 15:30:31 +02:00
|
|
|
if ENV.has_key?('S3_CLOUDFRONT_HOST')
|
|
|
|
Paperclip::Attachment.default_options[:fog_host] = "#{s3_protocol}://#{ENV['S3_CLOUDFRONT_HOST']}"
|
2016-12-07 18:19:29 +02:00
|
|
|
end
|
2017-09-06 00:17:06 +03:00
|
|
|
elsif ENV['SWIFT_ENABLED'] == 'true'
|
2017-11-07 15:30:31 +02:00
|
|
|
require 'fog/openstack'
|
|
|
|
|
2017-09-06 00:17:06 +03:00
|
|
|
Paperclip::Attachment.default_options.merge!(
|
|
|
|
fog_credentials: {
|
|
|
|
provider: 'OpenStack',
|
2017-11-07 15:30:31 +02:00
|
|
|
openstack_username: ENV['SWIFT_USERNAME'],
|
|
|
|
openstack_project_name: ENV['SWIFT_TENANT'],
|
|
|
|
openstack_tenant: ENV['SWIFT_TENANT'], # Some OpenStack-v2 ignores project_name but needs tenant
|
|
|
|
openstack_api_key: ENV['SWIFT_PASSWORD'],
|
|
|
|
openstack_auth_url: ENV['SWIFT_AUTH_URL'],
|
|
|
|
openstack_domain_name: ENV.fetch('SWIFT_DOMAIN_NAME') { 'default' },
|
2017-09-11 16:11:13 +03:00
|
|
|
openstack_region: ENV['SWIFT_REGION'],
|
2017-11-07 15:30:31 +02:00
|
|
|
openstack_cache_ttl: ENV.fetch('SWIFT_CACHE_TTL') { 60 },
|
2017-09-06 00:17:06 +03:00
|
|
|
},
|
2017-11-07 15:30:31 +02:00
|
|
|
fog_directory: ENV['SWIFT_CONTAINER'],
|
2017-11-07 20:06:30 +02:00
|
|
|
fog_host: ENV['SWIFT_OBJECT_URL'],
|
2017-09-06 00:17:06 +03:00
|
|
|
fog_public: true
|
|
|
|
)
|
2017-04-15 03:07:21 +03:00
|
|
|
else
|
2017-11-07 15:30:31 +02:00
|
|
|
require 'fog/local'
|
|
|
|
|
|
|
|
Paperclip::Attachment.default_options.merge!(
|
|
|
|
fog_credentials: {
|
|
|
|
provider: 'Local',
|
|
|
|
local_root: ENV.fetch('PAPERCLIP_ROOT_PATH') { Rails.root.join('public', 'system') },
|
|
|
|
},
|
|
|
|
fog_directory: '',
|
|
|
|
fog_host: ENV.fetch('PAPERCLIP_ROOT_URL') { '/system' }
|
|
|
|
)
|
2016-11-06 19:35:46 +02:00
|
|
|
end
|