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.

128 lines
4.0 KiB

  1. # frozen_string_literal: true
  2. Paperclip::DataUriAdapter.register
  3. Paperclip::ResponseWithLimitAdapter.register
  4. Paperclip.interpolates :filename do |attachment, style|
  5. if style == :original
  6. attachment.original_filename
  7. else
  8. [basename(attachment, style), extension(attachment, style)].delete_if(&:blank?).join('.')
  9. end
  10. end
  11. Paperclip.interpolates :prefix_path do |attachment, style|
  12. if attachment.storage_schema_version >= 1 && attachment.instance.respond_to?(:local?) && !attachment.instance.local?
  13. 'cache' + File::SEPARATOR
  14. else
  15. ''
  16. end
  17. end
  18. Paperclip.interpolates :prefix_url do |attachment, style|
  19. if attachment.storage_schema_version >= 1 && attachment.instance.respond_to?(:local?) && !attachment.instance.local?
  20. 'cache/'
  21. else
  22. ''
  23. end
  24. end
  25. Paperclip::Attachment.default_options.merge!(
  26. use_timestamp: false,
  27. path: ':prefix_url:class/:attachment/:id_partition/:style/:filename',
  28. storage: :fog
  29. )
  30. if ENV['S3_ENABLED'] == 'true'
  31. require 'aws-sdk-s3'
  32. s3_region = ENV.fetch('S3_REGION') { 'us-east-1' }
  33. s3_protocol = ENV.fetch('S3_PROTOCOL') { 'https' }
  34. s3_hostname = ENV.fetch('S3_HOSTNAME') { "s3-#{s3_region}.amazonaws.com" }
  35. Paperclip::Attachment.default_options.merge!(
  36. storage: :s3,
  37. s3_protocol: s3_protocol,
  38. s3_host_name: s3_hostname,
  39. s3_headers: {
  40. 'X-Amz-Multipart-Threshold' => ENV.fetch('S3_MULTIPART_THRESHOLD') { 15.megabytes }.to_i,
  41. 'Cache-Control' => 'public, max-age=315576000, immutable',
  42. },
  43. s3_permissions: ENV.fetch('S3_PERMISSION') { 'public-read' },
  44. s3_region: s3_region,
  45. s3_credentials: {
  46. bucket: ENV['S3_BUCKET'],
  47. access_key_id: ENV['AWS_ACCESS_KEY_ID'],
  48. secret_access_key: ENV['AWS_SECRET_ACCESS_KEY'],
  49. },
  50. s3_options: {
  51. signature_version: ENV.fetch('S3_SIGNATURE_VERSION') { 'v4' },
  52. http_open_timeout: ENV.fetch('S3_OPEN_TIMEOUT'){ '5' }.to_i,
  53. http_read_timeout: ENV.fetch('S3_READ_TIMEOUT'){ '5' }.to_i,
  54. http_idle_timeout: 5,
  55. retry_limit: 0,
  56. }
  57. )
  58. if ENV.has_key?('S3_ENDPOINT')
  59. Paperclip::Attachment.default_options[:s3_options].merge!(
  60. endpoint: ENV['S3_ENDPOINT'],
  61. force_path_style: ENV['S3_OVERRIDE_PATH_STYLE'] != 'true',
  62. )
  63. Paperclip::Attachment.default_options[:url] = ':s3_path_url'
  64. end
  65. if ENV.has_key?('S3_ALIAS_HOST') || ENV.has_key?('S3_CLOUDFRONT_HOST')
  66. Paperclip::Attachment.default_options.merge!(
  67. url: ':s3_alias_url',
  68. s3_host_alias: ENV['S3_ALIAS_HOST'] || ENV['S3_CLOUDFRONT_HOST']
  69. )
  70. end
  71. elsif ENV['SWIFT_ENABLED'] == 'true'
  72. require 'fog/openstack'
  73. Paperclip::Attachment.default_options.merge!(
  74. fog_credentials: {
  75. provider: 'OpenStack',
  76. openstack_username: ENV['SWIFT_USERNAME'],
  77. openstack_project_id: ENV['SWIFT_PROJECT_ID'],
  78. openstack_project_name: ENV['SWIFT_TENANT'],
  79. openstack_tenant: ENV['SWIFT_TENANT'], # Some OpenStack-v2 ignores project_name but needs tenant
  80. openstack_api_key: ENV['SWIFT_PASSWORD'],
  81. openstack_auth_url: ENV['SWIFT_AUTH_URL'],
  82. openstack_domain_name: ENV.fetch('SWIFT_DOMAIN_NAME') { 'default' },
  83. openstack_region: ENV['SWIFT_REGION'],
  84. openstack_cache_ttl: ENV.fetch('SWIFT_CACHE_TTL') { 60 },
  85. },
  86. fog_directory: ENV['SWIFT_CONTAINER'],
  87. fog_host: ENV['SWIFT_OBJECT_URL'],
  88. fog_public: true
  89. )
  90. else
  91. Paperclip::Attachment.default_options.merge!(
  92. storage: :filesystem,
  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
  97. Rails.application.reloader.to_prepare do
  98. Paperclip.options[:content_type_mappings] = { csv: Import::FILE_TYPES }
  99. end
  100. # In some places in the code, we rescue this exception, but we don't always
  101. # load the S3 library, so it may be an undefined constant:
  102. unless defined?(Seahorse)
  103. module Seahorse
  104. module Client
  105. class NetworkingError < StandardError; end
  106. end
  107. end
  108. end