diff --git a/.gitignore b/.gitignore index 9a9dd9ca4..4af928a07 100644 --- a/.gitignore +++ b/.gitignore @@ -30,6 +30,7 @@ yarn-debug.log* /public/packs /public/packs-test /node_modules +vendor/* /yarn-error.log yarn-debug.log* .yarn-integrity diff --git a/Gemfile b/Gemfile index d46ee525c..7ffa37c1c 100644 --- a/Gemfile +++ b/Gemfile @@ -67,6 +67,7 @@ gem 'turbolinks' # Turbolinks makes following links in your web application fast gem 'typhoeus' gem 'warden' gem 'webpacker' +gem 'zipline' gem 'zxcvbn-ruby', require: 'zxcvbn' group :test do diff --git a/Gemfile.lock b/Gemfile.lock index 76d7ca1fc..b925daa7a 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -170,6 +170,7 @@ GEM crass (1.0.4) css_parser (1.6.0) addressable + curb (0.9.10) daemons (1.3.1) database_cleaner (1.7.0) datetime_picker_rails (0.0.7) @@ -674,6 +675,11 @@ GEM nokogiri (~> 1.8) xray-rails (0.3.1) rails (>= 3.1.0) + zip_tricks (4.7.4) + zipline (1.1.0) + curb (>= 0.8.0, < 0.10) + rails (>= 3.2.1, < 6.1) + zip_tricks (>= 4.2.1, <= 5.0.0) zxcvbn-ruby (0.1.2) PLATFORMS @@ -779,6 +785,7 @@ DEPENDENCIES webmock webpacker xray-rails + zipline zxcvbn-ruby BUNDLED WITH diff --git a/app/controllers/gestionnaires/dossiers_controller.rb b/app/controllers/gestionnaires/dossiers_controller.rb index d58fed8fb..adbc9e237 100644 --- a/app/controllers/gestionnaires/dossiers_controller.rb +++ b/app/controllers/gestionnaires/dossiers_controller.rb @@ -5,6 +5,9 @@ module Gestionnaires include CreateAvisConcern include DossierHelper + include ActionController::Streaming + include Zipline + after_action :mark_demande_as_read, only: :show after_action :mark_messagerie_as_read, only: [:messagerie, :create_commentaire] after_action :mark_avis_as_read, only: [:avis, :create_avis] @@ -172,6 +175,14 @@ module Gestionnaires render layout: "print" end + def telecharger_pjs + return head(:forbidden) if !Flipflop.download_as_zip_enabled? || !dossier.attachments_downloadable? + + files = ActiveStorage::DownloadableFile.create_list_from_dossier(dossier) + + zipline(files, "dossier-#{dossier.id}.zip") + end + private def dossier diff --git a/app/lib/active_storage/downloadable_file.rb b/app/lib/active_storage/downloadable_file.rb new file mode 100644 index 000000000..3ef733e8d --- /dev/null +++ b/app/lib/active_storage/downloadable_file.rb @@ -0,0 +1,29 @@ +class ActiveStorage::DownloadableFile + def initialize(attached) + if using_local_backend? + @url = 'file://' + ActiveStorage::Blob.service.path_for(attached.key) + else + @url = attached.service_url + end + end + + def url + @url + end + + def self.create_list_from_dossier(dossier) + pjs = PiecesJustificativesService.liste_pieces_justificatives(dossier) + pjs.map do |pj| + [ + ActiveStorage::DownloadableFile.new(pj.piece_justificative_file), + pj.piece_justificative_file.filename.to_s + ] + end + end + + private + + def using_local_backend? + [:local, :local_test, :test].include?(Rails.application.config.active_storage.service) + end +end diff --git a/app/models/dossier.rb b/app/models/dossier.rb index 81c426824..28682f056 100644 --- a/app/models/dossier.rb +++ b/app/models/dossier.rb @@ -466,6 +466,10 @@ class Dossier < ApplicationRecord end end + def attachments_downloadable? + !PiecesJustificativesService.liste_pieces_justificatives(self).empty? && PiecesJustificativesService.pieces_justificatives_total_size(self) < 50.megabytes + end + private def log_dossier_operation(author, operation, subject = nil) diff --git a/app/services/pieces_justificatives_service.rb b/app/services/pieces_justificatives_service.rb index 411e11124..da7d53325 100644 --- a/app/services/pieces_justificatives_service.rb +++ b/app/services/pieces_justificatives_service.rb @@ -70,6 +70,17 @@ class PiecesJustificativesService end end + def self.liste_pieces_justificatives(dossier) + dossier.champs + .select { |c| c.type_champ == TypeDeChamp.type_champs.fetch(:piece_justificative) } + .filter { |pj| pj.piece_justificative_file.attached? } + end + + def self.pieces_justificatives_total_size(dossier) + liste_pieces_justificatives(dossier) + .sum { |pj| pj.piece_justificative_file.byte_size } + end + def self.serialize_types_de_champ_as_type_pj(procedure) tdcs = procedure.types_de_champ.select { |type_champ| type_champ.old_pj.present? } tdcs.map.with_index do |type_champ, order_place| diff --git a/app/views/gestionnaires/dossiers/_header.html.haml b/app/views/gestionnaires/dossiers/_header.html.haml index 186d4c4d6..61afc1361 100644 --- a/app/views/gestionnaires/dossiers/_header.html.haml +++ b/app/views/gestionnaires/dossiers/_header.html.haml @@ -18,6 +18,14 @@ = link_to "Tout le dossier", print_gestionnaire_dossier_path(dossier.procedure, dossier), target: "_blank", rel: "noopener", class: "menu-item menu-link" %li = link_to "Uniquement cet onglet", "#", onclick: "window.print()", class: "menu-item menu-link" + - if Flipflop.download_as_zip_enabled? && dossier.attachments_downloadable? + %span.dropdown.print-menu-opener + %button.button.dropdown-button.icon-only + %span.icon.attachment + %ul.print-menu.dropdown-content + %li + = link_to "Télécharger toutes les pièces jointes", telecharger_pjs_gestionnaire_dossier_path(dossier.procedure, dossier), target: "_blank", rel: "noopener", class: "menu-item menu-link" + = render partial: "gestionnaires/procedures/dossier_actions", locals: { procedure: dossier.procedure, dossier: dossier, dossier_is_followed: current_gestionnaire&.follow?(dossier) } %span.state-button diff --git a/config/features.rb b/config/features.rb index 1f7816d14..d4bb54e21 100644 --- a/config/features.rb +++ b/config/features.rb @@ -18,6 +18,8 @@ Flipflop.configure do feature :procedure_export_v2_enabled feature :operation_log_serialize_subject + feature :download_as_zip_enabled, + default: false group :development do feature :mini_profiler_enabled, diff --git a/config/routes.rb b/config/routes.rb index bb9785886..6abccf8e2 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -335,6 +335,7 @@ Rails.application.routes.draw do post 'send-to-instructeurs' => 'dossiers#send_to_instructeurs' post 'avis' => 'dossiers#create_avis' get 'print' => 'dossiers#print' + get 'telecharger_pjs' => 'dossiers#telecharger_pjs' end end end diff --git a/spec/controllers/gestionnaires/dossiers_controller_spec.rb b/spec/controllers/gestionnaires/dossiers_controller_spec.rb index cbf5b775a..0f327221a 100644 --- a/spec/controllers/gestionnaires/dossiers_controller_spec.rb +++ b/spec/controllers/gestionnaires/dossiers_controller_spec.rb @@ -524,4 +524,24 @@ describe Gestionnaires::DossiersController, type: :controller do it { expect(champ_repetition.champs.first.value).to eq('text') } it { expect(response).to redirect_to(annotations_privees_gestionnaire_dossier_path(dossier.procedure, dossier)) } end + + describe "#telecharger_pjs" do + subject do + get :telecharger_pjs, params: { + procedure_id: procedure.id, + dossier_id: dossier.id + } + end + + context 'when zip download is disabled through flipflop' do + before do + Flipflop::FeatureSet.current.test!.switch!(:download_as_zip_enabled, false) + end + + it 'is forbidden' do + subject + expect(response).to have_http_status(:forbidden) + end + end + end end diff --git a/spec/lib/active_storage/downloadable_file_spec.rb b/spec/lib/active_storage/downloadable_file_spec.rb new file mode 100644 index 000000000..b802a90c9 --- /dev/null +++ b/spec/lib/active_storage/downloadable_file_spec.rb @@ -0,0 +1,26 @@ +describe ActiveStorage::DownloadableFile do + let(:tpjs) { [tpj_not_mandatory] } + let!(:tpj_not_mandatory) do + TypeDePieceJustificative.create(libelle: 'not mandatory', mandatory: false) + end + let(:procedure) { Procedure.create(types_de_piece_justificative: tpjs) } + let(:dossier) { Dossier.create(procedure: procedure) } + let(:procedure) { Procedure.create(types_de_piece_justificative: tpjs) } + let(:dossier) { Dossier.create(procedure: procedure) } + let(:list) { ActiveStorage::DownloadableFile.create_list_from_dossier(dossier) } + + describe 'create_list_from_dossier' do + context 'when no piece_justificative is present' do + it { expect(list).to match([]) } + end + + context 'when there is a piece_justificative' do + let (:pj) { create(:champ, :piece_justificative, :with_piece_justificative_file) } + before do + dossier.champs = [pj] + end + + it { expect(list.length).to be 1 } + end + end +end diff --git a/spec/models/dossier_spec.rb b/spec/models/dossier_spec.rb index d1ac82b16..67ad2a39a 100644 --- a/spec/models/dossier_spec.rb +++ b/spec/models/dossier_spec.rb @@ -99,6 +99,14 @@ describe Dossier do end end + describe '#types_de_piece_justificative' do + subject { dossier.types_de_piece_justificative } + it 'returns list of required piece justificative' do + expect(subject.size).to eq(2) + expect(subject).to include(TypeDePieceJustificative.find(TypeDePieceJustificative.first.id)) + end + end + describe '#retrieve_last_piece_justificative_by_type', vcr: { cassette_name: 'models_dossier_retrieve_last_piece_justificative_by_type' } do let(:types_de_pj_dossier) { dossier.procedure.types_de_piece_justificative } @@ -113,6 +121,20 @@ describe Dossier do end end + describe '#retrieve_all_piece_justificative_by_type' do + let(:types_de_pj_dossier) { dossier.procedure.types_de_piece_justificative } + + subject { dossier.retrieve_all_piece_justificative_by_type types_de_pj_dossier.first } + + before do + create :piece_justificative, :rib, dossier: dossier, type_de_piece_justificative: types_de_pj_dossier.first + end + + it 'returns a list of the piece justificative' do + expect(subject).not_to be_empty + end + end + describe '#build_default_champs' do context 'when dossier is linked to a procedure with type_de_champ_public and private' do let(:dossier) { create(:dossier, user: user) } @@ -1008,4 +1030,32 @@ describe Dossier do after { Timecop.return } end + + describe '#attachments_downloadable?' do + let(:dossier) { create(:dossier, user: user) } + # subject { dossier.attachments_downloadable? } + + context "no attachments" do + it { + expect(PiecesJustificativesService).to receive(:liste_pieces_justificatives).and_return([]) + expect(dossier.attachments_downloadable?).to be false + } + end + + context "with a small attachment" do + it { + expect(PiecesJustificativesService).to receive(:liste_pieces_justificatives).and_return([Champ.new]) + expect(PiecesJustificativesService).to receive(:pieces_justificatives_total_size).and_return(4.megabytes) + expect(dossier.attachments_downloadable?).to be true + } + end + + context "with a too large attachment" do + it { + expect(PiecesJustificativesService).to receive(:liste_pieces_justificatives).and_return([Champ.new]) + expect(PiecesJustificativesService).to receive(:pieces_justificatives_total_size).and_return(100.megabytes) + expect(dossier.attachments_downloadable?).to be false + } + end + end end diff --git a/spec/services/pieces_justificatives_service_spec.rb b/spec/services/pieces_justificatives_service_spec.rb index 3d8eb6d09..6988e3007 100644 --- a/spec/services/pieces_justificatives_service_spec.rb +++ b/spec/services/pieces_justificatives_service_spec.rb @@ -20,6 +20,9 @@ describe PiecesJustificativesService do let(:errors) { PiecesJustificativesService.upload!(dossier, user, hash) } let(:tpjs) { [tpj_not_mandatory] } + let(:attachment_list) { PiecesJustificativesService.liste_pieces_justificatives(dossier) } + let(:poids_total) { PiecesJustificativesService.pieces_justificatives_total_size(dossier) } + describe 'self.upload!' do context 'when no params are given' do it { expect(errors).to eq([]) } @@ -81,12 +84,30 @@ describe PiecesJustificativesService do before :each do # we are messing around piece_justificative # because directly doubling carrierwave params seems complicated - piece_justificative_double = double(type_de_piece_justificative: tpj_mandatory) expect(dossier).to receive(:pieces_justificatives).and_return([piece_justificative_double]) end - it { expect(errors).to match([]) } + it { + expect(errors).to match([]) + } + end + end + + describe '#attachment_list' do + context 'when no piece_justificative is present' do + it { expect(attachment_list).to match([]) } + it { expect(poids_total).to be 0 } + end + + context 'when there is a piece_justificative' do + let (:pj) { create(:champ, :piece_justificative, :with_piece_justificative_file) } + before do + dossier.champs = [pj] + end + + it { expect(attachment_list).not_to be_empty } + it { expect(poids_total).to be pj.piece_justificative_file.byte_size } end end