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.

41 lines
1.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(&:empty?).join('.')
  6. end
  7. if ENV['S3_ENABLED'] == 'true'
  8. Aws.eager_autoload!(services: %w(S3))
  9. Paperclip::Attachment.default_options[:storage] = :s3
  10. Paperclip::Attachment.default_options[:s3_protocol] = ENV.fetch('S3_PROTOCOL') { 'https' }
  11. Paperclip::Attachment.default_options[:url] = ':s3_domain_url'
  12. Paperclip::Attachment.default_options[:s3_host_name] = ENV.fetch('S3_HOSTNAME') { "s3-#{ENV.fetch('S3_REGION')}.amazonaws.com" }
  13. Paperclip::Attachment.default_options[:path] = '/:class/:attachment/:id_partition/:style/:filename'
  14. Paperclip::Attachment.default_options[:s3_headers] = { 'Cache-Control' => 'max-age=315576000', 'Expires' => 10.years.from_now.httpdate }
  15. Paperclip::Attachment.default_options[:s3_permissions] = 'public-read'
  16. Paperclip::Attachment.default_options[:s3_region] = ENV.fetch('S3_REGION') { 'us-east-1' }
  17. Paperclip::Attachment.default_options[:s3_credentials] = {
  18. bucket: ENV.fetch('S3_BUCKET'),
  19. access_key_id: ENV.fetch('AWS_ACCESS_KEY_ID'),
  20. secret_access_key: ENV.fetch('AWS_SECRET_ACCESS_KEY'),
  21. }
  22. unless ENV['S3_ENDPOINT'].blank?
  23. Paperclip::Attachment.default_options[:s3_options] = {
  24. endpoint: ENV['S3_ENDPOINT'],
  25. force_path_style: true,
  26. }
  27. Paperclip::Attachment.default_options[:url] = ':s3_path_url'
  28. end
  29. unless ENV['S3_CLOUDFRONT_HOST'].blank?
  30. Paperclip::Attachment.default_options[:url] = ':s3_alias_url'
  31. Paperclip::Attachment.default_options[:s3_host_alias] = ENV['S3_CLOUDFRONT_HOST']
  32. end
  33. end