|
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) |
|
|
|
|
|
if os.path.exists(EXPIRATION_FILE): |
|
with open(EXPIRATION_FILE, "r") as f: |
|
expirations = json.load(f) |
|
else: |
|
expirations = {} |
|
|
|
st.title("📂 File Manager Simples") |
|
|
|
|
|
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) |
|
|
|
|
|
remove_expired_files() |
|
|
|
|
|
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()) |
|
|
|
|
|
if auto_delete: |
|
expirations[uploaded_file.name] = 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") |
|
|
|
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() |
|
|