Merge pull request #7065 from betagouv/US/fix-conflicting-archive-file-name
fix(ProcedureArchiveService.zip_root_folder): should take archive instance otherwise when we generate many archive for the same procedure, errors may occures
This commit is contained in:
commit
3b5b9a63c5
2 changed files with 40 additions and 39 deletions
|
@ -26,7 +26,7 @@ class ProcedureArchiveService
|
|||
end
|
||||
|
||||
attachments = create_list_of_attachments(dossiers)
|
||||
download_and_zip(attachments) do |zip_filepath|
|
||||
download_and_zip(archive, attachments) do |zip_filepath|
|
||||
ArchiveUploader.new(procedure: @procedure, archive: archive, filepath: zip_filepath)
|
||||
.upload
|
||||
end
|
||||
|
@ -46,10 +46,10 @@ class ProcedureArchiveService
|
|||
|
||||
private
|
||||
|
||||
def download_and_zip(attachments, &block)
|
||||
def download_and_zip(archive, attachments, &block)
|
||||
Dir.mktmpdir(nil, ARCHIVE_CREATION_DIR) do |tmp_dir|
|
||||
archive_dir = File.join(tmp_dir, zip_root_folder)
|
||||
zip_path = File.join(ARCHIVE_CREATION_DIR, "#{zip_root_folder}.zip")
|
||||
archive_dir = File.join(tmp_dir, zip_root_folder(archive))
|
||||
zip_path = File.join(ARCHIVE_CREATION_DIR, "#{zip_root_folder(archive)}.zip")
|
||||
|
||||
begin
|
||||
FileUtils.remove_entry_secure(archive_dir) if Dir.exist?(archive_dir)
|
||||
|
@ -60,7 +60,7 @@ class ProcedureArchiveService
|
|||
|
||||
Dir.chdir(tmp_dir) do
|
||||
File.delete(zip_path) if File.exist?(zip_path)
|
||||
system 'zip', '-0', '-r', zip_path, zip_root_folder
|
||||
system 'zip', '-0', '-r', zip_path, zip_root_folder(archive)
|
||||
end
|
||||
yield(zip_path)
|
||||
ensure
|
||||
|
@ -70,8 +70,8 @@ class ProcedureArchiveService
|
|||
end
|
||||
end
|
||||
|
||||
def zip_root_folder
|
||||
"procedure-#{@procedure.id}"
|
||||
def zip_root_folder(archive)
|
||||
"procedure-#{@procedure.id}-#{archive.id}"
|
||||
end
|
||||
|
||||
def create_list_of_attachments(dossiers)
|
||||
|
|
|
@ -55,11 +55,11 @@ describe ProcedureArchiveService do
|
|||
files = ZipTricks::FileReader.read_zip_structure(io: f)
|
||||
|
||||
structure = [
|
||||
"procedure-#{procedure.id}/",
|
||||
"procedure-#{procedure.id}/dossier-#{dossier.id}/",
|
||||
"procedure-#{procedure.id}/dossier-#{dossier.id}/pieces_justificatives/",
|
||||
"procedure-#{procedure.id}/dossier-#{dossier.id}/pieces_justificatives/attestation-dossier--05-03-2021-00-00-#{dossier.attestation.pdf.id % 10000}.pdf",
|
||||
"procedure-#{procedure.id}/dossier-#{dossier.id}/export-#{dossier.id}-05-03-2021-00-00-#{dossier.id}.pdf"
|
||||
"procedure-#{procedure.id}-#{archive.id}/",
|
||||
"procedure-#{procedure.id}-#{archive.id}/dossier-#{dossier.id}/",
|
||||
"procedure-#{procedure.id}-#{archive.id}/dossier-#{dossier.id}/pieces_justificatives/",
|
||||
"procedure-#{procedure.id}-#{archive.id}/dossier-#{dossier.id}/pieces_justificatives/attestation-dossier--05-03-2021-00-00-#{dossier.attestation.pdf.id % 10000}.pdf",
|
||||
"procedure-#{procedure.id}-#{archive.id}/dossier-#{dossier.id}/export-#{dossier.id}-05-03-2021-00-00-#{dossier.id}.pdf"
|
||||
]
|
||||
expect(files.map(&:filename)).to match_array(structure)
|
||||
end
|
||||
|
@ -75,11 +75,11 @@ describe ProcedureArchiveService do
|
|||
archive.file.open do |f|
|
||||
files = ZipTricks::FileReader.read_zip_structure(io: f)
|
||||
structure = [
|
||||
"procedure-#{procedure.id}/",
|
||||
"procedure-#{procedure.id}/dossier-#{dossier.id}/",
|
||||
"procedure-#{procedure.id}/dossier-#{dossier.id}/pieces_justificatives/",
|
||||
"procedure-#{procedure.id}/dossier-#{dossier.id}/export-#{dossier.id}-05-03-2021-00-00-#{dossier.id}.pdf",
|
||||
"procedure-#{procedure.id}/LISEZMOI.txt"
|
||||
"procedure-#{procedure.id}-#{archive.id}/",
|
||||
"procedure-#{procedure.id}-#{archive.id}/dossier-#{dossier.id}/",
|
||||
"procedure-#{procedure.id}-#{archive.id}/dossier-#{dossier.id}/pieces_justificatives/",
|
||||
"procedure-#{procedure.id}-#{archive.id}/dossier-#{dossier.id}/export-#{dossier.id}-05-03-2021-00-00-#{dossier.id}.pdf",
|
||||
"procedure-#{procedure.id}-#{archive.id}/LISEZMOI.txt"
|
||||
]
|
||||
expect(files.map(&:filename)).to match_array(structure)
|
||||
end
|
||||
|
@ -122,16 +122,16 @@ describe ProcedureArchiveService do
|
|||
archive.file.open do |f|
|
||||
zip_entries = ZipTricks::FileReader.read_zip_structure(io: f)
|
||||
structure = [
|
||||
"procedure-#{procedure.id}/",
|
||||
"procedure-#{procedure.id}/dossier-#{dossier.id}/",
|
||||
"procedure-#{procedure.id}/dossier-#{dossier.id}/export-dossier-05-03-2020-00-00-1.pdf",
|
||||
"procedure-#{procedure.id}/dossier-#{dossier.id}/pieces_justificatives/",
|
||||
"procedure-#{procedure.id}/dossier-#{dossier.id}/export-#{dossier.id}-05-03-2021-00-00-#{dossier.id}.pdf",
|
||||
"procedure-#{procedure.id}/LISEZMOI.txt"
|
||||
"procedure-#{procedure.id}-#{archive.id}/",
|
||||
"procedure-#{procedure.id}-#{archive.id}/dossier-#{dossier.id}/",
|
||||
"procedure-#{procedure.id}-#{archive.id}/dossier-#{dossier.id}/export-dossier-05-03-2020-00-00-1.pdf",
|
||||
"procedure-#{procedure.id}-#{archive.id}/dossier-#{dossier.id}/pieces_justificatives/",
|
||||
"procedure-#{procedure.id}-#{archive.id}/dossier-#{dossier.id}/export-#{dossier.id}-05-03-2021-00-00-#{dossier.id}.pdf",
|
||||
"procedure-#{procedure.id}-#{archive.id}/LISEZMOI.txt"
|
||||
]
|
||||
expect(zip_entries.map(&:filename)).to match_array(structure)
|
||||
zip_entries.map do |entry|
|
||||
next unless entry.filename == "procedure-#{procedure.id}/LISEZMOI.txt"
|
||||
next unless entry.filename == "procedure-#{procedure.id}-#{archive.id}/LISEZMOI.txt"
|
||||
extracted_content = ""
|
||||
extractor = entry.extractor_from(f)
|
||||
extracted_content << extractor.extract(1024 * 1024) until extractor.eof?
|
||||
|
@ -158,15 +158,15 @@ describe ProcedureArchiveService do
|
|||
archive.file.open do |f|
|
||||
files = ZipTricks::FileReader.read_zip_structure(io: f)
|
||||
structure = [
|
||||
"procedure-#{procedure.id}/",
|
||||
"procedure-#{procedure.id}/dossier-#{dossier.id}/",
|
||||
"procedure-#{procedure.id}/dossier-#{dossier.id}/pieces_justificatives/",
|
||||
"procedure-#{procedure.id}/dossier-#{dossier.id}/pieces_justificatives/attestation-dossier--05-03-2020-00-00-#{dossier.attestation.pdf.id % 10000}.pdf",
|
||||
"procedure-#{procedure.id}/dossier-#{dossier.id}/export-#{dossier.id}-05-03-2020-00-00-#{dossier.id}.pdf",
|
||||
"procedure-#{procedure.id}/dossier-#{dossier_2020.id}/",
|
||||
"procedure-#{procedure.id}/dossier-#{dossier_2020.id}/export-#{dossier_2020.id}-05-03-2020-00-00-#{dossier_2020.id}.pdf",
|
||||
"procedure-#{procedure.id}/dossier-#{dossier_2020.id}/pieces_justificatives/",
|
||||
"procedure-#{procedure.id}/dossier-#{dossier_2020.id}/pieces_justificatives/attestation-dossier--05-03-2020-00-00-#{dossier_2020.attestation.pdf.id % 10000}.pdf"
|
||||
"procedure-#{procedure.id}-#{archive.id}/",
|
||||
"procedure-#{procedure.id}-#{archive.id}/dossier-#{dossier.id}/",
|
||||
"procedure-#{procedure.id}-#{archive.id}/dossier-#{dossier.id}/pieces_justificatives/",
|
||||
"procedure-#{procedure.id}-#{archive.id}/dossier-#{dossier.id}/pieces_justificatives/attestation-dossier--05-03-2020-00-00-#{dossier.attestation.pdf.id % 10000}.pdf",
|
||||
"procedure-#{procedure.id}-#{archive.id}/dossier-#{dossier.id}/export-#{dossier.id}-05-03-2020-00-00-#{dossier.id}.pdf",
|
||||
"procedure-#{procedure.id}-#{archive.id}/dossier-#{dossier_2020.id}/",
|
||||
"procedure-#{procedure.id}-#{archive.id}/dossier-#{dossier_2020.id}/export-#{dossier_2020.id}-05-03-2020-00-00-#{dossier_2020.id}.pdf",
|
||||
"procedure-#{procedure.id}-#{archive.id}/dossier-#{dossier_2020.id}/pieces_justificatives/",
|
||||
"procedure-#{procedure.id}-#{archive.id}/dossier-#{dossier_2020.id}/pieces_justificatives/attestation-dossier--05-03-2020-00-00-#{dossier_2020.attestation.pdf.id % 10000}.pdf"
|
||||
]
|
||||
expect(files.map(&:filename)).to match_array(structure)
|
||||
end
|
||||
|
@ -176,29 +176,30 @@ describe ProcedureArchiveService do
|
|||
end
|
||||
|
||||
describe '#download_and_zip' do
|
||||
let(:archive) { build(:archive, id: '3') }
|
||||
it 'create a tmpdir while block is running' do
|
||||
previous_dir_list = Dir.entries(ProcedureArchiveService::ARCHIVE_CREATION_DIR)
|
||||
|
||||
service.send(:download_and_zip, []) do |_zip_file|
|
||||
service.send(:download_and_zip, archive, []) do |_zip_file|
|
||||
new_dir_list = Dir.entries(ProcedureArchiveService::ARCHIVE_CREATION_DIR)
|
||||
expect(previous_dir_list).not_to eq(new_dir_list)
|
||||
end
|
||||
end
|
||||
|
||||
it 'cleans up its tmpdir after block execution' do
|
||||
expect { service.send(:download_and_zip, []) { |zip_file| } }
|
||||
expect { service.send(:download_and_zip, archive, []) { |zip_file| } }
|
||||
.not_to change { Dir.entries(ProcedureArchiveService::ARCHIVE_CREATION_DIR) }
|
||||
end
|
||||
|
||||
it 'creates a zip with zip utility' do
|
||||
expected_zip_path = File.join(ProcedureArchiveService::ARCHIVE_CREATION_DIR, "#{service.send(:zip_root_folder)}.zip")
|
||||
expected_zip_path = File.join(ProcedureArchiveService::ARCHIVE_CREATION_DIR, "#{service.send(:zip_root_folder, archive)}.zip")
|
||||
expect(service).to receive(:system).with('zip', '-0', '-r', expected_zip_path, an_instance_of(String))
|
||||
service.send(:download_and_zip, []) { |zip_path| }
|
||||
service.send(:download_and_zip, archive, []) { |zip_path| }
|
||||
end
|
||||
|
||||
it 'cleans up its generated zip' do
|
||||
expected_zip_path = File.join(ProcedureArchiveService::ARCHIVE_CREATION_DIR, "#{service.send(:zip_root_folder)}.zip")
|
||||
service.send(:download_and_zip, []) do |_zip_path|
|
||||
expected_zip_path = File.join(ProcedureArchiveService::ARCHIVE_CREATION_DIR, "#{service.send(:zip_root_folder, archive)}.zip")
|
||||
service.send(:download_and_zip, archive, []) do |_zip_path|
|
||||
expect(File.exist?(expected_zip_path)).to be_truthy
|
||||
end
|
||||
expect(File.exist?(expected_zip_path)).to be_falsey
|
||||
|
|
Loading…
Reference in a new issue