From c06a4f7b91248f204d53e7ccd062f16a0db6d607 Mon Sep 17 00:00:00 2001 From: marti Date: Tue, 8 Jul 2025 16:13:20 +0000 Subject: [PATCH] Actualizar app/main.py MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Borrar archivos después de bajarlos --- app/main.py | 35 +++++++++++++++++++++++++---------- 1 file changed, 25 insertions(+), 10 deletions(-) diff --git a/app/main.py b/app/main.py index 5eb5b49..7670dda 100644 --- a/app/main.py +++ b/app/main.py @@ -1,4 +1,4 @@ -from flask import Flask, render_template, request, send_file +from flask import Flask, render_template, request, send_file, after_this_request import subprocess import os import uuid @@ -39,12 +39,23 @@ def index(): try: subprocess.run(cmd, check=True) - # Buscar el archivo descargado (único archivo en carpeta) files = os.listdir(folder_path) if not files: return render_template("index.html", error="No se pudo descargar el archivo.") downloaded_file_path = os.path.join(folder_path, files[0]) + + @after_this_request + def cleanup(response): + try: + if os.path.exists(downloaded_file_path): + os.remove(downloaded_file_path) + if os.path.exists(folder_path): + os.rmdir(folder_path) + except Exception as e: + print(f"Error al borrar archivos: {e}") + return response + return send_file(downloaded_file_path, as_attachment=True, download_name=files[0]) except subprocess.CalledProcessError: @@ -64,12 +75,8 @@ def index(): check=True, text=True ) - #playlist_title = result.stdout.strip() playlist_title = result.stdout.strip().splitlines()[0] - # Limpiar el nombre para que sea válido como nombre de archivo playlist_title_clean = "".join(c for c in playlist_title if c.isalnum() or c in " _-").strip().replace(" ", "_") - print(f"Título original: {playlist_title}") - print(f"Título limpio: {playlist_title_clean}") except subprocess.CalledProcessError: return render_template("index.html", error="Error al obtener el título de la playlist.") @@ -84,11 +91,20 @@ def index(): try: subprocess.run(cmd, check=True) - # Comprimir en ZIP zip_path = shutil.make_archive(folder_path, 'zip', folder_path) zip_filename = f"{playlist_title_clean}.zip" - # Enviar al navegador + @after_this_request + def cleanup(response): + try: + if os.path.exists(folder_path): + shutil.rmtree(folder_path) + if os.path.exists(zip_path): + os.remove(zip_path) + except Exception as e: + print(f"Error al borrar archivos: {e}") + return response + return send_file(zip_path, as_attachment=True, download_name=zip_filename, mimetype='application/zip') except subprocess.CalledProcessError: @@ -100,5 +116,4 @@ def index(): return render_template("index.html") if __name__ == "__main__": - app.run(host="0.0.0.0", port=5000) - + app.run(host="0.0.0.0", port=5000) \ No newline at end of file