paperclip.rb 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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)].compact_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. Paperclip::Attachment.default_options[:s3_permissions] = ->(*) { nil } if ENV['S3_PERMISSION'] == ''
  59. if ENV.has_key?('S3_ENDPOINT')
  60. Paperclip::Attachment.default_options[:s3_options].merge!(
  61. endpoint: ENV['S3_ENDPOINT'],
  62. force_path_style: ENV['S3_OVERRIDE_PATH_STYLE'] != 'true',
  63. )
  64. Paperclip::Attachment.default_options[:url] = ':s3_path_url'
  65. end
  66. if ENV.has_key?('S3_ALIAS_HOST') || ENV.has_key?('S3_CLOUDFRONT_HOST')
  67. Paperclip::Attachment.default_options.merge!(
  68. url: ':s3_alias_url',
  69. s3_host_alias: ENV['S3_ALIAS_HOST'] || ENV['S3_CLOUDFRONT_HOST']
  70. )
  71. end
  72. Paperclip::Attachment.default_options[:s3_headers]['X-Amz-Storage-Class'] = ENV['S3_STORAGE_CLASS'] if ENV.has_key?('S3_STORAGE_CLASS')
  73. # Some S3-compatible providers might not actually be compatible with some APIs
  74. # used by kt-paperclip, see https://github.com/mastodon/mastodon/issues/16822
  75. if ENV['S3_FORCE_SINGLE_REQUEST'] == 'true'
  76. module Paperclip
  77. module Storage
  78. module S3Extensions
  79. def copy_to_local_file(style, local_dest_path)
  80. log("copying #{path(style)} to local file #{local_dest_path}")
  81. s3_object(style).download_file(local_dest_path, { mode: 'single_request' })
  82. rescue Aws::Errors::ServiceError => e
  83. warn("#{e} - cannot copy #{path(style)} to local file #{local_dest_path}")
  84. false
  85. end
  86. end
  87. end
  88. end
  89. Paperclip::Storage::S3.prepend(Paperclip::Storage::S3Extensions)
  90. end
  91. elsif ENV['SWIFT_ENABLED'] == 'true'
  92. require 'fog/openstack'
  93. Paperclip::Attachment.default_options.merge!(
  94. fog_credentials: {
  95. provider: 'OpenStack',
  96. openstack_username: ENV['SWIFT_USERNAME'],
  97. openstack_project_id: ENV['SWIFT_PROJECT_ID'],
  98. openstack_project_name: ENV['SWIFT_TENANT'],
  99. openstack_tenant: ENV['SWIFT_TENANT'], # Some OpenStack-v2 ignores project_name but needs tenant
  100. openstack_api_key: ENV['SWIFT_PASSWORD'],
  101. openstack_auth_url: ENV['SWIFT_AUTH_URL'],
  102. openstack_domain_name: ENV.fetch('SWIFT_DOMAIN_NAME') { 'default' },
  103. openstack_region: ENV['SWIFT_REGION'],
  104. openstack_cache_ttl: ENV.fetch('SWIFT_CACHE_TTL') { 60 },
  105. openstack_temp_url_key: ENV['SWIFT_TEMP_URL_KEY'],
  106. },
  107. fog_file: { 'Cache-Control' => 'public, max-age=315576000, immutable' },
  108. fog_directory: ENV['SWIFT_CONTAINER'],
  109. fog_host: ENV['SWIFT_OBJECT_URL'],
  110. fog_public: true
  111. )
  112. elsif ENV['AZURE_ENABLED'] == 'true'
  113. require 'paperclip-azure'
  114. Paperclip::Attachment.default_options.merge!(
  115. storage: :azure,
  116. azure_options: {
  117. protocol: 'https',
  118. },
  119. azure_credentials: {
  120. storage_account_name: ENV['AZURE_STORAGE_ACCOUNT'],
  121. storage_access_key: ENV['AZURE_STORAGE_ACCESS_KEY'],
  122. container: ENV['AZURE_CONTAINER_NAME'],
  123. }
  124. )
  125. if ENV.has_key?('AZURE_ALIAS_HOST')
  126. Paperclip::Attachment.default_options.merge!(
  127. url: ':azure_alias_url',
  128. azure_host_alias: ENV['AZURE_ALIAS_HOST']
  129. )
  130. end
  131. else
  132. Paperclip::Attachment.default_options.merge!(
  133. storage: :filesystem,
  134. path: File.join(ENV.fetch('PAPERCLIP_ROOT_PATH', File.join(':rails_root', 'public', 'system')), ':prefix_path:class', ':attachment', ':id_partition', ':style', ':filename'),
  135. url: ENV.fetch('PAPERCLIP_ROOT_URL', '/system') + '/:prefix_url:class/:attachment/:id_partition/:style/:filename',
  136. )
  137. end
  138. Rails.application.reloader.to_prepare do
  139. Paperclip.options[:content_type_mappings] = { csv: Import::FILE_TYPES }
  140. end
  141. # In some places in the code, we rescue this exception, but we don't always
  142. # load the S3 library, so it may be an undefined constant:
  143. unless defined?(Seahorse)
  144. module Seahorse
  145. module Client
  146. class NetworkingError < StandardError; end
  147. end
  148. end
  149. end
  150. # Set our ImageMagick security policy, but allow admins to override it
  151. ENV['MAGICK_CONFIGURE_PATH'] = begin
  152. imagemagick_config_paths = ENV.fetch('MAGICK_CONFIGURE_PATH', '').split(File::PATH_SEPARATOR)
  153. imagemagick_config_paths << Rails.root.join('config', 'imagemagick').expand_path.to_s
  154. imagemagick_config_paths.join(File::PATH_SEPARATOR)
  155. end