demarches-normaliennes/app/models/concerns/blob_titre_identite_watermark_concern.rb

39 lines
1.1 KiB
Ruby
Raw Normal View History

# Request a watermark on blobs attached to a `Champs::TitreIdentiteChamp`
# after the virus scan has run.
#
# We're using a class extension here, but we could as well have a periodic
# job that watermarks relevant attachments.
#
# The `after_commit` hook is triggered, among other cases, when
# the analyzer or virus scan updates the blob metadata. When both the analyzer
# and the virus scan have run, it is now safe to start the watermarking,
# without risking to replace the picture while it is being scanned in a
# concurrent job.
2020-11-17 16:34:24 +01:00
module BlobTitreIdentiteWatermarkConcern
extend ActiveSupport::Concern
included do
after_commit :enqueue_watermark_job
2020-11-17 16:34:24 +01:00
end
def watermark_pending?
watermark_required? && !watermark_done?
end
2020-11-17 16:34:24 +01:00
private
def watermark_required?
attachments.any? { |attachment| attachment.record.class.name == 'Champs::TitreIdentiteChamp' }
2020-11-17 16:34:24 +01:00
end
def watermark_done?
2020-11-17 16:34:24 +01:00
metadata[:watermark]
end
def enqueue_watermark_job
if analyzed? && virus_scanner.done? && watermark_pending?
2020-11-17 16:34:24 +01:00
TitreIdentiteWatermarkJob.perform_later(self)
end
end
end