def _generate_zip_file(self, files_holder, name_prefix='material', name_suffix=None): """Generate a zip file containing the files passed. :param files_holder: An iterable (or an iterable containing) object that contains the files to be added in the zip file. :param name_prefix: The prifix to the zip file name :param name_suffix: The suffix to the zip file name :return: The generated zip file. """ from indico.util.tasks import delete_file temp_file = NamedTemporaryFile(suffix='indico.tmp', dir=Config.getInstance().getTempDir()) with ZipFile(temp_file.name, 'w', allowZip64=True) as zip_handler: self.used_filenames = set() for item in self._iter_items(files_holder): name = self._prepare_folder_structure(item) self.used_filenames.add(name) with item.storage.get_local_path(item.storage_file_id) as filepath: zip_handler.write(filepath.encode('utf-8'), name) # Delete the temporary file after some time. Even for a large file we don't # need a higher delay since the webserver will keep it open anyway until it's # done sending it to the client. delete_file.apply_async(args=[temp_file.name], countdown=3600) temp_file.delete = False zip_file_name = '{}-{}.zip'.format(name_prefix, name_suffix) if name_suffix else '{}.zip'.format(name_prefix) return send_file(zip_file_name, temp_file.name, 'application/zip', inline=False)
def _generate_zip_file(attachments, regform): temp_file = NamedTemporaryFile(suffix='indico.tmp', dir=Config.getInstance().getTempDir()) with ZipFile(temp_file.name, 'w', allowZip64=True) as zip_handler: for reg_attachments in attachments.itervalues(): for reg_attachment in reg_attachments: name = _prepare_folder_structure(reg_attachment) with reg_attachment.storage.get_local_path(reg_attachment.storage_file_id) as filepath: zip_handler.write(filepath, name) # Delete the temporary file after some time. Even for a large file we don't # need a higher delay since the webserver will keep it open anyway until it's # done sending it to the client. delete_file.apply_async(args=[temp_file.name], countdown=3600) temp_file.delete = False return send_file('attachments-{}.zip'.format(regform.id), temp_file.name, 'application/zip', inline=False)