db(maintenance task): create representations for pjs sent by messagerie

This commit is contained in:
Eric Leroy-Terquem 2024-07-10 23:28:08 +02:00
parent 3bbcda9c04
commit 5fbe2daca2
No known key found for this signature in database
GPG key ID: 53D8FAECEF207605
4 changed files with 166 additions and 0 deletions

View file

@ -0,0 +1,35 @@
# frozen_string_literal: true
module Maintenance
class CreatePreviewsForPjsFromMessagerieTask < MaintenanceTasks::Task
attribute :start_text, :string
validates :start_text, presence: true
attribute :end_text, :string
validates :end_text, presence: true
def collection
start_date = DateTime.parse(start_text)
end_date = DateTime.parse(end_text)
Dossier
.state_en_construction_ou_instruction
.where(depose_at: start_date..end_date)
end
def process(dossier)
commentaire_ids = Commentaire
.where(dossier_id: dossier)
.pluck(:id)
attachments = ActiveStorage::Attachment
.where(record_id: commentaire_ids)
attachments.each do |attachment|
next if !(attachment.previewable? && attachment.representation_required?)
attachment.preview(resize_to_limit: [400, 400]).processed unless attachment.preview(resize_to_limit: [400, 400]).image.attached?
rescue MiniMagick::Error, ActiveStorage::Error
end
end
end
end

View file

@ -0,0 +1,38 @@
# frozen_string_literal: true
module Maintenance
class CreateVariantsForPjsFromMessagerieTask < MaintenanceTasks::Task
attribute :start_text, :string
validates :start_text, presence: true
attribute :end_text, :string
validates :end_text, presence: true
def collection
start_date = DateTime.parse(start_text)
end_date = DateTime.parse(end_text)
Dossier
.state_en_construction_ou_instruction
.where(depose_at: start_date..end_date)
end
def process(dossier)
commentaire_ids = Commentaire
.where(dossier_id: dossier)
.pluck(:id)
attachments = ActiveStorage::Attachment
.where(record_id: commentaire_ids)
attachments.each do |attachment|
next if !(attachment.variable? && attachment.representation_required?)
attachment.variant(resize_to_limit: [400, 400]).processed if attachment.variant(resize_to_limit: [400, 400]).key.nil?
if attachment.blob.content_type.in?(RARE_IMAGE_TYPES) && attachment.variant(resize_to_limit: [2000, 2000]).key.nil?
attachment.variant(resize_to_limit: [2000, 2000]).processed
end
rescue MiniMagick::Error, ActiveStorage::Error
end
end
end
end

View file

@ -0,0 +1,39 @@
# frozen_string_literal: true
require "rails_helper"
module Maintenance
RSpec.describe CreatePreviewsForPjsFromMessagerieTask do
describe "#process" do
let(:procedure) { create(:procedure_with_dossiers) }
let(:dossier) { procedure.dossiers.first }
let(:commentaire) { create(:commentaire, dossier: dossier) }
before do
commentaire.piece_jointe.attach(
io: File.open(file_path),
filename: file_name,
content_type: content_type,
metadata: { virus_scan_result: ActiveStorage::VirusScanner::SAFE }
)
dossier.update(
depose_at: Date.new(2024, 05, 23),
state: "en_construction"
)
end
subject(:process) { described_class.process(dossier) }
context "when pj is a pdf" do
let(:file_path) { 'spec/fixtures/files/RIB.pdf' }
let(:file_name) { 'RIB.pdf' }
let(:content_type) { 'application/pdf' }
it "creates a preview" do
expect(commentaire.piece_jointe.first.preview(resize_to_limit: [400, 400]).image.attached?).to be false
expect { subject }.to change { commentaire.piece_jointe.first.reload.preview(resize_to_limit: [400, 400]).image.attached? }
end
end
end
end
end

View file

@ -0,0 +1,54 @@
# frozen_string_literal: true
require "rails_helper"
module Maintenance
RSpec.describe CreateVariantsForPjsFromMessagerieTask do
describe "#process" do
let(:procedure) { create(:procedure_with_dossiers) }
let(:dossier) { procedure.dossiers.first }
let(:commentaire) { create(:commentaire, dossier: dossier) }
before do
commentaire.piece_jointe.attach(
io: File.open(file_path),
filename: file_name,
content_type: content_type,
metadata: { virus_scan_result: ActiveStorage::VirusScanner::SAFE }
)
dossier.update(
depose_at: Date.new(2024, 05, 23),
state: "en_construction"
)
end
subject(:process) { described_class.process(dossier) }
context "when pj is a classical format image" do
let(:file_path) { 'spec/fixtures/files/logo_test_procedure.png' }
let(:file_name) { 'logo_test_procedure.png' }
let(:content_type) { 'image/png' }
it "creates a variant" do
expect(commentaire.piece_jointe.first.variant(resize_to_limit: [400, 400]).key).to be_nil
expect { subject }.to change { ActiveStorage::VariantRecord.count }.by(1)
expect(commentaire.piece_jointe.first.variant(resize_to_limit: [400, 400]).key).not_to be_nil
expect(commentaire.piece_jointe.first.variant(resize_to_limit: [2000, 2000]).key).to be_nil
end
end
context "when pj is a rare format image" do
let(:file_path) { 'spec/fixtures/files/pencil.tiff' }
let(:file_name) { 'pencil.tiff' }
let(:content_type) { 'image/tiff' }
it "creates a variant" do
expect(commentaire.piece_jointe.first.variant(resize_to_limit: [400, 400]).key).to be_nil
expect { subject }.to change { ActiveStorage::VariantRecord.count }.by(2)
expect(commentaire.piece_jointe.first.variant(resize_to_limit: [400, 400]).key).not_to be_nil
expect(commentaire.piece_jointe.first.variant(resize_to_limit: [2000, 2000]).key).not_to be_nil
end
end
end
end
end