demarches-normaliennes/app/services/downloadable_file_service.rb

29 lines
1,004 B
Ruby
Raw Normal View History

class DownloadableFileService
ARCHIVE_CREATION_DIR = ENV.fetch('ARCHIVE_CREATION_DIR') { '/tmp' }
2024-03-22 10:05:47 +01:00
EXPORT_DIRNAME = 'export'
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)
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
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