pcdoido2 commited on
Commit
1c83fd3
·
verified ·
1 Parent(s): 9333e34

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +60 -3
app.py CHANGED
@@ -1,23 +1,67 @@
1
  import streamlit as st
2
  import os
3
  import shutil
 
 
 
4
 
5
  UPLOAD_FOLDER = "uploaded_files"
 
 
6
  os.makedirs(UPLOAD_FOLDER, exist_ok=True)
7
 
 
 
 
 
 
 
 
8
  st.title("📂 File Manager Simples")
9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
10
  # --- Upload ---
11
  st.header("📤 Upload de Arquivos")
12
  uploaded_files = st.file_uploader("Selecione arquivos", accept_multiple_files=True)
 
13
 
14
  if uploaded_files:
15
  for uploaded_file in uploaded_files:
16
  file_path = os.path.join(UPLOAD_FOLDER, uploaded_file.name)
17
  with open(file_path, "wb") as f:
18
  f.write(uploaded_file.read())
 
 
 
 
 
 
 
19
  st.success("Arquivos enviados com sucesso!")
20
- st.rerun() # ← correção
21
 
22
  # --- Lista de Arquivos ---
23
  st.header("📄 Arquivos Disponíveis")
@@ -30,12 +74,25 @@ else:
30
  for file in files:
31
  col1, col2, col3 = st.columns([4, 1, 1])
32
  with col1:
33
- st.write(file)
 
 
 
 
 
 
 
 
 
 
34
  with col2:
35
  with open(os.path.join(UPLOAD_FOLDER, file), "rb") as f:
36
  st.download_button("⬇ Download", f, file_name=file)
37
  with col3:
38
  if st.button("🗑 Excluir", key=f"delete_{file}"):
39
  os.remove(os.path.join(UPLOAD_FOLDER, file))
 
 
 
40
  st.success(f"Arquivo '{file}' excluído.")
41
- st.rerun() # ← correção aqui também
 
1
  import streamlit as st
2
  import os
3
  import shutil
4
+ import json
5
+ import time
6
+ from datetime import datetime, timedelta
7
 
8
  UPLOAD_FOLDER = "uploaded_files"
9
+ EXPIRATION_FILE = "expirations.json"
10
+
11
  os.makedirs(UPLOAD_FOLDER, exist_ok=True)
12
 
13
+ # Carrega dados de expiração
14
+ if os.path.exists(EXPIRATION_FILE):
15
+ with open(EXPIRATION_FILE, "r") as f:
16
+ expirations = json.load(f)
17
+ else:
18
+ expirations = {}
19
+
20
  st.title("📂 File Manager Simples")
21
 
22
+ # --- Função: apagar arquivos expirados ---
23
+ def remove_expired_files():
24
+ changed = False
25
+ now = time.time()
26
+ expired_files = []
27
+
28
+ for file, expire_time in list(expirations.items()):
29
+ if now > expire_time:
30
+ file_path = os.path.join(UPLOAD_FOLDER, file)
31
+ if os.path.exists(file_path):
32
+ os.remove(file_path)
33
+ expired_files.append(file)
34
+ changed = True
35
+
36
+ for file in expired_files:
37
+ expirations.pop(file)
38
+
39
+ if changed:
40
+ with open(EXPIRATION_FILE, "w") as f:
41
+ json.dump(expirations, f)
42
+
43
+ # --- Apagar arquivos vencidos ao iniciar ---
44
+ remove_expired_files()
45
+
46
  # --- Upload ---
47
  st.header("📤 Upload de Arquivos")
48
  uploaded_files = st.file_uploader("Selecione arquivos", accept_multiple_files=True)
49
+ auto_delete = st.checkbox("Excluir automaticamente após 24 horas")
50
 
51
  if uploaded_files:
52
  for uploaded_file in uploaded_files:
53
  file_path = os.path.join(UPLOAD_FOLDER, uploaded_file.name)
54
  with open(file_path, "wb") as f:
55
  f.write(uploaded_file.read())
56
+
57
+ # Se marcado, registra expiração para 24h depois
58
+ if auto_delete:
59
+ expirations[uploaded_file.name] = time.time() + 24 * 60 * 60 # 24h em segundos
60
+ with open(EXPIRATION_FILE, "w") as f:
61
+ json.dump(expirations, f)
62
+
63
  st.success("Arquivos enviados com sucesso!")
64
+ st.rerun()
65
 
66
  # --- Lista de Arquivos ---
67
  st.header("📄 Arquivos Disponíveis")
 
74
  for file in files:
75
  col1, col2, col3 = st.columns([4, 1, 1])
76
  with col1:
77
+ expira = expirations.get(file)
78
+ if expira:
79
+ restante = int(expira - time.time())
80
+ if restante > 0:
81
+ restante_horas = restante // 3600
82
+ restante_min = (restante % 3600) // 60
83
+ st.write(f"{file} (expira em {restante_horas}h {restante_min}min)")
84
+ else:
85
+ st.write(f"{file} (expiração iminente)")
86
+ else:
87
+ st.write(file)
88
  with col2:
89
  with open(os.path.join(UPLOAD_FOLDER, file), "rb") as f:
90
  st.download_button("⬇ Download", f, file_name=file)
91
  with col3:
92
  if st.button("🗑 Excluir", key=f"delete_{file}"):
93
  os.remove(os.path.join(UPLOAD_FOLDER, file))
94
+ expirations.pop(file, None)
95
+ with open(EXPIRATION_FILE, "w") as f:
96
+ json.dump(expirations, f)
97
  st.success(f"Arquivo '{file}' excluído.")
98
+ st.rerun()