File size: 5,153 Bytes
6b3f2a3
 
1c83fd3
 
824fdfc
9c8d68c
 
1c83fd3
 
ef8a826
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9c8d68c
 
 
d51c8cc
1c83fd3
 
 
 
 
 
 
9c8d68c
d51c8cc
1c83fd3
 
 
 
 
 
9c8d68c
 
 
1c83fd3
 
 
9c8d68c
1c83fd3
 
9c8d68c
 
1c83fd3
 
 
 
 
 
 
 
d51c8cc
a1f1c77
d51c8cc
9c8d68c
a1f1c77
 
8b00905
a1f1c77
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d51c8cc
d0baa2d
 
d51c8cc
d0baa2d
 
 
d51c8cc
d0baa2d
 
 
 
 
 
ef8a826
 
 
d0baa2d
 
 
 
 
 
 
 
ef8a826
 
 
d0baa2d
ef8a826
d0baa2d
 
 
 
 
c275c0c
d0baa2d
 
 
9c8d68c
 
2600a3c
b696593
d0baa2d
b696593
d0baa2d
 
 
 
 
 
 
 
 
 
ef8a826
 
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
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)