feat(attachment): allow user to "view" a file without the complete download UI

This commit is contained in:
Colin Darie 2022-12-07 23:04:50 +01:00
parent 7719ce1865
commit 7c5d27d8e9
3 changed files with 30 additions and 4 deletions

View file

@ -119,6 +119,11 @@ class Attachment::EditComponent < ApplicationComponent
def downloadable? def downloadable?
return false unless user_can_download? return false unless user_can_download?
viewable?
end
def viewable?
return false if attachment.virus_scanner_error? return false if attachment.virus_scanner_error?
return false if attachment.watermark_pending? return false if attachment.watermark_pending?

View file

@ -9,7 +9,7 @@
- if downloadable? - if downloadable?
= render Dsfr::DownloadComponent.new(attachment:) = render Dsfr::DownloadComponent.new(attachment:)
- else - else
%span.attachment-filename.fr-mr-1w-= attachment.filename.to_s %span.attachment-filename.fr-mr-1w= link_to_if(viewable?, attachment.filename.to_s, helpers.url_for(attachment.blob), title: "Ouvrir le fichier #{attachment.filename.to_s}", **helpers.external_link_attributes)
= render Attachment::ProgressComponent.new(attachment: attachment) = render Attachment::ProgressComponent.new(attachment: attachment)

View file

@ -86,13 +86,19 @@ RSpec.describe Attachment::EditComponent, type: :component do
context 'when user can download' do context 'when user can download' do
let(:kwargs) { { user_can_download: true } } let(:kwargs) { { user_can_download: true } }
it 'renders a link to download the file' do context 'when watermarking is done' do
expect(subject).to have_link(filename) before do
attachment.metadata['watermark'] = true
end
it 'renders a complete downlaod interface with details to download the file' do
expect(subject).to have_link(text: filename)
expect(subject).to have_text(/PNG.+\d+ octets/)
end
end end
context 'when watermark is pending' do context 'when watermark is pending' do
let(:champ) { create(:champ_titre_identite) } let(:champ) { create(:champ_titre_identite) }
let(:kwargs) { { user_can_download: true } }
it 'displays the filename, but doesnt allow to download the file' do it 'displays the filename, but doesnt allow to download the file' do
expect(attachment.watermark_pending?).to be_truthy expect(attachment.watermark_pending?).to be_truthy
@ -104,6 +110,21 @@ RSpec.describe Attachment::EditComponent, type: :component do
end end
end end
context 'when user cannot download' do
let(:kwargs) { { user_can_download: false } }
context 'when watermarking is done' do
before do
attachment.metadata['watermark'] = true
end
it 'renders a simple link to view file' do
expect(subject).to have_link(text: filename)
expect(subject).not_to have_text(/PNG.+\d+ octets/)
end
end
end
context 'with non nominal or final antivirus status' do context 'with non nominal or final antivirus status' do
before do before do
champ.piece_justificative_file[0].blob.update(metadata: attachment.blob.metadata.merge(virus_scan_result: virus_scan_result)) champ.piece_justificative_file[0].blob.update(metadata: attachment.blob.metadata.merge(virus_scan_result: virus_scan_result))