Skip to content
Snippets Groups Projects

Issue #1: ADD check if there any files to zip

Merged Richard Fernando Heise Ferreira requested to merge issue-1/check-zip-empty into master
1 file
+ 17
3
Compare changes
  • Side-by-side
  • Inline
+ 17
3
@@ -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)
Loading