pcdoido2 commited on
Commit
9c8d68c
·
verified ·
1 Parent(s): 02d5837

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -24
app.py CHANGED
@@ -4,10 +4,13 @@ import shutil
4
  import json
5
  import time
6
 
7
- UPLOAD_FOLDER = "uploaded_files"
 
8
  EXPIRATION_FILE = "expirations.json"
9
 
10
- os.makedirs(UPLOAD_FOLDER, exist_ok=True)
 
 
11
 
12
  # Carrega dados de expiração
13
  if os.path.exists(EXPIRATION_FILE):
@@ -16,7 +19,7 @@ if os.path.exists(EXPIRATION_FILE):
16
  else:
17
  expirations = {}
18
 
19
- st.title("📂 File Manager Simples")
20
 
21
  # --- Função: apagar arquivos expirados ---
22
  def remove_expired_files():
@@ -24,16 +27,17 @@ def remove_expired_files():
24
  now = time.time()
25
  expired_files = []
26
 
27
- for file, expire_time in list(expirations.items()):
 
 
28
  if now > expire_time:
29
- file_path = os.path.join(UPLOAD_FOLDER, file)
30
  if os.path.exists(file_path):
31
  os.remove(file_path)
32
- expired_files.append(file)
33
  changed = True
34
 
35
- for file in expired_files:
36
- expirations.pop(file)
37
 
38
  if changed:
39
  with open(EXPIRATION_FILE, "w") as f:
@@ -44,18 +48,22 @@ remove_expired_files()
44
 
45
  # --- Upload ---
46
  st.header("📤 Upload de Arquivos")
 
 
47
  uploaded_files = st.file_uploader("Selecione arquivos", accept_multiple_files=True)
48
  auto_delete = st.checkbox("Excluir automaticamente após 24 horas")
49
 
50
  if uploaded_files:
51
  for uploaded_file in uploaded_files:
52
- file_path = os.path.join(UPLOAD_FOLDER, uploaded_file.name)
 
53
  with open(file_path, "wb") as f:
54
  f.write(uploaded_file.read())
55
 
56
- # Se marcado, registra expiração para 24h depois
57
  if auto_delete:
58
- expirations[uploaded_file.name] = time.time() + 24 * 60 * 60 # 24h em segundos
 
59
  with open(EXPIRATION_FILE, "w") as f:
60
  json.dump(expirations, f)
61
 
@@ -63,9 +71,10 @@ if uploaded_files:
63
  st.rerun()
64
 
65
  # --- Lista de Arquivos ---
66
- st.header("📄 Arquivos Disponíveis")
67
 
68
- files = os.listdir(UPLOAD_FOLDER)
 
69
 
70
  if not files:
71
  st.info("Nenhum arquivo disponível.")
@@ -73,7 +82,9 @@ else:
73
  for file in files:
74
  st.subheader(f"📄 {file}")
75
 
76
- expira = expirations.get(file)
 
 
77
  if expira:
78
  restante = int(expira - time.time())
79
  if restante > 0:
@@ -83,27 +94,26 @@ else:
83
  else:
84
  st.write("⚠ Expiração iminente")
85
 
86
- # Botões abaixo do nome do arquivo
87
  col1, col2, col3 = st.columns(3)
88
 
89
  with col1:
90
- with open(os.path.join(UPLOAD_FOLDER, file), "rb") as f_obj:
91
- st.download_button("⬇ Download", f_obj, file_name=file, key=f"down_{file}")
92
 
93
  with col2:
94
- if st.button("🗑 Excluir", key=f"delete_{file}"):
95
- os.remove(os.path.join(UPLOAD_FOLDER, file))
96
- expirations.pop(file, None)
97
  with open(EXPIRATION_FILE, "w") as f:
98
  json.dump(expirations, f)
99
  st.success(f"Arquivo '{file}' excluído.")
100
  st.rerun()
101
 
102
  with col3:
103
- with open(os.path.join(UPLOAD_FOLDER, file), "rb") as f_obj:
104
- if st.download_button("⬇ Baixar & Apagar", f_obj, file_name=file, key=f"download_delete_{file}"):
105
- os.remove(os.path.join(UPLOAD_FOLDER, file))
106
- expirations.pop(file, None)
107
  with open(EXPIRATION_FILE, "w") as f:
108
  json.dump(expirations, f)
