Skip to content

Commit

Permalink
task(pj): create representations for latest pieces jointes
Browse files Browse the repository at this point in the history
  • Loading branch information
E-L-T committed Jun 12, 2024
1 parent d39167d commit 12444e9
Show file tree
Hide file tree
Showing 4 changed files with 149 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# frozen_string_literal: true

module Maintenance
class CreatePreviewsForPjOfLatestDossiersTask < MaintenanceTasks::Task
def collection
dossier_ids = Dossier

Check warning on line 6 in app/tasks/maintenance/create_previews_for_pj_of_latest_dossiers_task.rb

View check run for this annotation

Codecov / codecov/patch

app/tasks/maintenance/create_previews_for_pj_of_latest_dossiers_task.rb#L6

Added line #L6 was not covered by tests
.state_en_construction_ou_instruction
.where('depose_at > ?', 3.months.ago)
.pluck(:id)

champ_ids = Champ

Check warning on line 11 in app/tasks/maintenance/create_previews_for_pj_of_latest_dossiers_task.rb

View check run for this annotation

Codecov / codecov/patch

app/tasks/maintenance/create_previews_for_pj_of_latest_dossiers_task.rb#L11

Added line #L11 was not covered by tests
.where(dossier_id: dossier_ids)
.where(type: ["Champs::PieceJustificativeChamp", 'Champs::TitreIdentiteChamp'])
.pluck(:id)

ActiveStorage::Attachment

Check warning on line 16 in app/tasks/maintenance/create_previews_for_pj_of_latest_dossiers_task.rb

View check run for this annotation

Codecov / codecov/patch

app/tasks/maintenance/create_previews_for_pj_of_latest_dossiers_task.rb#L16

Added line #L16 was not covered by tests
.where(record_id: champ_ids)
end

def process(attachment)
return unless attachment.previewable?
attachment.preview(resize_to_limit: [400, 400]).processed unless attachment.preview(resize_to_limit: [400, 400]).image.attached?
rescue MiniMagick::Error
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# frozen_string_literal: true

module Maintenance
class CreateVariantsForPjOfLatestDossiersTask < MaintenanceTasks::Task
def collection
dossier_ids = Dossier

Check warning on line 6 in app/tasks/maintenance/create_variants_for_pj_of_latest_dossiers_task.rb

View check run for this annotation

Codecov / codecov/patch

app/tasks/maintenance/create_variants_for_pj_of_latest_dossiers_task.rb#L6

Added line #L6 was not covered by tests
.state_en_construction_ou_instruction
.where('depose_at > ?', 3.months.ago)
.pluck(:id)

champ_ids = Champ

Check warning on line 11 in app/tasks/maintenance/create_variants_for_pj_of_latest_dossiers_task.rb

View check run for this annotation

Codecov / codecov/patch

app/tasks/maintenance/create_variants_for_pj_of_latest_dossiers_task.rb#L11

Added line #L11 was not covered by tests
.where(dossier_id: dossier_ids)
.where(type: ["Champs::PieceJustificativeChamp", 'Champs::TitreIdentiteChamp'])
.pluck(:id)

ActiveStorage::Attachment

Check warning on line 16 in app/tasks/maintenance/create_variants_for_pj_of_latest_dossiers_task.rb

View check run for this annotation

Codecov / codecov/patch

app/tasks/maintenance/create_variants_for_pj_of_latest_dossiers_task.rb#L16

Added line #L16 was not covered by tests
.where(record_id: champ_ids)
end

def process(attachment)
return unless attachment.variable?
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
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# frozen_string_literal: true

require "rails_helper"

module Maintenance
RSpec.describe CreatePreviewsForPjOfLatestDossiersTask do
describe "#process" do
let(:procedure) { create(:procedure_with_dossiers) }
let(:type_de_champ_pj) { create(:type_de_champ_piece_justificative, stable_id: 3, libelle: 'Justificatif de domicile', procedure:) }
let(:champ_pj) { create(:champ_piece_justificative, type_de_champ: type_de_champ_pj) }
let(:blob_info) do
{
filename: file.original_filename,
byte_size: file.size,
checksum: Digest::SHA256.file(file.path),
content_type: file.content_type,
# we don't want to run virus scanner on this file
metadata: { virus_scan_result: ActiveStorage::VirusScanner::SAFE }
}
end
let(:blob) do
blob = ActiveStorage::Blob.create_before_direct_upload!(**blob_info)
blob.upload(file)
blob
end

let(:attachment) { ActiveStorage::Attachment.create(name: "test", blob: blob, record: champ_pj) }
subject(:process) { described_class.process(attachment) }

context "when pj is a pdf" do
let(:file) { fixture_file_upload('spec/fixtures/files/RIB.pdf', 'application/pdf') }

it "creates a preview" do
expect(attachment.preview(resize_to_limit: [400, 400]).image.attached?).to be false
subject
expect(attachment.preview(resize_to_limit: [400, 400]).image.attached?).to be true
end
end
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# frozen_string_literal: true

require "rails_helper"

module Maintenance
RSpec.describe CreateVariantsForPjOfLatestDossiersTask do
describe "#process" do
let(:procedure) { create(:procedure_with_dossiers) }
let(:type_de_champ_pj) { create(:type_de_champ_piece_justificative, stable_id: 3, libelle: 'Justificatif de domicile', procedure:) }
let(:champ_pj) { create(:champ_piece_justificative, type_de_champ: type_de_champ_pj) }
let(:blob_info) do
{
filename: file.original_filename,
byte_size: file.size,
checksum: Digest::SHA256.file(file.path),
content_type: file.content_type,
# we don't want to run virus scanner on this file
metadata: { virus_scan_result: ActiveStorage::VirusScanner::SAFE }
}
end
let(:blob) do
blob = ActiveStorage::Blob.create_before_direct_upload!(**blob_info)
blob.upload(file)
blob
end

let(:attachment) { ActiveStorage::Attachment.create(name: "test", blob: blob, record: champ_pj) }
subject(:process) { described_class.process(attachment) }

context "when pj is a classical format image" do
let(:file) { fixture_file_upload('spec/fixtures/files/logo_test_procedure.png', 'image/png') }

it "creates a variant" do
expect(attachment.variant(resize_to_limit: [400, 400]).key).to be_nil
expect { subject }.to change { ActiveStorage::VariantRecord.count }.by(1)
expect(attachment.variant(resize_to_limit: [400, 400]).key).not_to be_nil
expect(attachment.variant(resize_to_limit: [2000, 2000]).key).to be_nil
end
end

context "when pj is a rare format image" do
let(:file) { fixture_file_upload('spec/fixtures/files/pencil.tiff', 'image/tiff') }

it "creates a variant" do
expect(attachment.variant(resize_to_limit: [400, 400]).key).to be_nil
expect { subject }.to change { ActiveStorage::VariantRecord.count }.by(2)
expect(attachment.variant(resize_to_limit: [400, 400]).key).not_to be_nil
expect(attachment.variant(resize_to_limit: [2000, 2000]).key).not_to be_nil
end
end
end
end
end

0 comments on commit 12444e9

Please sign in to comment.