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.
 
 
 
 

98 lines
3.1 KiB

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