The code powering m.abunchtell.com https://m.abunchtell.com
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.
 
 
 
 

84 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 'aws-sdk-s3'
  14. s3_region = ENV.fetch('S3_REGION') { 'us-east-1' }
  15. s3_protocol = ENV.fetch('S3_PROTOCOL') { 'https' }
  16. s3_hostname = ENV.fetch('S3_HOSTNAME') { "s3-#{s3_region}.amazonaws.com" }
  17. Paperclip::Attachment.default_options.merge!(
  18. storage: :s3,
  19. s3_protocol: s3_protocol,
  20. s3_host_name: s3_hostname,
  21. s3_headers: {
  22. 'Cache-Control' => 'max-age=315576000',
  23. },
  24. s3_permissions: ENV.fetch('S3_PERMISSION') { 'public-read' },
  25. s3_region: s3_region,
  26. s3_credentials: {
  27. bucket: ENV['S3_BUCKET'],
  28. access_key_id: ENV['AWS_ACCESS_KEY_ID'],
  29. secret_access_key: ENV['AWS_SECRET_ACCESS_KEY'],
  30. },
  31. s3_options: {
  32. signature_version: ENV.fetch('S3_SIGNATURE_VERSION') { 'v4' },
  33. }
  34. )
  35. if ENV.has_key?('S3_ENDPOINT')
  36. Paperclip::Attachment.default_options[:s3_options].merge!(
  37. endpoint: ENV['S3_ENDPOINT'],
  38. force_path_style: true
  39. )
  40. Paperclip::Attachment.default_options[:url] = ':s3_path_url'
  41. end
  42. if ENV.has_key?('S3_ALIAS_HOST') || ENV.has_key?('S3_CLOUDFRONT_HOST')
  43. Paperclip::Attachment.default_options.merge!(
  44. url: ':s3_alias_url',
  45. s3_host_alias: ENV['S3_ALIAS_HOST'] || ENV['S3_CLOUDFRONT_HOST']
  46. )
  47. end
  48. elsif ENV['SWIFT_ENABLED'] == 'true'
  49. require 'fog/openstack'
  50. Paperclip::Attachment.default_options.merge!(
  51. fog_credentials: {
  52. provider: 'OpenStack',
  53. openstack_username: ENV['SWIFT_USERNAME'],
  54. openstack_project_id: ENV['SWIFT_PROJECT_ID'],
  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. Paperclip::Attachment.default_options.merge!(
  69. storage: :filesystem,
  70. use_timestamp: true,
  71. path: (ENV['PAPERCLIP_ROOT_PATH'] || ':rails_root/public/system') + '/:class/:attachment/:id_partition/:style/:filename',
  72. url: (ENV['PAPERCLIP_ROOT_URL'] || '/system') + '/:class/:attachment/:id_partition/:style/:filename',
  73. )
  74. end