diff --git a/app/tasks/maintenance/create_previews_for_pjs_from_messagerie_task.rb b/app/tasks/maintenance/create_previews_for_pjs_from_messagerie_task.rb new file mode 100644 index 000000000..b75c530b9 --- /dev/null +++ b/app/tasks/maintenance/create_previews_for_pjs_from_messagerie_task.rb @@ -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 diff --git a/app/tasks/maintenance/create_variants_for_pjs_from_messagerie__task.rb b/app/tasks/maintenance/create_variants_for_pjs_from_messagerie__task.rb new file mode 100644 index 000000000..fa1eb30d2 --- /dev/null +++ b/app/tasks/maintenance/create_variants_for_pjs_from_messagerie__task.rb @@ -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 diff --git a/spec/tasks/maintenance/create_previews_for_pjs_from_messagerie_task_spec.rb b/spec/tasks/maintenance/create_previews_for_pjs_from_messagerie_task_spec.rb new file mode 100644 index 000000000..245c1971c --- /dev/null +++ b/spec/tasks/maintenance/create_previews_for_pjs_from_messagerie_task_spec.rb @@ -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 diff --git a/spec/tasks/maintenance/create_variants_for_pjs_from_messagerie__task_spec.rb b/spec/tasks/maintenance/create_variants_for_pjs_from_messagerie__task_spec.rb new file mode 100644 index 000000000..0428e6d0b --- /dev/null +++ b/spec/tasks/maintenance/create_variants_for_pjs_from_messagerie__task_spec.rb @@ -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