2024-04-29 00:17:15 +02:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2024-04-29 16:53:21 +02:00
|
|
|
class ImageProcessorJob < ApplicationJob
|
|
|
|
class FileNotScannedYetError < StandardError
|
|
|
|
end
|
|
|
|
|
|
|
|
# If by the time the job runs the blob has been deleted, ignore the error
|
|
|
|
discard_on ActiveRecord::RecordNotFound
|
|
|
|
# If the file is deleted during the scan, ignore the error
|
|
|
|
discard_on ActiveStorage::FileNotFoundError
|
|
|
|
# If the file is not analyzed or scanned for viruses yet, retry later
|
|
|
|
# (to avoid modifying the file while it is being scanned).
|
|
|
|
retry_on FileNotScannedYetError, wait: :exponentially_longer, attempts: 10
|
|
|
|
|
2024-05-29 16:08:23 +02:00
|
|
|
rescue_from ActiveStorage::PreviewError do
|
|
|
|
retry_or_discard
|
|
|
|
end
|
|
|
|
|
2024-04-29 16:53:21 +02:00
|
|
|
def perform(blob)
|
|
|
|
return if blob.nil?
|
|
|
|
raise FileNotScannedYetError if blob.virus_scanner.pending?
|
2024-05-07 16:27:08 +02:00
|
|
|
return if ActiveStorage::Attachment.find_by(blob_id: blob.id)&.record_type == "ActiveStorage::VariantRecord"
|
2024-04-29 16:53:21 +02:00
|
|
|
|
|
|
|
auto_rotate(blob) if ["image/jpeg", "image/jpg"].include?(blob.content_type)
|
2024-07-04 17:16:52 +02:00
|
|
|
uninterlace(blob) if blob.content_type == "image/png"
|
2024-05-07 16:41:20 +02:00
|
|
|
create_representations(blob) if blob.representation_required?
|
2024-04-29 16:53:21 +02:00
|
|
|
add_watermark(blob) if blob.watermark_pending?
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def auto_rotate(blob)
|
|
|
|
blob.open do |file|
|
|
|
|
Tempfile.create(["rotated", File.extname(file)]) do |output|
|
|
|
|
processed = AutoRotateService.new.process(file, output)
|
|
|
|
return if processed.blank?
|
|
|
|
|
|
|
|
blob.upload(processed) # also update checksum & byte_size accordingly
|
|
|
|
blob.save!
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2024-07-04 17:16:52 +02:00
|
|
|
def uninterlace(blob)
|
|
|
|
blob.open do |file|
|
|
|
|
processed = UninterlaceService.new.process(file)
|
|
|
|
return if processed.blank?
|
|
|
|
|
|
|
|
blob.upload(processed)
|
|
|
|
blob.save!
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2024-05-07 16:41:20 +02:00
|
|
|
def create_representations(blob)
|
2024-04-29 16:53:21 +02:00
|
|
|
blob.attachments.each do |attachment|
|
|
|
|
next unless attachment&.representable?
|
|
|
|
attachment.representation(resize_to_limit: [400, 400]).processed
|
2024-05-24 09:58:19 +02:00
|
|
|
if attachment.blob.content_type.in?(RARE_IMAGE_TYPES)
|
|
|
|
attachment.variant(resize_to_limit: [2000, 2000]).processed
|
|
|
|
end
|
2024-08-29 17:30:23 +02:00
|
|
|
if attachment.record.class == ActionText::RichText
|
|
|
|
attachment.variant(resize_to_limit: [1024, 768]).processed
|
|
|
|
end
|
2024-04-29 16:53:21 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def add_watermark(blob)
|
|
|
|
return if blob.watermark_done?
|
|
|
|
|
|
|
|
blob.open do |file|
|
|
|
|
Tempfile.create(["watermarked", File.extname(file)]) do |output|
|
|
|
|
processed = WatermarkService.new.process(file, output)
|
|
|
|
return if processed.blank?
|
|
|
|
|
|
|
|
blob.upload(processed) # also update checksum & byte_size accordingly
|
|
|
|
blob.watermarked_at = Time.current
|
|
|
|
blob.save!
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2024-05-29 16:08:23 +02:00
|
|
|
|
|
|
|
def retry_or_discard
|
|
|
|
if executions < max_attempts
|
|
|
|
retry_job wait: 5.minutes
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def max_attempts
|
|
|
|
3
|
|
|
|
end
|
2024-04-29 16:53:21 +02:00
|
|
|
end
|