add PDF preview in view for gallery

This commit is contained in:
Lisa Durand 2024-05-07 16:41:20 +02:00 committed by Eric Leroy-Terquem
parent 93c3761107
commit affb1820d8
No known key found for this signature in database
GPG key ID: ECE60B4C1FA2ABB3
9 changed files with 13 additions and 18 deletions

Binary file not shown.

Before

Width:  |  Height:  |  Size: 5.3 KiB

After

Width:  |  Height:  |  Size: 5.1 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 3.5 KiB

View file

@ -16,7 +16,7 @@ class ImageProcessorJob < ApplicationJob
return if ActiveStorage::Attachment.find_by(blob_id: blob.id)&.record_type == "ActiveStorage::VariantRecord"
auto_rotate(blob) if ["image/jpeg", "image/jpg"].include?(blob.content_type)
create_variants(blob) if blob.variant_required?
create_representations(blob) if blob.representation_required?
add_watermark(blob) if blob.watermark_pending?
end
@ -34,10 +34,9 @@ class ImageProcessorJob < ApplicationJob
end
end
def create_variants(blob)
def create_representations(blob)
blob.attachments.each do |attachment|
next unless attachment&.representable?
attachment.representation(resize_to_limit: [300, 300]).processed
attachment.representation(resize_to_limit: [400, 400]).processed
end
end

View file

@ -1,9 +1,7 @@
class Champs::PieceJustificativeChamp < Champ
FILE_MAX_SIZE = 200.megabytes
has_many_attached :piece_justificative_file do |attachable|
attachable.variant :medium, resize: '400x400'
end
has_many_attached :piece_justificative_file
# TODO: if: -> { validate_champ_value? || validation_context == :prefill }
validates :piece_justificative_file,

View file

@ -2,9 +2,7 @@ class Champs::TitreIdentiteChamp < Champ
FILE_MAX_SIZE = 20.megabytes
ACCEPTED_FORMATS = ['image/png', 'image/jpeg']
has_many_attached :piece_justificative_file do |attachable|
attachable.variant :medium, resize: '400x400'
end
has_many_attached :piece_justificative_file
# TODO: if: -> { validate_champ_value? || validation_context == :prefill }
validates :piece_justificative_file, content_type: ACCEPTED_FORMATS, size: { less_than: FILE_MAX_SIZE }

View file

@ -7,7 +7,7 @@ module BlobImageProcessorConcern
watermarked_at.present?
end
def variant_required?
def representation_required?
attachments.any? { _1.record.class == Champs::TitreIdentiteChamp || _1.record.class == Champs::PieceJustificativeChamp }
end

View file

@ -5,13 +5,13 @@
.fr-container
.gallery.gallery-pieces-jointes{ "data-controller": "lightbox" }
- @champs_with_pieces_jointes.each do |champ|
- champ.piece_justificative_file.each do |attachment|
- champ.piece_justificative_file.with_all_variant_records.each do |attachment|
.gallery-item
- blob = attachment.blob
- if blob.content_type.in?(AUTHORIZED_PDF_TYPES)
= link_to blob.url, id: blob.id, data: { iframe: true, src: blob.url }, class: 'gallery-link', type: blob.content_type, title: "#{champ.libelle} -- #{blob.filename}" do
.thumbnail
= image_tag("pdf-placeholder.png")
= image_tag(attachment.representation(resize_to_limit: [400, 400]).processed.url, loading: :lazy)
.fr-btn.fr-btn--tertiary.fr-btn--icon-left.fr-icon-eye{ role: :button }
Visualiser
.champ-libelle
@ -21,7 +21,7 @@
- elsif blob.content_type.in?(AUTHORIZED_IMAGE_TYPES)
= link_to image_url(blob.url), title: "#{champ.libelle} -- #{blob.filename}", data: { src: blob.url }, class: 'gallery-link' do
.thumbnail
= image_tag(attachment.variant(:medium), loading: :lazy)
= image_tag(attachment.representation(resize_to_limit: [400, 400]).processed.url, loading: :lazy)
.fr-btn.fr-btn--tertiary.fr-btn--icon-left.fr-icon-eye{ role: :button }
Visualiser
.champ-libelle

View file

@ -5,20 +5,20 @@
%li= render Attachment::ShowComponent.new(attachment:, new_tab: true)
- else
.gallery-items-list
- champ.piece_justificative_file.attachments.each do |attachment|
- champ.piece_justificative_file.attachments.with_all_variant_records.each do |attachment|
.gallery-item
- blob = attachment.blob
- if blob.content_type.in?(AUTHORIZED_PDF_TYPES)
= link_to blob.url, id: blob.id, data: { iframe: true, src: blob.url }, class: 'gallery-link', type: blob.content_type, title: "#{champ.libelle} -- #{blob.filename}" do
.thumbnail
= image_tag("pdf-placeholder.png")
= image_tag(attachment.representation(resize_to_limit: [400, 400]).processed.url, loading: :lazy)
.fr-btn.fr-btn--tertiary.fr-btn--icon-left.fr-icon-eye{ role: :button }
= 'Visualiser'
- elsif blob.content_type.in?(AUTHORIZED_IMAGE_TYPES)
= link_to image_url(blob.url), title: "#{champ.libelle} -- #{blob.filename}", data: { src: blob.url }, class: 'gallery-link' do
.thumbnail
= image_tag(attachment.variant(:medium), loading: :lazy)
= image_tag(attachment.representation(resize_to_limit: [400, 400]).processed.url, loading: :lazy)
.fr-btn.fr-btn--tertiary.fr-btn--icon-left.fr-icon-eye{ role: :button }
= 'Visualiser'
- else

View file

@ -89,11 +89,11 @@ describe ImageProcessorJob, type: :job do
context "when representation is required" do
before do
allow(blob).to receive(:variant_required?).and_return(true)
allow(blob).to receive(:representation_required?).and_return(true)
end
it "it creates blob representation" do
expect { described_class.perform_now(blob) }.to change { ActiveStorage::VariantRecord.count }.by(2)
expect { described_class.perform_now(blob) }.to change { ActiveStorage::VariantRecord.count }.by(1)
end
end
end