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.

86 lines
2.9 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.merge!(
  8. use_timestamp: false,
  9. path: ':class/:attachment/:id_partition/:style/:filename',
  10. storage: :fog
  11. )
  12. if ENV['S3_ENABLED'] == 'true'
  13. require 'aws-sdk-s3'
  14. s3_region = ENV.fetch('S3_REGION') { 'us-east-1' }
  15. s3_protocol = ENV.fetch('S3_PROTOCOL') { 'https' }
  16. s3_hostname = ENV.fetch('S3_HOSTNAME') { "s3-#{s3_region}.amazonaws.com" }
  17. Paperclip::Attachment.default_options.merge!(
  18. storage: :s3,
  19. s3_protocol: s3_protocol,
  20. s3_host_name: s3_hostname,
  21. s3_headers: {
  22. 'Cache-Control' => 'public, max-age=315576000, immutable',
  23. },
  24. s3_permissions: ENV.fetch('S3_PERMISSION') { 'public-read' },
  25. s3_region: s3_region,
  26. s3_credentials: {
  27. bucket: ENV['S3_BUCKET'],
  28. access_key_id: ENV['AWS_ACCESS_KEY_ID'],
  29. secret_access_key: ENV['AWS_SECRET_ACCESS_KEY'],
  30. },
  31. s3_options: {
  32. signature_version: ENV.fetch('S3_SIGNATURE_VERSION') { 'v4' },
  33. http_open_timeout: 5,
  34. http_read_timeout: 5,
  35. http_idle_timeout: 5,
  36. }
  37. )
  38. if ENV.has_key?('S3_ENDPOINT')
  39. Paperclip::Attachment.default_options[:s3_options].merge!(
  40. endpoint: ENV['S3_ENDPOINT'],
  41. force_path_style: true
  42. )
  43. Paperclip::Attachment.default_options[:url] = ':s3_path_url'
  44. end
  45. if ENV.has_key?('S3_ALIAS_HOST') || ENV.has_key?('S3_CLOUDFRONT_HOST')
  46. Paperclip::Attachment.default_options.merge!(
  47. url: ':s3_alias_url',
  48. s3_host_alias: ENV['S3_ALIAS_HOST'] || ENV['S3_CLOUDFRONT_HOST']
  49. )
  50. end
  51. elsif ENV['SWIFT_ENABLED'] == 'true'
  52. require 'fog/openstack'
  53. Paperclip::Attachment.default_options.merge!(
  54. fog_credentials: {
  55. provider: 'OpenStack',
  56. openstack_username: ENV['SWIFT_USERNAME'],
  57. openstack_project_id: ENV['SWIFT_PROJECT_ID'],
  58. openstack_project_name: ENV['SWIFT_TENANT'],
  59. openstack_tenant: ENV['SWIFT_TENANT'], # Some OpenStack-v2 ignores project_name but needs tenant
  60. openstack_api_key: ENV['SWIFT_PASSWORD'],
  61. openstack_auth_url: ENV['SWIFT_AUTH_URL'],
  62. openstack_domain_name: ENV.fetch('SWIFT_DOMAIN_NAME') { 'default' },
  63. openstack_region: ENV['SWIFT_REGION'],
  64. openstack_cache_ttl: ENV.fetch('SWIFT_CACHE_TTL') { 60 },
  65. },
  66. fog_directory: ENV['SWIFT_CONTAINER'],
  67. fog_host: ENV['SWIFT_OBJECT_URL'],
  68. fog_public: true
  69. )
  70. else
  71. Paperclip::Attachment.default_options.merge!(
  72. storage: :filesystem,
  73. use_timestamp: true,
  74. path: (ENV['PAPERCLIP_ROOT_PATH'] || ':rails_root/public/system') + '/:class/:attachment/:id_partition/:style/:filename',
  75. url: (ENV['PAPERCLIP_ROOT_URL'] || '/system') + '/:class/:attachment/:id_partition/:style/:filename',
  76. )
  77. end