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.

66 lines
2.1 KiB

  1. # frozen_string_literal: true
  2. module Paperclip
  3. module AttachmentExtensions
  4. def meta
  5. instance_read(:meta)
  6. end
  7. # We overwrite this method to support delayed processing in
  8. # Sidekiq. Since we process the original file to reduce disk
  9. # usage, and we still want to generate thumbnails straight
  10. # away, it's the only style we need to exclude
  11. def process_style?(style_name, style_args)
  12. if style_name == :original && instance.respond_to?(:delay_processing_for_attachment?) && instance.delay_processing_for_attachment?(name)
  13. false
  14. else
  15. style_args.empty? || style_args.include?(style_name)
  16. end
  17. end
  18. def storage_schema_version
  19. instance_read(:storage_schema_version) || 0
  20. end
  21. def assign_attributes
  22. super
  23. instance_write(:storage_schema_version, 1)
  24. end
  25. def variant?(other_filename)
  26. return true if original_filename == other_filename
  27. return false if original_filename.nil?
  28. formats = styles.values.filter_map(&:format)
  29. return false if formats.empty?
  30. other_extension = File.extname(other_filename)
  31. formats.include?(other_extension.delete('.')) && File.basename(other_filename, other_extension) == File.basename(original_filename, File.extname(original_filename))
  32. end
  33. def default_url(style_name = default_style)
  34. @url_generator.for_as_default(style_name)
  35. end
  36. STOPLIGHT_THRESHOLD = 10
  37. STOPLIGHT_COOLDOWN = 30
  38. # We overwrite this method to put a circuit breaker around
  39. # calls to object storage, to stop hitting APIs that are slow
  40. # to respond or don't respond at all and as such minimize the
  41. # impact of object storage outages on application throughput
  42. def save
  43. Stoplight('object-storage') { super }.with_threshold(STOPLIGHT_THRESHOLD).with_cool_off_time(STOPLIGHT_COOLDOWN).with_error_handler do |error, handle|
  44. if error.is_a?(Seahorse::Client::NetworkingError)
  45. handle.call(error)
  46. else
  47. raise error
  48. end
  49. end.run
  50. end
  51. end
  52. end
  53. Paperclip::Attachment.prepend(Paperclip::AttachmentExtensions)