diff --git a/app/proxy.py b/app/proxy.py index 981c4d415c734eefef5bfbe858dd45df0bf74d15..ec3758581bddbe9090af46db5f76a8f96837dc7e 100644 --- a/app/proxy.py +++ b/app/proxy.py @@ -57,17 +57,31 @@ def create_zip(files, collection_id): @app.route('/<collection_id>', methods=['GET']) def download_zip(collection_id): try: - if os.path.exists(f"colecao_{collection_id}.zip"): - zip_path = os.path.abspath(f"colecao_{collection_id}.zip") + zip_filename = f"colecao_{collection_id}.zip" + if os.path.exists(zip_filename): + # Check if zip file is empty + with ZipFile(zip_filename, 'r') as zip: + if not zip.namelist(): # If namelist() returns an empty list, the zip is empty + return "Zip is empty", 200 + + # Zip file exists and is not empty, send it + zip_path = os.path.abspath(zip_filename) return send_file(zip_path, as_attachment=True), 200 + ids = get_collection_ids(collection_id) downloaded_files = download_files(ids) + + # Check if downloaded_files is empty + if not downloaded_files: + return "No files to zip", 200 + create_zip(downloaded_files, collection_id) - zip_path = os.path.abspath(f"colecao_{collection_id}.zip") + zip_path = os.path.abspath(zip_filename) response = send_file(zip_path, as_attachment=True) return response, 200 except Exception as e: return str(e), 500 + if __name__ == "__main__": app.run(port=3333)