闭社主体 forked from https://github.com/tootsuite/mastodon
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.

61 lines
2.7 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. 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. elsif ENV['SWIFT_ENABLED'] == 'true'
  36. Paperclip::Attachment.default_options.merge!(
  37. path: ':class/:attachment/:id_partition/:style/:filename',
  38. storage: :fog,
  39. fog_credentials: {
  40. provider: 'OpenStack',
  41. openstack_username: ENV.fetch('SWIFT_USERNAME'),
  42. openstack_tenant: ENV.fetch('SWIFT_TENANT'),
  43. openstack_api_key: ENV.fetch('SWIFT_PASSWORD'),
  44. openstack_auth_url: ENV.fetch('SWIFT_AUTH_URL'),
  45. },
  46. fog_directory: ENV.fetch('SWIFT_CONTAINER'),
  47. fog_host: ENV.fetch('SWIFT_OBJECT_URL'),
  48. fog_public: true
  49. )
  50. else
  51. Paperclip::Attachment.default_options[:path] = (ENV['PAPERCLIP_ROOT_PATH'] || ':rails_root/public/system') + '/:class/:attachment/:id_partition/:style/:filename'
  52. Paperclip::Attachment.default_options[:url] = (ENV['PAPERCLIP_ROOT_URL'] || '/system') + '/:class/:attachment/:id_partition/:style/:filename'
  53. end