files / app.py
pcdoido2's picture
Update app.py
c275c0c verified
raw
history blame
5.15 kB
import streamlit as st
import os
import json
import time
BASE_FOLDER = "uploaded_files"
CATEGORIES = ["AVATAR WORLD", "BLOX FRUITS", "TOCA LIFE"]
EXPIRATION_FILE = "expirations.json"
# --- Estilos CSS ---
st.markdown("""
<style>
.file-box {
border: 1px solid #ccc;
padding: 10px;
margin-bottom: 10px;
border-radius: 5px;
background-color: #f9f9f9;
}
.file-name {
font-size: 18px;
font-weight: bold;
}
.expire-text {
font-size: 14px;
color: #555;
}
</style>
""", unsafe_allow_html=True)
# Cria as pastas se não existirem
for cat in CATEGORIES:
os.makedirs(os.path.join(BASE_FOLDER, cat), 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 por Categoria")
# --- Função: apagar arquivos expirados ---
def remove_expired_files():
changed = False
now = time.time()
expired_files = []
for file_full, expire_time in list(expirations.items()):
cat, file = file_full.split("|||")
file_path = os.path.join(BASE_FOLDER, cat, file)
if now > expire_time:
if os.path.exists(file_path):
os.remove(file_path)
expired_files.append(file_full)
changed = True
for file_full in expired_files:
expirations.pop(file_full)
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")
st.subheader("Selecione uma categoria:")
categoria = st.radio("Categoria:", CATEGORIES, index=None)
if categoria:
uploaded_files = st.file_uploader(
f"Selecione arquivos para '{categoria}'",
accept_multiple_files=True,
key=f"uploader_{categoria}"
)
auto_delete = st.checkbox("Excluir automaticamente após 24 horas")
if uploaded_files:
for uploaded_file in uploaded_files:
folder = os.path.join(BASE_FOLDER, categoria)
file_path = os.path.join(folder, uploaded_file.name)
with open(file_path, "wb") as f:
f.write(uploaded_file.read())
# Salvar expiração se marcada
if auto_delete:
key = f"{categoria}|||{uploaded_file.name}"
expirations[key] = time.time() + 24 * 60 * 60
with open(EXPIRATION_FILE, "w") as f:
json.dump(expirations, f)
st.success("Arquivos enviados com sucesso!")
st.rerun()
# --- Lista de Arquivos agrupada por pasta ---
st.header("📄 Arquivos Disponíveis")
for categoria in CATEGORIES:
folder = os.path.join(BASE_FOLDER, categoria)
files = os.listdir(folder)
st.subheader(f"📁 {categoria}")
if not files:
st.info("Nenhum arquivo na categoria.")
else:
for file in files:
st.markdown(f"<div class='file-box'>", unsafe_allow_html=True)
st.markdown(f"<div class='file-name'>{file}</div>", unsafe_allow_html=True)
key = f"{categoria}|||{file}"
expira = expirations.get(key)
if expira:
restante = int(expira - time.time())
if restante > 0:
restante_horas = restante // 3600
restante_min = (restante % 3600) // 60
st.markdown(
f"<div class='expire-text'>⏳ Expira em {restante_horas}h {restante_min}min</div>",
unsafe_allow_html=True)
else:
st.markdown("<div class='expire-text'>⚠ Expiração iminente</div>", unsafe_allow_html=True)
col1, col2, col3 = st.columns(3)
with col1:
with open(os.path.join(folder, file), "rb") as f_obj:
st.download_button("⬇ Download", f_obj, file_name=file, key=f"down_{categoria}_{file}")
with col2:
if st.button("🗑 Excluir", key=f"delete_{categoria}_{file}"):
os.remove(os.path.join(folder, file))
expirations.pop(key, None)
with open(EXPIRATION_FILE, "w") as f:
json.dump(expirations, f)
st.success(f"Arquivo '{file}' excluído.")
st.rerun()
with col3:
with open(os.path.join(folder, file), "rb") as f_obj:
if st.download_button("⬇ Baixar & Apagar", f_obj, file_name=file, key=f"download_delete_{categoria}_{file}"):
os.remove(os.path.join(folder, file))
expirations.pop(key, None)
with open(EXPIRATION_FILE, "w") as f:
json.dump(expirations, f)
st.success(f"Arquivo '{file}' baixado e removido.")
st.rerun()
st.markdown("</div>", unsafe_allow_html=True)