Actualizar app/main.py
Borrar archivos después de bajarlos
This commit is contained in:
35
app/main.py
35
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)
|
||||
Reference in New Issue
Block a user