rename root directory in zip export

This commit is contained in:
Christophe Robillard 2024-03-22 10:05:47 +01:00 committed by simon lehericey
parent 7661b8b1b2
commit 7a39752630
No known key found for this signature in database
GPG key ID: CDE670D827C7B3C5
3 changed files with 34 additions and 32 deletions

View file

@ -1,9 +1,10 @@
class DownloadableFileService
ARCHIVE_CREATION_DIR = ENV.fetch('ARCHIVE_CREATION_DIR') { '/tmp' }
EXPORT_DIRNAME = 'export'
def self.download_and_zip(procedure, attachments, filename, &block)
Dir.mktmpdir(nil, ARCHIVE_CREATION_DIR) do |tmp_dir|
export_dir = File.join(tmp_dir, filename)
export_dir = File.join(tmp_dir, EXPORT_DIRNAME)
zip_path = File.join(ARCHIVE_CREATION_DIR, "#{filename}.zip")
begin
@ -15,7 +16,7 @@ class DownloadableFileService
Dir.chdir(tmp_dir) do
File.delete(zip_path) if File.exist?(zip_path)
system 'zip', '-0', '-r', zip_path, filename
system 'zip', '-0', '-r', zip_path, EXPORT_DIRNAME
end
yield(zip_path)
ensure

View file

