2021-03-04 11:50:22 +00:00
|
|
|
# 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
|
2021-03-04 11:50:22 +00:00
|
|
|
after_commit :enqueue_watermark_job
|
2020-11-17 16:34:24 +01:00
|
|
|
end
|
|
|
|
|
2020-12-10 12:50:40 +01:00
|
|
|
def watermark_pending?
|
|
|
|
watermark_required? && !watermark_done?
|
|
|
|
end
|
|
|
|
|
2020-11-17 16:34:24 +01:00
|
|
|
private
|
|
|
|
|
2020-12-10 12:50:40 +01:00
|
|
|
def watermark_required?
|
2021-03-04 11:50:22 +00:00
|
|
|
attachments.any? { |attachment| attachment.record.class.name == 'Champs::TitreIdentiteChamp' }
|
2020-11-17 16:34:24 +01:00
|
|
|
end
|
|
|
|
|
2020-12-10 12:50:40 +01:00
|
|
|
def watermark_done?
|
2020-11-17 16:34:24 +01:00
|
|
|
metadata[:watermark]
|
|
|
|
end
|
|
|
|
|
|
|
|
def enqueue_watermark_job
|
2020-12-10 12:50:40 +01:00
|
|
|
if analyzed? && virus_scanner.done? && watermark_pending?
|
2020-11-17 16:34:24 +01:00
|
|
|
TitreIdentiteWatermarkJob.perform_later(self)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|