109
  st.success(f"Arquivo '{file}' baixado e removido.")
 
4
  import json
5
  import time
6
 
7
+ BASE_FOLDER = "uploaded_files"
8
+ CATEGORIES = ["AVATAR WORLD", "BLOX FRUITS", "TOCA LIFE"]
9
  EXPIRATION_FILE = "expirations.json"
10
 
11
+ # Cria as pastas se não existirem
12
+ for cat in CATEGORIES:
13
+ os.makedirs(os.path.join(BASE_FOLDER, cat), exist_ok=True)
14
 
15
  # Carrega dados de expiração
16
  if os.path.exists(EXPIRATION_FILE):
 
19
  else:
20
  expirations = {}
21
 
22
+ st.title("📂 File Manager por Categoria")
23
 
24
  # --- Função: apagar arquivos expirados ---
25
  def remove_expired_files():
 
27
  now = time.time()
28
  expired_files = []
29
 
30
+ for file_full, expire_time in list(expirations.items()):
31
+ cat, file = file_full.split("|||")
32
+ file_path = os.path.join(BASE_FOLDER, cat, file)
33
  if now > expire_time:
 
34
  if os.path.exists(file_path):
35
  os.remove(file_path)
36
+ expired_files.append(file_full)
37
  changed = True
38
 
39
+ for file_full in expired_files:
40
+ expirations.pop(file_full)
41
 
42
  if changed:
43
  with open(EXPIRATION_FILE, "w") as f:
 
48
 
49
  # --- Upload ---
50
  st.header("📤 Upload de Arquivos")
51
+
52
+ categoria = st.selectbox("Selecione a categoria", CATEGORIES)
53
  uploaded_files = st.file_uploader("Selecione arquivos", accept_multiple_files=True)
54
  auto_delete = st.checkbox("Excluir automaticamente após 24 horas")
55
 
56
  if uploaded_files:
57
  for uploaded_file in uploaded_files:
58
+ folder = os.path.join(BASE_FOLDER, categoria)
59
+ file_path = os.path.join(folder, uploaded_file.name)
60
  with open(file_path, "wb") as f:
61
  f.write(uploaded_file.read())
62
 
63
+ # Salvar expiração se marcada
64
  if auto_delete:
65
+ key = f"{categoria}|||{uploaded_file.name}"
66
+ expirations[key] = time.time() + 24 * 60 * 60
67
  with open(EXPIRATION_FILE, "w") as f:
68
  json.dump(expirations, f)
69
 
 
71
  st.rerun()
72
 
73
  # --- Lista de Arquivos ---
74
+ st.header(f"📄 Arquivos em {categoria}")
75
 
76
+ folder = os.path.join(BASE_FOLDER, categoria)
77
+ files = os.listdir(folder)
78
 
79
  if not files:
80
  st.info("Nenhum arquivo disponível.")
 
82
  for file in files:
83
  st.subheader(f"📄 {file}")
84
 
85
+ key = f"{categoria}|||{file}"
86
+ expira = expirations.get(key)
87
+
88
  if expira:
89
  restante = int(expira - time.time())
90
  if restante > 0:
 
94
  else:
95
  st.write("⚠ Expiração iminente")
96
 
 
97
  col1, col2, col3 = st.columns(3)
98
 
99
  with col1:
100
+ with open(os.path.join(folder, file), "rb") as f_obj:
101
+ st.download_button("⬇ Download", f_obj, file_name=file, key=f"down_{categoria}_{file}")
102
 
103
  with col2:
104
+ if st.button("🗑 Excluir", key=f"delete_{categoria}_{file}"):
105
+ os.remove(os.path.join(folder, file))
106
+ expirations.pop(key, None)
107
  with open(EXPIRATION_FILE, "w") as f:
108
  json.dump(expirations, f)
109
  st.success(f"Arquivo '{file}' excluído.")
110
  st.rerun()
111
 
112
  with col3:
113
+ with open(os.path.join(folder, file), "rb") as f_obj:
114
+ if st.download_button("⬇ Baixar & Apagar", f_obj, file_name=file, key=f"download_delete_{categoria}_{file}"):
115
+ os.remove(os.path.join(folder, file))
116
+ expirations.pop(key, None)
117
  with open(EXPIRATION_FILE, "w") as f:
118
  json.dump(expirations, f)
119
  st.success(f"Arquivo '{file}' baixado e removido.")