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.

62 lines
2.0 KiB

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