DaniFera commited on
Commit
01441bd
·
verified ·
1 Parent(s): e62963a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -31
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
- # 1. Esperar 30 segundos entre revisiones
22
- time.sleep(30)
23
 
24
- # 2. Configurar umbral de 2 minutos (120 segundos)
25
- LIMIT_MINUTES = 2
26
- limit_seconds = LIMIT_MINUTES * 60
27
 
28
- now = time.time()
29
- cutoff = now - limit_seconds
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
- # Calcular edad del archivo
43
- creation_time = os.path.getmtime(filepath)
44
- age_seconds = now - creation_time
45
- age_minutes = int(age_seconds // 60)
46
- age_secs_remainder = int(age_seconds % 60)
47
-
48
- file_age_str = f"{age_minutes}m {age_secs_remainder}s"
49
 
50
- # Decidir si borrar o mantener
51
- if creation_time < cutoff:
52
  try:
53
  os.remove(filepath)
54
- print(f"❌ [BORRADO] {filename} | Edad: {file_age_str} (> {LIMIT_MINUTES}m)")
 
55
  except Exception as e:
56
- print(f"⚠️ [ERROR] Fallo al borrar {filename}: {e}")
57
  else:
58
- print(f"✅ [VIGENTE] {filename} | Edad: {file_age_str}")
59
- else:
60
- print("[INFO] La carpeta temporal aún no existe.")
61
-
 
 
62
  except Exception as e:
63
- print(f"[CRITICAL] El hilo de limpieza ha fallado: {e}")
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()