paperclip.rb 3.0 KB

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