2022-04-08 17:07:54 +02:00
|
|
|
class DownloadableFileService
|
|
|
|
ARCHIVE_CREATION_DIR = ENV.fetch('ARCHIVE_CREATION_DIR') { '/tmp' }
|
2024-03-22 10:05:47 +01:00
|
|
|
EXPORT_DIRNAME = 'export'
|
2022-04-08 17:07:54 +02:00
|
|
|
|
|
|
|
def self.download_and_zip(procedure, attachments, filename, &block)
|
|
|
|
Dir.mktmpdir(nil, ARCHIVE_CREATION_DIR) do |tmp_dir|
|
2024-03-22 10:05:47 +01:00
|
|
|
export_dir = File.join(tmp_dir, EXPORT_DIRNAME)
|
2022-04-08 17:07:54 +02:00
|
|
|
zip_path = File.join(ARCHIVE_CREATION_DIR, "#{filename}.zip")
|
|
|
|
|
|
|
|
begin
|
|
|
|
FileUtils.remove_entry_secure(export_dir) if Dir.exist?(export_dir)
|
|
|
|
Dir.mkdir(export_dir)
|
|
|
|
|
|
|
|
download_manager = DownloadManager::ProcedureAttachmentsExport.new(procedure, attachments, export_dir)
|
|
|
|
download_manager.download_all
|
|
|
|
|
|
|
|
Dir.chdir(tmp_dir) do
|
|
|
|
File.delete(zip_path) if File.exist?(zip_path)
|
2024-03-22 10:05:47 +01:00
|
|
|
system 'zip', '-0', '-r', zip_path, EXPORT_DIRNAME
|
2022-04-08 17:07:54 +02:00
|
|
|
end
|
|
|
|
yield(zip_path)
|
|
|
|
ensure
|
|
|
|
FileUtils.remove_entry_secure(export_dir) if Dir.exist?(export_dir)
|
|
|
|
File.delete(zip_path) if File.exist?(zip_path)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|