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.

113 lines
3.6 KiB

  1. # frozen_string_literal: true
  2. Paperclip::DataUriAdapter.register
  3. Paperclip.interpolates :filename do |attachment, style|
  4. if style == :original
  5. attachment.original_filename
  6. else
  7. [basename(attachment, style), extension(attachment, style)].delete_if(&:blank?).join('.')
  8. end
  9. end
  10. Paperclip.interpolates :prefix_path do |attachment, style|
  11. if attachment.storage_schema_version >= 1 && attachment.instance.respond_to?(:local?) && !attachment.instance.local?
  12. 'cache' + File::SEPARATOR
  13. else
  14. ''
  15. end
  16. end
  17. Paperclip.interpolates :prefix_url do |attachment, style|
  18. if attachment.storage_schema_version >= 1 && attachment.instance.respond_to?(:local?) && !attachment.instance.local?
  19. 'cache/'
  20. else
  21. ''
  22. end
  23. end
  24. Paperclip::Attachment.default_options.merge!(
  25. use_timestamp: false,
  26. path: ':prefix_url:class/:attachment/:id_partition/:style/:filename',
  27. storage: :fog
  28. )
  29. if ENV['S3_ENABLED'] == 'true'
  30. require 'aws-sdk-s3'
  31. s3_region = ENV.fetch('S3_REGION') { 'us-east-1' }
  32. s3_protocol = ENV.fetch('S3_PROTOCOL') { 'https' }
  33. s3_hostname = ENV.fetch('S3_HOSTNAME') { "s3-#{s3_region}.amazonaws.com" }
  34. Paperclip::Attachment.default_options.merge!(
  35. storage: :s3,
  36. s3_protocol: s3_protocol,
  37. s3_host_name: s3_hostname,
  38. s3_headers: {
  39. 'X-Amz-Multipart-Threshold' => ENV.fetch('S3_MULTIPART_THRESHOLD') { 15.megabytes }.to_i,
  40. 'Cache-Control' => 'public, max-age=315576000, immutable',
  41. },
  42. s3_permissions: ENV.fetch('S3_PERMISSION') { 'public-read' },
  43. s3_region: s3_region,
  44. s3_credentials: {
  45. bucket: ENV['S3_BUCKET'],
  46. access_key_id: ENV['AWS_ACCESS_KEY_ID'],
  47. secret_access_key: ENV['AWS_SECRET_ACCESS_KEY'],
  48. },
  49. s3_options: {
  50. signature_version: ENV.fetch('S3_SIGNATURE_VERSION') { 'v4' },
  51. http_open_timeout: ENV.fetch('S3_OPEN_TIMEOUT'){ '5' }.to_i,
  52. http_read_timeout: 5,
  53. http_idle_timeout: 5,
  54. retry_limit: 0,
  55. }
  56. )
  57. if ENV.has_key?('S3_ENDPOINT')
  58. Paperclip::Attachment.default_options[:s3_options].merge!(
  59. endpoint: ENV['S3_ENDPOINT'],
  60. force_path_style: ENV['S3_OVERRIDE_PATH_STYLE'] != 'true',
  61. )
  62. Paperclip::Attachment.default_options[:url] = ':s3_path_url'
  63. end
  64. if ENV.has_key?('S3_ALIAS_HOST') || ENV.has_key?('S3_CLOUDFRONT_HOST')
  65. Paperclip::Attachment.default_options.merge!(
  66. url: ':s3_alias_url',
  67. s3_host_alias: ENV['S3_ALIAS_HOST'] || ENV['S3_CLOUDFRONT_HOST']
  68. )
  69. end
  70. elsif ENV['SWIFT_ENABLED'] == 'true'
  71. require 'fog/openstack'
  72. Paperclip::Attachment.default_options.merge!(
  73. fog_credentials: {
  74. provider: 'OpenStack',
  75. openstack_username: ENV['SWIFT_USERNAME'],
  76. openstack_project_id: ENV['SWIFT_PROJECT_ID'],
  77. openstack_project_name: ENV['SWIFT_TENANT'],
  78. openstack_tenant: ENV['SWIFT_TENANT'], # Some OpenStack-v2 ignores project_name but needs tenant
  79. openstack_api_key: ENV['SWIFT_PASSWORD'],
  80. openstack_auth_url: ENV['SWIFT_AUTH_URL'],
  81. openstack_domain_name: ENV.fetch('SWIFT_DOMAIN_NAME') { 'default' },
  82. openstack_region: ENV['SWIFT_REGION'],
  83. openstack_cache_ttl: ENV.fetch('SWIFT_CACHE_TTL') { 60 },
  84. },
  85. fog_directory: ENV['SWIFT_CONTAINER'],
  86. fog_host: ENV['SWIFT_OBJECT_URL'],
  87. fog_public: true
  88. )
  89. else
  90. Paperclip::Attachment.default_options.merge!(
  91. storage: :filesystem,
  92. use_timestamp: true,
  93. path: File.join(ENV.fetch('PAPERCLIP_ROOT_PATH', File.join(':rails_root', 'public', 'system')), ':prefix_path:class', ':attachment', ':id_partition', ':style', ':filename'),
  94. url: ENV.fetch('PAPERCLIP_ROOT_URL', '/system') + '/:prefix_url:class/:attachment/:id_partition/:style/:filename',
  95. )
  96. end