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.

82 lines
2.8 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 'fog/aws'
  14. s3_protocol = ENV.fetch('S3_PROTOCOL') { 'https' }
  15. s3_hostname = ENV.fetch('S3_HOSTNAME') { "s3-#{ENV['S3_REGION']}.amazonaws.com" }
  16. aws_signature_version = ENV['S3_SIGNATURE_VERSION'] == 's3' ? 2 : ENV['S3_SIGNATURE_VERSION'].to_i
  17. aws_signature_version = 4 if aws_signature_version.zero?
  18. Paperclip::Attachment.default_options.merge!(
  19. fog_credentials: {
  20. provider: 'AWS',
  21. aws_access_key_id: ENV['AWS_ACCESS_KEY_ID'],
  22. aws_secret_access_key: ENV['AWS_SECRET_ACCESS_KEY'],
  23. aws_signature_version: aws_signature_version,
  24. region: ENV.fetch('S3_REGION') { 'us-east-1' },
  25. scheme: s3_protocol,
  26. host: s3_hostname
  27. },
  28. fog_directory: ENV['S3_BUCKET'],
  29. fog_options: {
  30. acl: ENV.fetch('S3_PERMISSION') { 'public-read' },
  31. cache_control: 'max-age=315576000',
  32. }
  33. )
  34. if ENV.has_key?('S3_ENDPOINT')
  35. Paperclip::Attachment.default_options[:fog_credentials].merge!(
  36. endpoint: ENV['S3_ENDPOINT'],
  37. path_style: true
  38. )
  39. Paperclip::Attachment.default_options[:fog_host] = "#{s3_protocol}://#{s3_hostname}/#{ENV['S3_BUCKET']}"
  40. end
  41. if ENV.has_key?('S3_CLOUDFRONT_HOST')
  42. Paperclip::Attachment.default_options[:fog_host] = "#{s3_protocol}://#{ENV['S3_CLOUDFRONT_HOST']}"
  43. end
  44. elsif ENV['SWIFT_ENABLED'] == 'true'
  45. require 'fog/openstack'
  46. Paperclip::Attachment.default_options.merge!(
  47. fog_credentials: {
  48. provider: 'OpenStack',
  49. openstack_username: ENV['SWIFT_USERNAME'],
  50. openstack_project_name: ENV['SWIFT_TENANT'],
  51. openstack_tenant: ENV['SWIFT_TENANT'], # Some OpenStack-v2 ignores project_name but needs tenant
  52. openstack_api_key: ENV['SWIFT_PASSWORD'],
  53. openstack_auth_url: ENV['SWIFT_AUTH_URL'],
  54. openstack_domain_name: ENV.fetch('SWIFT_DOMAIN_NAME') { 'default' },
  55. openstack_region: ENV['SWIFT_REGION'],
  56. openstack_cache_ttl: ENV.fetch('SWIFT_CACHE_TTL') { 60 },
  57. },
  58. fog_directory: ENV['SWIFT_CONTAINER'],
  59. fog_host: ENV['SWIFT_OBJECT_URL'],
  60. fog_public: true
  61. )
  62. else
  63. require 'fog/local'
  64. Paperclip::Attachment.default_options.merge!(
  65. fog_credentials: {
  66. provider: 'Local',
  67. local_root: ENV.fetch('PAPERCLIP_ROOT_PATH') { Rails.root.join('public', 'system') },
  68. },
  69. fog_directory: '',
  70. fog_host: ENV.fetch('PAPERCLIP_ROOT_URL') { '/system' }
  71. )
  72. end