1
0

paperclip.rb 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. # frozen_string_literal: true
  2. Paperclip::DataUriAdapter.register
  3. Paperclip::ResponseWithLimitAdapter.register
  4. PATH = ':prefix_url:class/:attachment/:id_partition/:style/:filename'
  5. Paperclip.interpolates :filename do |attachment, style|
  6. if style == :original
  7. attachment.original_filename
  8. else
  9. [basename(attachment, style), extension(attachment, style)].compact_blank!.join('.')
  10. end
  11. end
  12. Paperclip.interpolates :prefix_path do |attachment, _style|
  13. if attachment.storage_schema_version >= 1 && attachment.instance.respond_to?(:local?) && !attachment.instance.local?
  14. "cache#{File::SEPARATOR}"
  15. else
  16. ''
  17. end
  18. end
  19. Paperclip.interpolates :prefix_url do |attachment, _style|
  20. if attachment.storage_schema_version >= 1 && attachment.instance.respond_to?(:local?) && !attachment.instance.local?
  21. 'cache/'
  22. else
  23. ''
  24. end
  25. end
  26. Paperclip::Attachment.default_options.merge!(
  27. use_timestamp: false,
  28. path: PATH,
  29. storage: :fog
  30. )
  31. if ENV['S3_ENABLED'] == 'true'
  32. require 'aws-sdk-s3'
  33. s3_region = ENV.fetch('S3_REGION') { 'us-east-1' }
  34. s3_protocol = ENV.fetch('S3_PROTOCOL') { 'https' }
  35. s3_hostname = ENV.fetch('S3_HOSTNAME') { "s3-#{s3_region}.amazonaws.com" }
  36. Paperclip::Attachment.default_options[:path] = ENV.fetch('S3_KEY_PREFIX') + "/#{PATH}" if ENV.has_key?('S3_KEY_PREFIX')
  37. Paperclip::Attachment.default_options.merge!(
  38. storage: :s3,
  39. s3_protocol: s3_protocol,
  40. s3_host_name: s3_hostname,
  41. s3_headers: {
  42. 'X-Amz-Multipart-Threshold' => ENV.fetch('S3_MULTIPART_THRESHOLD') { 15.megabytes }.to_i,
  43. 'Cache-Control' => 'public, max-age=315576000, immutable',
  44. },
  45. s3_permissions: ENV.fetch('S3_PERMISSION') { 'public-read' },
  46. s3_region: s3_region,
  47. s3_credentials: {
  48. bucket: ENV['S3_BUCKET'],
  49. access_key_id: ENV['AWS_ACCESS_KEY_ID'],
  50. secret_access_key: ENV['AWS_SECRET_ACCESS_KEY'],
  51. },
  52. s3_options: {
  53. signature_version: ENV.fetch('S3_SIGNATURE_VERSION') { 'v4' },
  54. http_open_timeout: ENV.fetch('S3_OPEN_TIMEOUT') { '5' }.to_i,
  55. http_read_timeout: ENV.fetch('S3_READ_TIMEOUT') { '5' }.to_i,
  56. http_idle_timeout: 5,
  57. retry_limit: ENV.fetch('S3_RETRY_LIMIT') { '0' }.to_i,
  58. }
  59. )
  60. Paperclip::Attachment.default_options[:s3_permissions] = ->(*) {} if ENV['S3_PERMISSION'] == ''
  61. if ENV.has_key?('S3_ENDPOINT')
  62. Paperclip::Attachment.default_options[:s3_options].merge!(
  63. endpoint: ENV['S3_ENDPOINT'],
  64. force_path_style: ENV['S3_OVERRIDE_PATH_STYLE'] != 'true'
  65. )
  66. Paperclip::Attachment.default_options[:url] = ':s3_path_url'
  67. end
  68. if ENV.has_key?('S3_ALIAS_HOST') || ENV.has_key?('S3_CLOUDFRONT_HOST')
  69. Paperclip::Attachment.default_options.merge!(
  70. url: ':s3_alias_url',
  71. s3_host_alias: ENV['S3_ALIAS_HOST'] || ENV['S3_CLOUDFRONT_HOST']
  72. )
  73. end
  74. Paperclip::Attachment.default_options[:s3_headers]['X-Amz-Storage-Class'] = ENV['S3_STORAGE_CLASS'] if ENV.has_key?('S3_STORAGE_CLASS')
  75. # Some S3-compatible providers might not actually be compatible with some APIs
  76. # used by kt-paperclip, see https://github.com/mastodon/mastodon/issues/16822
  77. # and https://github.com/mastodon/mastodon/issues/26394
  78. module Paperclip
  79. module Storage
  80. module S3Extensions
  81. def copy_to_local_file(style, local_dest_path)
  82. log("copying #{path(style)} to local file #{local_dest_path}")
  83. options = {}
  84. options[:mode] = 'single_request' if ENV['S3_FORCE_SINGLE_REQUEST'] == 'true'
  85. options[:checksum_mode] = 'DISABLED' unless ENV['S3_ENABLE_CHECKSUM_MODE'] == 'true'
  86. s3_object(style).download_file(local_dest_path, options)
  87. rescue Aws::Errors::ServiceError => e
  88. warn("#{e} - cannot copy #{path(style)} to local file #{local_dest_path}")
  89. false
  90. end
  91. end
  92. end
  93. end
  94. Paperclip::Storage::S3.prepend(Paperclip::Storage::S3Extensions)
  95. elsif ENV['SWIFT_ENABLED'] == 'true'
  96. require 'fog/openstack'
  97. Paperclip::Attachment.default_options.merge!(
  98. fog_credentials: {
  99. provider: 'OpenStack',
  100. openstack_username: ENV['SWIFT_USERNAME'],
  101. openstack_project_id: ENV['SWIFT_PROJECT_ID'],
  102. openstack_project_name: ENV['SWIFT_TENANT'],
  103. openstack_tenant: ENV['SWIFT_TENANT'], # Some OpenStack-v2 ignores project_name but needs tenant
  104. openstack_api_key: ENV['SWIFT_PASSWORD'],
  105. openstack_auth_url: ENV['SWIFT_AUTH_URL'],
  106. openstack_domain_name: ENV.fetch('SWIFT_DOMAIN_NAME') { 'default' },
  107. openstack_region: ENV['SWIFT_REGION'],
  108. openstack_cache_ttl: ENV.fetch('SWIFT_CACHE_TTL') { 60 },
  109. openstack_temp_url_key: ENV['SWIFT_TEMP_URL_KEY'],
  110. },
  111. fog_file: { 'Cache-Control' => 'public, max-age=315576000, immutable' },
  112. fog_directory: ENV['SWIFT_CONTAINER'],
  113. fog_host: ENV['SWIFT_OBJECT_URL'],
  114. fog_public: true
  115. )
  116. elsif ENV['AZURE_ENABLED'] == 'true'
  117. require 'paperclip-azure'
  118. Paperclip::Attachment.default_options.merge!(
  119. storage: :azure,
  120. azure_options: {
  121. protocol: 'https',
  122. },
  123. azure_credentials: {
  124. storage_account_name: ENV['AZURE_STORAGE_ACCOUNT'],
  125. storage_access_key: ENV['AZURE_STORAGE_ACCESS_KEY'],
  126. container: ENV['AZURE_CONTAINER_NAME'],
  127. }
  128. )
  129. if ENV.has_key?('AZURE_ALIAS_HOST')
  130. Paperclip::Attachment.default_options.merge!(
  131. url: ':azure_alias_url',
  132. azure_host_alias: ENV['AZURE_ALIAS_HOST']
  133. )
  134. end
  135. else
  136. Rails.configuration.x.file_storage_root_path = ENV.fetch('PAPERCLIP_ROOT_PATH', File.join(':rails_root', 'public', 'system'))
  137. Paperclip::Attachment.default_options.merge!(
  138. storage: :filesystem,
  139. path: File.join(Rails.configuration.x.file_storage_root_path, ':prefix_path:class', ':attachment', ':id_partition', ':style', ':filename'),
  140. url: ENV.fetch('PAPERCLIP_ROOT_URL', '/system') + "/#{PATH}"
  141. )
  142. end
  143. Rails.application.reloader.to_prepare do
  144. Paperclip.options[:content_type_mappings] = { csv: Import::FILE_TYPES }
  145. end
  146. # In some places in the code, we rescue this exception, but we don't always
  147. # load the S3 library, so it may be an undefined constant:
  148. unless defined?(Seahorse)
  149. module Seahorse
  150. module Client
  151. class NetworkingError < StandardError; end
  152. end
  153. end
  154. end
  155. # Set our ImageMagick security policy, but allow admins to override it
  156. ENV['MAGICK_CONFIGURE_PATH'] = begin
  157. imagemagick_config_paths = ENV.fetch('MAGICK_CONFIGURE_PATH', '').split(File::PATH_SEPARATOR)
  158. imagemagick_config_paths << Rails.root.join('config', 'imagemagick').expand_path.to_s
  159. imagemagick_config_paths.join(File::PATH_SEPARATOR)
  160. end