files / app.py
pcdoido2's picture
Update app.py
b696593 verified
raw
history blame
3.71 kB
import streamlit as st
import os
import shutil
import json
import time
from datetime import datetime, timedelta
UPLOAD_FOLDER = "uploaded_files"
EXPIRATION_FILE = "expirations.json"
os.makedirs(UPLOAD_FOLDER, exist_ok=True)
# Carrega dados de expiração
if os.path.exists(EXPIRATION_FILE):
with open(EXPIRATION_FILE, "r") as f:
expirations = json.load(f)
else:
expirations = {}
st.title("📂 File Manager Simples")
# --- Função: apagar arquivos expirados ---
def remove_expired_files():
changed = False
now = time.time()
expired_files = []
for file, expire_time in list(expirations.items()):
if now > expire_time:
file_path = os.path.join(UPLOAD_FOLDER, file)
if os.path.exists(file_path):
os.remove(file_path)
expired_files.append(file)
changed = True
for file in expired_files:
expirations.pop(file)
if changed:
with open(EXPIRATION_FILE, "w") as f:
json.dump(expirations, f)
# --- Apagar arquivos vencidos ao iniciar ---
remove_expired_files()
# --- Upload ---
st.header("📤 Upload de Arquivos")
uploaded_files = st.file_uploader("Selecione arquivos", accept_multiple_files=True)
auto_delete = st.checkbox("Excluir automaticamente após 24 horas")
if uploaded_files:
for uploaded_file in uploaded_files:
file_path = os.path.join(UPLOAD_FOLDER, uploaded_file.name)
with open(file_path, "wb") as f:
f.write(uploaded_file.read())
# Se marcado, registra expiração para 24h depois
if auto_delete:
expirations[uploaded_file.name] = time.time() + 24 * 60 * 60 # 24h em segundos
with open(EXPIRATION_FILE, "w") as f:
json.dump(expirations, f)
st.success("Arquivos enviados com sucesso!")
st.rerun()
# --- Lista de Arquivos ---
st.header("📄 Arquivos Disponíveis")
files = os.listdir(UPLOAD_FOLDER)
if not files:
st.info("Nenhum arquivo disponível.")
else:
for file in files:
col1, col2, col3, col4 = st.columns([4, 1, 1, 2])
with col1:
expira = expirations.get(file)
if expira:
restante = int(expira - time.time())
if restante > 0:
restante_horas = restante // 3600
restante_min = (restante % 3600) // 60
st.write(f"{file} (expira em {restante_horas}h {restante_min}min)")
else:
st.write(f"{file} (expiração iminente)")
else:
st.write(file)
with col2:
with open(os.path.join(UPLOAD_FOLDER, file), "rb") as f_obj:
st.download_button("⬇ Download", f_obj, file_name=file, key=f"down_{file}")
with col3:
if st.button("🗑 Excluir", key=f"delete_{file}"):
os.remove(os.path.join(UPLOAD_FOLDER, file))
expirations.pop(file, None)
with open(EXPIRATION_FILE, "w") as f:
json.dump(expirations, f)
st.success(f"Arquivo '{file}' excluído.")
st.rerun()
with col4:
with open(os.path.join(UPLOAD_FOLDER, file), "rb") as f_obj:
if st.download_button("⬇ Baixar & Apagar", f_obj, file_name=file, key=f"download_delete_{file}"):
os.remove(os.path.join(UPLOAD_FOLDER, file))
expirations.pop(file, None)
with open(EXPIRATION_FILE, "w") as f:
json.dump(expirations, f)
st.success(f"Arquivo '{file}' baixado e removido.")
st.rerun()