From 4cffd09f8712aacce45a898eec1fec9aa6045b5a Mon Sep 17 00:00:00 2001 From: drorganvidez Date: Fri, 29 Nov 2024 14:32:21 +0100 Subject: [PATCH] fix: Problem downloading all datasets A bug meant that no temporary zip folder was deleted. This caused the server to crash in production. --- app/modules/dataset/routes.py | 26 ++++++++++++++++++-------- app/modules/dataset/services.py | 6 +----- 2 files changed, 19 insertions(+), 13 deletions(-) diff --git a/app/modules/dataset/routes.py b/app/modules/dataset/routes.py index 759059359..249e8ce25 100644 --- a/app/modules/dataset/routes.py +++ b/app/modules/dataset/routes.py @@ -3,6 +3,7 @@ import os import json import shutil +import tempfile from flask import ( abort, @@ -200,15 +201,24 @@ def download_dataset(dataset_id): @dataset_bp.route("/dataset/download/all", methods=["GET"]) def download_all_dataset(): - zip_path = dataset_service.zip_all_datasets() + # Crear un directorio temporal + temp_dir = tempfile.mkdtemp() + zip_path = os.path.join(temp_dir, "all_datasets.zip") - # Obtener la fecha actual en el formato deseado (por ejemplo, YYYYMMDD) - current_date = datetime.now().strftime("%Y_%m_%d") - - # Crear el nombre del archivo con la fecha - zip_filename = f"uvlhub_bulk_{current_date}.zip" - - return send_file(zip_path, as_attachment=True, download_name=zip_filename) + try: + # Generar el archivo ZIP + dataset_service.zip_all_datasets(zip_path) + + # Crear el nombre del archivo con la fecha + current_date = datetime.now().strftime("%Y_%m_%d") + zip_filename = f"uvlhub_bulk_{current_date}.zip" + + # Enviar el archivo como respuesta + return send_file(zip_path, as_attachment=True, download_name=zip_filename) + finally: + # Asegurar que la carpeta temporal se elimine después de que Flask sirva el archivo + if os.path.exists(temp_dir): + shutil.rmtree(temp_dir) @dataset_bp.route("/doi//", methods=["GET"]) diff --git a/app/modules/dataset/services.py b/app/modules/dataset/services.py index 7a7278835..4ed6cc2dc 100644 --- a/app/modules/dataset/services.py +++ b/app/modules/dataset/services.py @@ -302,10 +302,7 @@ def zip_dataset(self, dataset: DataSet) -> str: return temp_dir - def zip_all_datasets(self) -> str: - temp_dir = tempfile.mkdtemp() - zip_path = os.path.join(temp_dir, "all_datasets.zip") - + def zip_all_datasets(self, zip_path: str): with ZipFile(zip_path, "w") as zipf: for user_dir in os.listdir("uploads"): user_path = os.path.join("uploads", user_dir) @@ -327,7 +324,6 @@ def zip_all_datasets(self) -> str: full_path, arcname=os.path.join(dataset_dir, relative_path), ) - return zip_path class AuthorService(BaseService):