File size: 3,710 Bytes
6b3f2a3
 
 
1c83fd3
 
 
824fdfc
d51c8cc
1c83fd3
 
d51c8cc
 
1c83fd3
 
 
 
 
 
 
d51c8cc
 
1c83fd3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d51c8cc
 
 
1c83fd3
d51c8cc
 
 
 
 
 
1c83fd3
 
 
 
 
 
 
d51c8cc
1c83fd3
d51c8cc
 
 
 
 
 
 
 
 
 
2600a3c
d51c8cc
1c83fd3
 
 
 
 
 
 
 
 
 
 
b696593
d51c8cc
2600a3c
b696593
 
d51c8cc
 
 
1c83fd3
 
 
d51c8cc
1c83fd3
b696593
2600a3c
 
 
 
 
 
b696593
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
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()