Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,14 +1,10 @@
|
|
1 |
import gradio as gr
|
2 |
import os
|
3 |
import time
|
4 |
-
import sqlite3
|
5 |
-
import secrets
|
6 |
-
from pathlib import Path
|
7 |
-
from functools import partial
|
8 |
from helper import (
|
9 |
create_user, verify_user, get_user_files, upload_file, download_file,
|
10 |
delete_file, empty_vault, is_admin, get_all_accounts, delete_account,
|
11 |
-
encrypt_file, decrypt_file,
|
12 |
)
|
13 |
|
14 |
# Constants
|
@@ -19,13 +15,6 @@ INACTIVITY_TIMEOUT = 300 # 5 minutes in seconds
|
|
19 |
current_user = None
|
20 |
last_activity_time = time.time()
|
21 |
|
22 |
-
# Initialize database
|
23 |
-
db_path = Path("database/grimvault.db")
|
24 |
-
db_path.parent.mkdir(exist_ok=True)
|
25 |
-
conn = sqlite3.connect(str(db_path))
|
26 |
-
create_tables(conn)
|
27 |
-
conn.close()
|
28 |
-
|
29 |
def check_inactivity():
|
30 |
global current_user, last_activity_time
|
31 |
if current_user and time.time() - last_activity_time > INACTIVITY_TIMEOUT:
|
@@ -42,12 +31,19 @@ def login(username, password):
|
|
42 |
if check_inactivity():
|
43 |
return gr.update(visible=True), gr.update(visible=False), "You have been logged out due to inactivity."
|
44 |
|
45 |
-
|
46 |
-
|
|
|
|
|
|
|
|
|
|
|
47 |
current_user = username
|
48 |
update_activity()
|
|
|
49 |
return gr.update(visible=False), gr.update(visible=True), f"Welcome, {username}!"
|
50 |
else:
|
|
|
51 |
return gr.update(visible=True), gr.update(visible=False), "Invalid username or password."
|
52 |
|
53 |
def logout():
|
@@ -75,8 +71,8 @@ def upload(files):
|
|
75 |
if file.size > MAX_FILE_SIZE:
|
76 |
results.append(f"File {file.name} exceeds the 5GB limit.")
|
77 |
else:
|
78 |
-
|
79 |
-
result = upload_file(current_user, file.name,
|
80 |
results.append(result)
|
81 |
|
82 |
update_activity()
|
@@ -100,9 +96,9 @@ def download(filename):
|
|
100 |
if not current_user:
|
101 |
return None
|
102 |
|
103 |
-
|
104 |
-
if
|
105 |
-
decrypted_content = decrypt_file(filename,
|
106 |
update_activity()
|
107 |
return decrypted_content
|
108 |
else:
|
|
|
1 |
import gradio as gr
|
2 |
import os
|
3 |
import time
|
|
|
|
|
|
|
|
|
4 |
from helper import (
|
5 |
create_user, verify_user, get_user_files, upload_file, download_file,
|
6 |
delete_file, empty_vault, is_admin, get_all_accounts, delete_account,
|
7 |
+
encrypt_file, decrypt_file, is_rate_limited, is_account_locked, record_login_attempt
|
8 |
)
|
9 |
|
10 |
# Constants
|
|
|
15 |
current_user = None
|
16 |
last_activity_time = time.time()
|
17 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
18 |
def check_inactivity():
|
19 |
global current_user, last_activity_time
|
20 |
if current_user and time.time() - last_activity_time > INACTIVITY_TIMEOUT:
|
|
|
31 |
if check_inactivity():
|
32 |
return gr.update(visible=True), gr.update(visible=False), "You have been logged out due to inactivity."
|
33 |
|
34 |
+
if is_account_locked(username):
|
35 |
+
return gr.update(visible=True), gr.update(visible=False), "Account is locked. Please try again later."
|
36 |
+
|
37 |
+
if is_rate_limited(username):
|
38 |
+
return gr.update(visible=True), gr.update(visible=False), "Too many login attempts. Please try again later."
|
39 |
+
|
40 |
+
if verify_user(username, password):
|
41 |
current_user = username
|
42 |
update_activity()
|
43 |
+
record_login_attempt(username, True)
|
44 |
return gr.update(visible=False), gr.update(visible=True), f"Welcome, {username}!"
|
45 |
else:
|
46 |
+
record_login_attempt(username, False)
|
47 |
return gr.update(visible=True), gr.update(visible=False), "Invalid username or password."
|
48 |
|
49 |
def logout():
|
|
|
71 |
if file.size > MAX_FILE_SIZE:
|
72 |
results.append(f"File {file.name} exceeds the 5GB limit.")
|
73 |
else:
|
74 |
+
encrypted_content = encrypt_file(file.name, file.read())
|
75 |
+
result = upload_file(current_user, file.name, encrypted_content)
|
76 |
results.append(result)
|
77 |
|
78 |
update_activity()
|
|
|
96 |
if not current_user:
|
97 |
return None
|
98 |
|
99 |
+
encrypted_content = download_file(current_user, filename)
|
100 |
+
if encrypted_content:
|
101 |
+
decrypted_content = decrypt_file(filename, encrypted_content)
|
102 |
update_activity()
|
103 |
return decrypted_content
|
104 |
else:
|