bug(instructeurs/dossiers#telecharger_pjs): zipline does not play well with not available active storage attachments

This commit is contained in:
Martin 2022-09-05 11:20:30 +02:00
parent 3aec5fe413
commit cbe77dd2f6
5 changed files with 88 additions and 2 deletions

View file

@ -132,8 +132,9 @@ module Experts
def telecharger_pjs
files = ActiveStorage::DownloadableFile.create_list_from_dossiers(Dossier.where(id: @dossier.id), true)
cleaned_files = ActiveStorage::DownloadableFile.cleanup_list_from_dossier(files)
zipline(files, "dossier-#{@dossier.id}.zip")
zipline(cleaned_files, "dossier-#{@dossier.id}.zip")
end
private

View file

@ -228,8 +228,9 @@ module Instructeurs
def telecharger_pjs
files = ActiveStorage::DownloadableFile.create_list_from_dossiers(Dossier.where(id: dossier.id))
cleaned_files = ActiveStorage::DownloadableFile.cleanup_list_from_dossier(files)
zipline(files, "dossier-#{dossier.id}.zip")
zipline(cleaned_files, "dossier-#{dossier.id}.zip")
end
def destroy

View file

@ -4,6 +4,27 @@ class ActiveStorage::DownloadableFile
PiecesJustificativesService.liste_documents(dossiers, for_expert)
end
def self.cleanup_list_from_dossier(files)
if Rails.application.config.active_storage.service != :openstack
return files
end
files.filter do |file, _filename|
if file.is_a?(PiecesJustificativesService::FakeAttachment)
true
else
service = file.blob.service
client = service.client
begin
client.head_object(service.container, file.blob.key)
true
rescue Fog::OpenStack::Storage::NotFound
false
end
end
end
end
private
def self.bill_and_path(bill)

View file

@ -769,6 +769,17 @@ describe Instructeurs::DossiersController, type: :controller do
dossier_id: dossier.id
}
end
it 'includes an attachment' do
expect(subject.headers['Content-Disposition']).to start_with('attachment; ')
end
it 'the attachment.zip is extractable' do
content = ZipTricks::FileReader.read_zip_structure(io: StringIO.new(subject.body))
file_names = content.map(&:filename)
expect(file_names.size).to eq(1)
expect(file_names.first).to start_with("dossier-#{dossier.id}/export-")
end
end
describe "#destroy" do

View file

@ -9,4 +9,56 @@ describe ActiveStorage::DownloadableFile do
it { expect(list.first[0].name).to eq "pdf_export_for_instructeur" }
end
end
describe '.cleanup_list_from_dossier' do
context 'active_storage.service test' do
before { Rails.application.config.active_storage.service = :test }
it 'returns the list' do
list = [:a, :b, :c]
result = ActiveStorage::DownloadableFile.cleanup_list_from_dossier(list)
expect(list).to eq(result)
end
end
context 'active_storage.service local' do
before { Rails.application.config.active_storage.service = :local }
it 'returns the list' do
list = [:a, :b, :c]
result = ActiveStorage::DownloadableFile.cleanup_list_from_dossier(list)
expect(list).to eq(result)
end
end
context 'active_storage.service openstack' do
let(:object_storage_container) { 'object_storage' }
let(:available_blob_key) { 'available' }
let(:unavailable_blob_key) { 'broken' }
let(:active_storage_client) { double }
let(:active_storage_service) { double(client: active_storage_client, container: object_storage_container) }
before do
require 'fog/openstack'
Rails.application.config.active_storage.service = :openstack
end
it 'returns the list' do
available_blob = double(key: available_blob_key)
unavailable_blob = double(key: unavailable_blob_key)
[available_blob, unavailable_blob].map do |attachment|
allow(attachment).to receive(:service).and_return(active_storage_service)
end
expect(active_storage_client).to receive(:head_object).with(object_storage_container, available_blob_key).and_return(true)
expect(active_storage_client).to receive(:head_object).with(object_storage_container, unavailable_blob_key).and_raise(Fog::OpenStack::Storage::NotFound.new('Object storage 99.99% availability leave space to 0.01% failure'))
list = [
[instance_double(ActiveStorage::Attachment, blob: available_blob), 'filename.pdf'],
[instance_double(ActiveStorage::Attachment, blob: unavailable_blob), 'filename.pdf']
]
result = ActiveStorage::DownloadableFile.cleanup_list_from_dossier(list)
expect(result.size).to eq(1)
expect(result.first.first.blob.key).to eq(available_blob_key)
end
end
end
end