Skip to content
Snippets Groups Projects
Commit 971e451d authored by Richard Fernando Heise Ferreira's avatar Richard Fernando Heise Ferreira
Browse files

Merge branch 'issue-1/check-zip-empty' into 'master'

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

See merge request !1
parents 321bdca7 becf65b0
No related branches found
No related tags found
1 merge request!1Issue #1: ADD check if there any files to zip
...@@ -57,17 +57,31 @@ def create_zip(files, collection_id): ...@@ -57,17 +57,31 @@ def create_zip(files, collection_id):
@app.route('/<collection_id>', methods=['GET']) @app.route('/<collection_id>', methods=['GET'])
def download_zip(collection_id): def download_zip(collection_id):
try: try:
if os.path.exists(f"colecao_{collection_id}.zip"): zip_filename = f"colecao_{collection_id}.zip"
zip_path = os.path.abspath(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 return send_file(zip_path, as_attachment=True), 200
ids = get_collection_ids(collection_id) ids = get_collection_ids(collection_id)
downloaded_files = download_files(ids) 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) 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) response = send_file(zip_path, as_attachment=True)
return response, 200 return response, 200
except Exception as e: except Exception as e:
return str(e), 500 return str(e), 500
if __name__ == "__main__": if __name__ == "__main__":
app.run(port=3333) app.run(port=3333)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment