Spaces:
Sleeping
Sleeping
Delete app.py
Browse files
app.py
DELETED
@@ -1,217 +0,0 @@
|
|
1 |
-
import gradio as gr
|
2 |
-
import os
|
3 |
-
import time
|
4 |
-
from functools import partial
|
5 |
-
from helper import (
|
6 |
-
create_user, verify_user, get_user_files, upload_file, download_file,
|
7 |
-
delete_file, empty_vault, is_admin, get_all_accounts, delete_account,
|
8 |
-
encrypt_file, decrypt_file, is_rate_limited, is_account_locked, record_login_attempt
|
9 |
-
)
|
10 |
-
|
11 |
-
# Constants
|
12 |
-
MAX_FILE_SIZE = 5 * 1024 * 1024 * 1024 # 5GB in bytes
|
13 |
-
INACTIVITY_TIMEOUT = 300 # 5 minutes in seconds
|
14 |
-
|
15 |
-
# Global variables
|
16 |
-
current_user = None
|
17 |
-
last_activity_time = time.time()
|
18 |
-
|
19 |
-
def check_inactivity():
|
20 |
-
global current_user, last_activity_time
|
21 |
-
if current_user and time.time() - last_activity_time > INACTIVITY_TIMEOUT:
|
22 |
-
logout()
|
23 |
-
return True
|
24 |
-
return False
|
25 |
-
|
26 |
-
def update_activity():
|
27 |
-
global last_activity_time
|
28 |
-
last_activity_time = time.time()
|
29 |
-
|
30 |
-
def login(username, password):
|
31 |
-
global current_user
|
32 |
-
if check_inactivity():
|
33 |
-
return gr.update(visible=True), gr.update(visible=False), "You have been logged out due to inactivity."
|
34 |
-
|
35 |
-
if is_rate_limited(username):
|
36 |
-
return gr.update(visible=True), gr.update(visible=False), "Too many login attempts. Please try again later."
|
37 |
-
|
38 |
-
if is_account_locked(username):
|
39 |
-
return gr.update(visible=True), gr.update(visible=False), "Account is locked. Please try again later."
|
40 |
-
|
41 |
-
if verify_user(username, password):
|
42 |
-
current_user = username
|
43 |
-
update_activity()
|
44 |
-
record_login_attempt(username, True)
|
45 |
-
return gr.update(visible=False), gr.update(visible=True), f"Welcome, {username}!"
|
46 |
-
else:
|
47 |
-
record_login_attempt(username, False)
|
48 |
-
return gr.update(visible=True), gr.update(visible=False), "Invalid username or password."
|
49 |
-
|
50 |
-
def logout():
|
51 |
-
global current_user
|
52 |
-
current_user = None
|
53 |
-
return gr.update(visible=True), gr.update(visible=False), "You have been logged out."
|
54 |
-
|
55 |
-
def register(username, password):
|
56 |
-
if check_inactivity():
|
57 |
-
return "You have been logged out due to inactivity."
|
58 |
-
|
59 |
-
result = create_user(username, password)
|
60 |
-
update_activity()
|
61 |
-
return result
|
62 |
-
|
63 |
-
def upload(files):
|
64 |
-
if check_inactivity():
|
65 |
-
return "You have been logged out due to inactivity."
|
66 |
-
|
67 |
-
if not current_user:
|
68 |
-
return "Please log in to upload files."
|
69 |
-
|
70 |
-
results = []
|
71 |
-
for file in files:
|
72 |
-
if file.size > MAX_FILE_SIZE:
|
73 |
-
results.append(f"File {file.name} exceeds the 5GB limit.")
|
74 |
-
else:
|
75 |
-
encrypted_file = encrypt_file(file.name, file.read())
|
76 |
-
result = upload_file(current_user, file.name, encrypted_file)
|
77 |
-
results.append(result)
|
78 |
-
|
79 |
-
update_activity()
|
80 |
-
return "\n".join(results)
|
81 |
-
|
82 |
-
def get_files():
|
83 |
-
if check_inactivity():
|
84 |
-
return []
|
85 |
-
|
86 |
-
if not current_user:
|
87 |
-
return []
|
88 |
-
|
89 |
-
files = get_user_files(current_user)
|
90 |
-
update_activity()
|
91 |
-
return [{"File": file[0], "Size": f"{file[1] / 1024 / 1024:.2f} MB"} for file in files]
|
92 |
-
|
93 |
-
def download(filename):
|
94 |
-
if check_inactivity():
|
95 |
-
return None
|
96 |
-
|
97 |
-
if not current_user:
|
98 |
-
return None
|
99 |
-
|
100 |
-
file_content = download_file(current_user, filename)
|
101 |
-
if file_content:
|
102 |
-
decrypted_content = decrypt_file(filename, file_content)
|
103 |
-
update_activity()
|
104 |
-
return decrypted_content
|
105 |
-
else:
|
106 |
-
return None
|
107 |
-
|
108 |
-
def delete(filename):
|
109 |
-
if check_inactivity():
|
110 |
-
return "You have been logged out due to inactivity."
|
111 |
-
|
112 |
-
if not current_user:
|
113 |
-
return "Please log in to delete files."
|
114 |
-
|
115 |
-
result = delete_file(current_user, filename)
|
116 |
-
update_activity()
|
117 |
-
return result
|
118 |
-
|
119 |
-
def empty_user_vault(password):
|
120 |
-
if check_inactivity():
|
121 |
-
return "You have been logged out due to inactivity."
|
122 |
-
|
123 |
-
if not current_user:
|
124 |
-
return "Please log in to empty your vault."
|
125 |
-
|
126 |
-
if verify_user(current_user, password):
|
127 |
-
result = empty_vault(current_user)
|
128 |
-
update_activity()
|
129 |
-
return result
|
130 |
-
else:
|
131 |
-
return "Invalid password. Vault not emptied."
|
132 |
-
|
133 |
-
def admin_view():
|
134 |
-
if not is_admin(current_user):
|
135 |
-
return [], "Access denied."
|
136 |
-
|
137 |
-
accounts = get_all_accounts()
|
138 |
-
update_activity()
|
139 |
-
return accounts, "Admin view loaded."
|
140 |
-
|
141 |
-
def admin_delete_account(username):
|
142 |
-
if not is_admin(current_user):
|
143 |
-
return "Access denied."
|
144 |
-
|
145 |
-
result = delete_account(username)
|
146 |
-
update_activity()
|
147 |
-
return result
|
148 |
-
|
149 |
-
# Gradio interface
|
150 |
-
with gr.Blocks(css="styles.css") as app:
|
151 |
-
gr.HTML('<img src="./logo.svg" alt="Grimvault Logo" class="logo">')
|
152 |
-
|
153 |
-
with gr.Tab("Login"):
|
154 |
-
login_username = gr.Textbox(label="Username")
|
155 |
-
login_password = gr.Textbox(label="Password", type="password")
|
156 |
-
login_button = gr.Button("Login")
|
157 |
-
register_button = gr.Button("Register")
|
158 |
-
login_message = gr.Textbox(label="Message", interactive=False)
|
159 |
-
|
160 |
-
with gr.Tab("Dashboard", visible=False) as dashboard:
|
161 |
-
gr.HTML('<img src="file/logo.svg" alt="Grimvault Logo" class="logo-small">')
|
162 |
-
upload_button = gr.File(label="Upload File(s)", file_count="multiple")
|
163 |
-
files_table = gr.Dataframe(label="Your Files", headers=["File", "Size"])
|
164 |
-
refresh_button = gr.Button("Refresh Files")
|
165 |
-
download_button = gr.Button("Download")
|
166 |
-
delete_button = gr.Button("Delete")
|
167 |
-
selected_file = gr.Textbox(label="Selected File")
|
168 |
-
empty_vault_password = gr.Textbox(label="Password to Empty Vault", type="password")
|
169 |
-
empty_vault_button = gr.Button("Empty Vault")
|
170 |
-
logout_button = gr.Button("Logout")
|
171 |
-
dashboard_message = gr.Textbox(label="Message", interactive=False)
|
172 |
-
|
173 |
-
with gr.Tab("Admin", visible=False) as admin_tab:
|
174 |
-
admin_accounts = gr.Dataframe(label="User Accounts", headers=["Username", "Created At"])
|
175 |
-
admin_delete_username = gr.Textbox(label="Username to Delete")
|
176 |
-
admin_delete_button = gr.Button("Delete User")
|
177 |
-
admin_message = gr.Textbox(label="Admin Message", interactive=False)
|
178 |
-
|
179 |
-
# Login events
|
180 |
-
login_button.click(
|
181 |
-
login,
|
182 |
-
inputs=[login_username, login_password],
|
183 |
-
outputs=[gr.Tabs(), dashboard, login_message]
|
184 |
-
)
|
185 |
-
register_button.click(
|
186 |
-
register,
|
187 |
-
inputs=[login_username, login_password],
|
188 |
-
outputs=[login_message]
|
189 |
-
)
|
190 |
-
|
191 |
-
# Dashboard events
|
192 |
-
upload_button.upload(upload, inputs=[upload_button], outputs=[dashboard_message])
|
193 |
-
refresh_button.click(get_files, outputs=[files_table])
|
194 |
-
files_table.select(lambda df, evt: evt.data['File'], inputs=[files_table], outputs=[selected_file])
|
195 |
-
download_button.click(
|
196 |
-
download,
|
197 |
-
inputs=[selected_file],
|
198 |
-
outputs=[gr.File(label="Downloaded File")]
|
199 |
-
)
|
200 |
-
delete_button.click(delete, inputs=[selected_file], outputs=[dashboard_message])
|
201 |
-
empty_vault_button.click(
|
202 |
-
empty_user_vault,
|
203 |
-
inputs=[empty_vault_password],
|
204 |
-
outputs=[dashboard_message]
|
205 |
-
)
|
206 |
-
logout_button.click(logout, outputs=[gr.Tabs(), dashboard, login_message])
|
207 |
-
|
208 |
-
# Admin events
|
209 |
-
admin_tab.select(admin_view, outputs=[admin_accounts, admin_message])
|
210 |
-
admin_delete_button.click(
|
211 |
-
admin_delete_account,
|
212 |
-
inputs=[admin_delete_username],
|
213 |
-
outputs=[admin_message]
|
214 |
-
)
|
215 |
-
|
216 |
-
if __name__ == "__main__":
|
217 |
-
app.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|