gearheads
/
mastodon
Archived
2
0
Fork 0
This repository has been archived on 2024-06-09. You can view files and clone it, but cannot push or open issues/pull-requests.
mastodon/config/initializers/paperclip.rb

88 lines
2.7 KiB
Ruby
Raw Normal View History

2016-12-04 19:07:02 +01:00
# frozen_string_literal: true
Paperclip.options[:read_timeout] = 60
Paperclip.interpolates :filename do |attachment, style|
return attachment.original_filename if style == :original
2017-05-05 21:32:14 +02:00
[basename(attachment, style), extension(attachment, style)].delete_if(&:blank?).join('.')
end
2017-11-07 14:30:31 +01:00
Paperclip::Attachment.default_options.merge!(
use_timestamp: false,
path: ':class/:attachment/:id_partition/:style/:filename',
storage: :fog
)
2016-11-06 18:35:46 +01:00
if ENV['S3_ENABLED'] == 'true'
require 'aws-sdk'
Aws.eager_autoload!(services: %w(S3))
2017-12-09 00:47:52 +01:00
s3_region = ENV.fetch('S3_REGION') { 'us-east-1' }
s3_protocol = ENV.fetch('S3_PROTOCOL') { 'https' }
2017-12-09 05:59:59 +01:00
s3_hostname = ENV.fetch('S3_HOSTNAME') { "s3-#{s3_region}.amazonaws.com" }
2016-11-06 18:35:46 +01:00
2017-11-07 14:30:31 +01:00
Paperclip::Attachment.default_options.merge!(
2017-12-09 00:47:52 +01:00
storage: :s3,
s3_protocol: s3_protocol,
s3_host_name: s3_hostname,
s3_headers: {
'Cache-Control' => 'max-age=315576000',
2017-11-07 14:30:31 +01:00
},
2017-12-09 00:47:52 +01:00
s3_permissions: ENV.fetch('S3_PERMISSION') { 'public-read' },
s3_region: s3_region,
s3_credentials: {
bucket: ENV['S3_BUCKET'],
access_key_id: ENV['AWS_ACCESS_KEY_ID'],
secret_access_key: ENV['AWS_SECRET_ACCESS_KEY'],
},
s3_options: {
signature_version: ENV.fetch('S3_SIGNATURE_VERSION') { 'v4' },
}
2017-11-07 14:30:31 +01:00
)
2017-11-07 14:30:31 +01:00
if ENV.has_key?('S3_ENDPOINT')
2017-12-09 00:47:52 +01:00
Paperclip::Attachment.default_options[:s3_options].merge!(
2017-11-07 14:30:31 +01:00
endpoint: ENV['S3_ENDPOINT'],
2017-12-09 00:47:52 +01:00
force_path_style: true
2017-11-07 14:30:31 +01:00
)
2017-12-09 00:47:52 +01:00
Paperclip::Attachment.default_options[:url] = ':s3_path_url'
end
2016-12-07 17:19:29 +01:00
2017-11-07 14:30:31 +01:00
if ENV.has_key?('S3_CLOUDFRONT_HOST')
2017-12-09 00:47:52 +01:00
Paperclip::Attachment.default_options.merge!(
url: ':s3_alias_url',
s3_host_alias: ENV['S3_CLOUDFRONT_HOST']
)
2016-12-07 17:19:29 +01:00
end
elsif ENV['SWIFT_ENABLED'] == 'true'
2017-11-07 14:30:31 +01:00
require 'fog/openstack'
Paperclip::Attachment.default_options.merge!(
fog_credentials: {
provider: 'OpenStack',
2017-11-07 14:30:31 +01: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' },
openstack_region: ENV['SWIFT_REGION'],
2017-11-07 14:30:31 +01:00
openstack_cache_ttl: ENV.fetch('SWIFT_CACHE_TTL') { 60 },
},
2017-11-07 14:30:31 +01:00
fog_directory: ENV['SWIFT_CONTAINER'],
fog_host: ENV['SWIFT_OBJECT_URL'],
fog_public: true
)
else
2017-11-07 14:30:31 +01: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 18:35:46 +01:00
end