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.

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