@ -33,11 +33,11 @@ describe ProcedureArchiveService do
files = ZipTricks::FileReader.read_zip_structure(io: f)
structure = [
"#{service.send(:zip_root_folder, archive)}/",
"#{service.send(:zip_root_folder, archive)}/dossier-#{dossier.id}/",
"#{service.send(:zip_root_folder, archive)}/dossier-#{dossier.id}/pieces_justificatives/",
"#{service.send(:zip_root_folder, archive)}/dossier-#{dossier.id}/pieces_justificatives/attestation-dossier--05-03-2021-00-00-#{dossier.attestation.pdf.id % 10000}.pdf",
"#{service.send(:zip_root_folder, archive)}/dossier-#{dossier.id}/export-#{dossier.id}-05-03-2021-00-00-#{dossier.id % 10000}.pdf"
"export/",
"export/dossier-#{dossier.id}/",
"export/dossier-#{dossier.id}/pieces_justificatives/",
"export/dossier-#{dossier.id}/pieces_justificatives/attestation-dossier--05-03-2021-00-00-#{dossier.attestation.pdf.id % 10000}.pdf",
"export/dossier-#{dossier.id}/export-#{dossier.id}-05-03-2021-00-00-#{dossier.id % 10000}.pdf"
]
expect(files.map(&:filename)).to match_array(structure)
end
@ -53,11 +53,11 @@ describe ProcedureArchiveService do
archive.file.open do |f|
files = ZipTricks::FileReader.read_zip_structure(io: f)
structure = [
"#{service.send(:zip_root_folder, archive)}/",
"#{service.send(:zip_root_folder, archive)}/-LISTE-DES-FICHIERS-EN-ERREURS.txt",
"#{service.send(:zip_root_folder, archive)}/dossier-#{dossier.id}/",
"#{service.send(:zip_root_folder, archive)}/dossier-#{dossier.id}/pieces_justificatives/",
"#{service.send(:zip_root_folder, archive)}/dossier-#{dossier.id}/export-#{dossier.id}-05-03-2021-00-00-#{dossier.id % 10000}.pdf"
"export/",
"export/-LISTE-DES-FICHIERS-EN-ERREURS.txt",
"export/dossier-#{dossier.id}/",
"export/dossier-#{dossier.id}/pieces_justificatives/",
"export/dossier-#{dossier.id}/export-#{dossier.id}-05-03-2021-00-00-#{dossier.id % 10000}.pdf"
]
expect(files.map(&:filename)).to match_array(structure)
end
@ -100,12 +100,12 @@ describe ProcedureArchiveService do
archive.file.open do |f|
zip_entries = ZipTricks::FileReader.read_zip_structure(io: f)
structure = [
"#{service.send(:zip_root_folder, archive)}/",
"#{service.send(:zip_root_folder, archive)}/-LISTE-DES-FICHIERS-EN-ERREURS.txt",
"#{service.send(:zip_root_folder, archive)}/dossier-#{dossier.id}/",
"#{service.send(:zip_root_folder, archive)}/dossier-#{dossier.id}/export-dossier-05-03-2020-00-00-1.pdf",
"#{service.send(:zip_root_folder, archive)}/dossier-#{dossier.id}/pieces_justificatives/",
"#{service.send(:zip_root_folder, archive)}/dossier-#{dossier.id}/export-#{dossier.id}-05-03-2021-00-00-#{dossier.id % 10000}.pdf"
"export/",
"export/-LISTE-DES-FICHIERS-EN-ERREURS.txt",
"export/dossier-#{dossier.id}/",
"export/dossier-#{dossier.id}/export-dossier-05-03-2020-00-00-1.pdf",
"export/dossier-#{dossier.id}/pieces_justificatives/",
"export/dossier-#{dossier.id}/export-#{dossier.id}-05-03-2021-00-00-#{dossier.id % 10000}.pdf"
]
expect(zip_entries.map(&:filename)).to match_array(structure)
zip_entries.map do |entry|
@ -134,15 +134,15 @@ describe ProcedureArchiveService do
archive.file.open do |f|
files = ZipTricks::FileReader.read_zip_structure(io: f)
structure = [
"#{service.send(:zip_root_folder, archive)}/",
"#{service.send(:zip_root_folder, archive)}/dossier-#{dossier.id}/",
"#{service.send(:zip_root_folder, archive)}/dossier-#{dossier.id}/pieces_justificatives/",
"#{service.send(:zip_root_folder, archive)}/dossier-#{dossier.id}/pieces_justificatives/attestation-dossier--05-03-2020-00-00-#{dossier.attestation.pdf.id % 10000}.pdf",
"#{service.send(:zip_root_folder, archive)}/dossier-#{dossier.id}/export-#{dossier.id}-05-03-2020-00-00-#{dossier.id % 10000}.pdf",
"#{service.send(:zip_root_folder, archive)}/dossier-#{dossier_2020.id}/",
"#{service.send(:zip_root_folder, archive)}/dossier-#{dossier_2020.id}/export-#{dossier_2020.id}-05-03-2020-00-00-#{dossier_2020.id % 10000}.pdf",
"#{service.send(:zip_root_folder, archive)}/dossier-#{dossier_2020.id}/pieces_justificatives/",
"#{service.send(:zip_root_folder, archive)}/dossier-#{dossier_2020.id}/pieces_justificatives/attestation-dossier--05-03-2020-00-00-#{dossier_2020.attestation.pdf.id % 10000}.pdf"
"export/",
"export/dossier-#{dossier.id}/",
"export/dossier-#{dossier.id}/pieces_justificatives/",
"export/dossier-#{dossier.id}/pieces_justificatives/attestation-dossier--05-03-2020-00-00-#{dossier.attestation.pdf.id % 10000}.pdf",
"export/dossier-#{dossier.id}/export-#{dossier.id}-05-03-2020-00-00-#{dossier.id % 10000}.pdf",
"export/dossier-#{dossier_2020.id}/",
"export/dossier-#{dossier_2020.id}/export-#{dossier_2020.id}-05-03-2020-00-00-#{dossier_2020.id % 10000}.pdf",
"export/dossier-#{dossier_2020.id}/pieces_justificatives/",
"export/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

View file

@ -540,12 +540,13 @@ describe ProcedureExportService do
File.write('tmp.zip', subject.download, mode: 'wb')
File.open('tmp.zip') do |fd|
files = ZipTricks::FileReader.read_zip_structure(io: fd)
base_fn = 'export'
structure = [
"#{service.send(:base_filename)}/",
"#{service.send(:base_filename)}/dossier-#{dossier.id}/",
"#{service.send(:base_filename)}/dossier-#{dossier.id}/pieces_justificatives/",
"#{service.send(:base_filename)}/dossier-#{dossier.id}/#{ActiveStorage::DownloadableFile.timestamped_filename(ActiveStorage::Attachment.where(record_type: "Champ").first)}",
"#{service.send(:base_filename)}/dossier-#{dossier.id}/#{ActiveStorage::DownloadableFile.timestamped_filename(dossier_exports.first.first)}"
"#{base_fn}/",
"#{base_fn}/dossier-#{dossier.id}/",
"#{base_fn}/dossier-#{dossier.id}/pieces_justificatives/",
"#{base_fn}/dossier-#{dossier.id}/#{ActiveStorage::DownloadableFile.timestamped_filename(ActiveStorage::Attachment.where(record_type: "Champ").first)}",
"#{base_fn}/dossier-#{dossier.id}/#{ActiveStorage::DownloadableFile.timestamped_filename(dossier_exports.first.first)}"
]
expect(files.size).to eq(structure.size)
expect(files.map(&:filename)).to match_array(structure)