|
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" |
|
|
|
|
|
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) |
|
|
|
|
|
for cat in CATEGORIES: |
|
os.makedirs(os.path.join(BASE_FOLDER, cat), exist_ok=True) |
|
|
|
|
|
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") |
|
|
|
|
|
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) |
|
|
|
|
|
remove_expired_files() |
|
|
|
|
|
|
|
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()) |
|
|
|
|
|
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() |
|
|
|
|
|
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) |
|
|