paperclip.rb 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  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. 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' }
  15. Paperclip::Attachment.default_options[:s3_permissions] = ENV.fetch('S3_PERMISSION') { 'public-read' }
  16. Paperclip::Attachment.default_options[:s3_region] = ENV.fetch('S3_REGION') { 'us-east-1' }
  17. Paperclip::Attachment.default_options[:use_timestamp] = false
  18. Paperclip::Attachment.default_options[:s3_credentials] = {
  19. bucket: ENV.fetch('S3_BUCKET'),
  20. access_key_id: ENV.fetch('AWS_ACCESS_KEY_ID'),
  21. secret_access_key: ENV.fetch('AWS_SECRET_ACCESS_KEY'),
  22. }
  23. unless ENV['S3_ENDPOINT'].blank?
  24. Paperclip::Attachment.default_options[:s3_options] = {
  25. endpoint: ENV['S3_ENDPOINT'],
  26. signature_version: ENV['S3_SIGNATURE_VERSION'] || 'v4',
  27. force_path_style: true,
  28. }
  29. Paperclip::Attachment.default_options[:url] = ':s3_path_url'
  30. end
  31. unless ENV['S3_CLOUDFRONT_HOST'].blank?
  32. Paperclip::Attachment.default_options[:url] = ':s3_alias_url'
  33. Paperclip::Attachment.default_options[:s3_host_alias] = ENV['S3_CLOUDFRONT_HOST']
  34. end
  35. else
  36. Paperclip::Attachment.default_options[:path] = (ENV['PAPERCLIP_ROOT_PATH'] || ':rails_root/public/system') + '/:class/:attachment/:id_partition/:style/:filename'
  37. Paperclip::Attachment.default_options[:url] = (ENV['PAPERCLIP_ROOT_URL'] || '/system') + '/:class/:attachment/:id_partition/:style/:filename'
  38. end