You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

66 lines
3.0 KiB

  1. # frozen_string_literal: true
  2. Paperclip.options[:read_timeout] = 60
  3. Paperclip.interpolates :filename do |attachment, style|
  4. return attachment.original_filename if style == :original
  5. [basename(attachment, style), extension(attachment, style)].delete_if(&:blank?).join('.')
  6. end
  7. Paperclip::Attachment.default_options[:use_timestamp] = false
  8. if ENV['S3_ENABLED'] == 'true'
  9. Aws.eager_autoload!(services: %w(S3))
  10. Paperclip::Attachment.default_options[:storage] = :s3
  11. Paperclip::Attachment.default_options[:s3_protocol] = ENV.fetch('S3_PROTOCOL') { 'https' }
  12. Paperclip::Attachment.default_options[:url] = ':s3_domain_url'
  13. Paperclip::Attachment.default_options[:s3_host_name] = ENV.fetch('S3_HOSTNAME') { "s3-#{ENV.fetch('S3_REGION')}.amazonaws.com" }
  14. Paperclip::Attachment.default_options[:path] = '/:class/:attachment/:id_partition/:style/:filename'
  15. Paperclip::Attachment.default_options[:s3_headers] = { 'Cache-Control' => 'max-age=315576000' }
  16. Paperclip::Attachment.default_options[:s3_permissions] = ENV.fetch('S3_PERMISSION') { 'public-read' }
  17. Paperclip::Attachment.default_options[:s3_region] = ENV.fetch('S3_REGION') { 'us-east-1' }
  18. Paperclip::Attachment.default_options[:s3_credentials] = {
  19. bucket: ENV.fetch('S3_BUCKET'),
  20. access_key_id: ENV.fetch('AWS_ACCESS_KEY_ID'),
  21. secret_access_key: ENV.fetch('AWS_SECRET_ACCESS_KEY'),
  22. }
  23. unless ENV['S3_ENDPOINT'].blank?
  24. Paperclip::Attachment.default_options[:s3_options] = {
  25. endpoint: ENV['S3_ENDPOINT'],
  26. signature_version: ENV['S3_SIGNATURE_VERSION'] || 'v4',
  27. force_path_style: true,
  28. }
  29. Paperclip::Attachment.default_options[:url] = ':s3_path_url'
  30. end
  31. unless ENV['S3_CLOUDFRONT_HOST'].blank?
  32. Paperclip::Attachment.default_options[:url] = ':s3_alias_url'
  33. Paperclip::Attachment.default_options[:s3_host_alias] = ENV['S3_CLOUDFRONT_HOST']
  34. end
  35. elsif ENV['SWIFT_ENABLED'] == 'true'
  36. Paperclip::Attachment.default_options.merge!(
  37. path: ':class/:attachment/:id_partition/:style/:filename',
  38. storage: :fog,
  39. fog_credentials: {
  40. provider: 'OpenStack',
  41. openstack_username: ENV.fetch('SWIFT_USERNAME'),
  42. openstack_project_name: ENV.fetch('SWIFT_TENANT'),
  43. openstack_tenant: ENV.fetch('SWIFT_TENANT'), # Some OpenStack-v2 ignores project_name but needs tenant
  44. openstack_api_key: ENV.fetch('SWIFT_PASSWORD'),
  45. openstack_auth_url: ENV.fetch('SWIFT_AUTH_URL'),
  46. openstack_domain_name: ENV['SWIFT_DOMAIN_NAME'] || 'default',
  47. openstack_region: ENV['SWIFT_REGION'],
  48. openstack_cache_ttl: ENV['SWIFT_CACHE_TTL'] || 60,
  49. },
  50. fog_directory: ENV.fetch('SWIFT_CONTAINER'),
  51. fog_host: ENV['SWIFT_OBJECT_URL'],
  52. fog_public: true
  53. )
  54. else
  55. Paperclip::Attachment.default_options[:path] = (ENV['PAPERCLIP_ROOT_PATH'] || ':rails_root/public/system') + '/:class/:attachment/:id_partition/:style/:filename'
  56. Paperclip::Attachment.default_options[:url] = (ENV['PAPERCLIP_ROOT_URL'] || '/system') + '/:class/:attachment/:id_partition/:style/:filename'
  57. end