Update app.py
Browse files
app.py
CHANGED
|
@@ -12,55 +12,46 @@ engine = PDFEngine()
|
|
| 12 |
|
| 13 |
# --- SEGURIDAD: GARBAGE COLLECTOR CON LOGS (VERBOSE) ---
|
| 14 |
def cleanup_cron():
|
| 15 |
-
"""
|
| 16 |
-
Revisa cada minuto y borra archivos mayores a 5 minutos.
|
| 17 |
-
Muestra el estado en la consola de Hugging Face.
|
| 18 |
-
"""
|
| 19 |
while True:
|
| 20 |
try:
|
| 21 |
-
|
| 22 |
-
time.sleep(30)
|
| 23 |
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
limit_seconds = LIMIT_MINUTES * 60
|
| 27 |
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
print(f"\n--- [SEGURIDAD] Ronda de limpieza: {time.strftime('%H:%M:%S')} ---")
|
| 32 |
|
| 33 |
if os.path.exists(config.TEMP_DIR):
|
| 34 |
files = os.listdir(config.TEMP_DIR)
|
| 35 |
-
if not files:
|
| 36 |
-
print("[ESTADO] Carpeta temporal vacía (0 archivos).")
|
| 37 |
|
| 38 |
for filename in files:
|
| 39 |
filepath = os.path.join(config.TEMP_DIR, filename)
|
| 40 |
|
| 41 |
if os.path.isfile(filepath):
|
| 42 |
-
#
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
file_age_str = f"{age_minutes}m {age_secs_remainder}s"
|
| 49 |
|
| 50 |
-
|
| 51 |
-
if creation_time < cutoff:
|
| 52 |
try:
|
| 53 |
os.remove(filepath)
|
| 54 |
-
|
|
|
|
| 55 |
except Exception as e:
|
| 56 |
-
print(f"⚠️ [ERROR] Fallo al borrar {
|
| 57 |
else:
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
|
|
|
|
|
|
| 62 |
except Exception as e:
|
| 63 |
-
print(f"[CRITICAL]
|
| 64 |
|
| 65 |
# Iniciar el hilo "chivato"
|
| 66 |
threading.Thread(target=cleanup_cron, daemon=True).start()
|
|
|
|
| 12 |
|
| 13 |
# --- SEGURIDAD: GARBAGE COLLECTOR CON LOGS (VERBOSE) ---
|
| 14 |
def cleanup_cron():
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
while True:
|
| 16 |
try:
|
| 17 |
+
time.sleep(60) # Revisar cada minuto
|
|
|
|
| 18 |
|
| 19 |
+
LIMIT_MINUTES = 5
|
| 20 |
+
cutoff = time.time() - (LIMIT_MINUTES * 60)
|
|
|
|
| 21 |
|
| 22 |
+
# Solo imprimimos cabecera si hay actividad para no ensuciar el log
|
| 23 |
+
# print(f"--- [SEGURIDAD] Ronda... ---") # Comentado para reducir ruido
|
|
|
|
|
|
|
| 24 |
|
| 25 |
if os.path.exists(config.TEMP_DIR):
|
| 26 |
files = os.listdir(config.TEMP_DIR)
|
|
|
|
|
|
|
| 27 |
|
| 28 |
for filename in files:
|
| 29 |
filepath = os.path.join(config.TEMP_DIR, filename)
|
| 30 |
|
| 31 |
if os.path.isfile(filepath):
|
| 32 |
+
# OFUSCACIÓN: Mostramos solo los primeros 4 caracteres y la extensión
|
| 33 |
+
# Ejemplo: "a1b2c3d4_dni.pdf" -> "a1b2****.pdf"
|
| 34 |
+
if len(filename) > 8:
|
| 35 |
+
masked_name = f"{filename[:4]}****{os.path.splitext(filename)[1]}"
|
| 36 |
+
else:
|
| 37 |
+
masked_name = "****" + os.path.splitext(filename)[1]
|
|
|
|
| 38 |
|
| 39 |
+
if os.path.getmtime(filepath) < cutoff:
|
|
|
|
| 40 |
try:
|
| 41 |
os.remove(filepath)
|
| 42 |
+
# Log seguro: Informa de la acción pero no del dato
|
| 43 |
+
print(f"🛡️ [SEC-CLEAN] Archivo expirado eliminado: {masked_name}")
|
| 44 |
except Exception as e:
|
| 45 |
+
print(f"⚠️ [ERROR] Fallo al borrar {masked_name}")
|
| 46 |
else:
|
| 47 |
+
# Opcional: No imprimir nada para los vigentes en producción
|
| 48 |
+
# para no dar pistas de CUÁNDO hay tráfico.
|
| 49 |
+
# Si quieres verlos, usa el nombre enmascarado:
|
| 50 |
+
# print(f"👁️ [MONITOR] Archivo activo: {masked_name}")
|
| 51 |
+
pass
|
| 52 |
+
|
| 53 |
except Exception as e:
|
| 54 |
+
print(f"[CRITICAL] Error en limpieza: {e}")
|
| 55 |
|
| 56 |
# Iniciar el hilo "chivato"
|
| 57 |
threading.Thread(target=cleanup_cron, daemon=True).start()
|