Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -15,38 +15,42 @@ app_settings = {
|
|
15 |
# Ensure the upload directory exists
|
16 |
os.makedirs(UPLOAD_FOLDER, exist_ok=True)
|
17 |
|
18 |
-
# Function to encode
|
19 |
-
def
|
20 |
-
encoded_bytes = base64.b64encode(
|
21 |
return encoded_bytes.decode('utf-8')
|
22 |
|
23 |
-
# Function to decode
|
24 |
-
def
|
25 |
-
decoded_bytes = base64.b64decode(
|
26 |
return decoded_bytes.decode('utf-8')
|
27 |
|
28 |
# Function to register a user
|
29 |
def register(username, password):
|
|
|
|
|
30 |
hashed_password = hashlib.sha256(password.encode()).hexdigest()
|
31 |
-
encoded_password =
|
32 |
|
|
|
33 |
with open(PASSWORD_FILE, "a") as f:
|
34 |
-
f.write(f"{
|
35 |
|
36 |
return f"User '{username}' registered successfully!"
|
37 |
|
38 |
# Function to log in a user
|
39 |
def login(username, password):
|
40 |
hashed_password = hashlib.sha256(password.encode()).hexdigest()
|
41 |
-
encoded_password =
|
|
|
42 |
|
43 |
if os.path.exists(PASSWORD_FILE):
|
44 |
with open(PASSWORD_FILE, "r") as f:
|
45 |
users = f.readlines()
|
46 |
|
47 |
for user in users:
|
48 |
-
|
49 |
-
if
|
50 |
return "Login successful!"
|
51 |
|
52 |
return "Invalid username or password."
|
@@ -56,7 +60,9 @@ def upload_file(file, username):
|
|
56 |
if not app_settings['file_uploads_enabled']:
|
57 |
return "File uploads are disabled by admin."
|
58 |
|
59 |
-
|
|
|
|
|
60 |
return "Please log in to upload files."
|
61 |
|
62 |
# Save the uploaded file
|
@@ -64,7 +70,7 @@ def upload_file(file, username):
|
|
64 |
with open(file_path, "wb") as f:
|
65 |
f.write(file.read())
|
66 |
|
67 |
-
return f"File '{file.name}' uploaded successfully by {
|
68 |
|
69 |
# Function to retrieve the list of users
|
70 |
def get_users():
|
@@ -72,8 +78,8 @@ def get_users():
|
|
72 |
if os.path.exists(PASSWORD_FILE):
|
73 |
with open(PASSWORD_FILE, "r") as f:
|
74 |
for line in f:
|
75 |
-
|
76 |
-
users[
|
77 |
return users
|
78 |
|
79 |
# Gradio Interface
|
|
|
15 |
# Ensure the upload directory exists
|
16 |
os.makedirs(UPLOAD_FOLDER, exist_ok=True)
|
17 |
|
18 |
+
# Function to encode data in a "Very Hard Encoded Language"
|
19 |
+
def encode_data(data):
|
20 |
+
encoded_bytes = base64.b64encode(data.encode('utf-8'))
|
21 |
return encoded_bytes.decode('utf-8')
|
22 |
|
23 |
+
# Function to decode data
|
24 |
+
def decode_data(encoded_data):
|
25 |
+
decoded_bytes = base64.b64decode(encoded_data.encode('utf-8'))
|
26 |
return decoded_bytes.decode('utf-8')
|
27 |
|
28 |
# Function to register a user
|
29 |
def register(username, password):
|
30 |
+
# Encode username and password
|
31 |
+
encoded_username = encode_data(username)
|
32 |
hashed_password = hashlib.sha256(password.encode()).hexdigest()
|
33 |
+
encoded_password = encode_data(hashed_password)
|
34 |
|
35 |
+
# Write the encoded username and password to the file
|
36 |
with open(PASSWORD_FILE, "a") as f:
|
37 |
+
f.write(f"{encoded_username}:{encoded_password}\n")
|
38 |
|
39 |
return f"User '{username}' registered successfully!"
|
40 |
|
41 |
# Function to log in a user
|
42 |
def login(username, password):
|
43 |
hashed_password = hashlib.sha256(password.encode()).hexdigest()
|
44 |
+
encoded_password = encode_data(hashed_password)
|
45 |
+
encoded_username = encode_data(username)
|
46 |
|
47 |
if os.path.exists(PASSWORD_FILE):
|
48 |
with open(PASSWORD_FILE, "r") as f:
|
49 |
users = f.readlines()
|
50 |
|
51 |
for user in users:
|
52 |
+
stored_encoded_username, stored_encoded_password = user.strip().split(":")
|
53 |
+
if stored_encoded_username == encoded_username and stored_encoded_password == encoded_password:
|
54 |
return "Login successful!"
|
55 |
|
56 |
return "Invalid username or password."
|
|
|
60 |
if not app_settings['file_uploads_enabled']:
|
61 |
return "File uploads are disabled by admin."
|
62 |
|
63 |
+
# Decode username to verify
|
64 |
+
decoded_username = decode_data(encode_data(username)) # Placeholder, verify appropriately
|
65 |
+
if decoded_username not in get_users():
|
66 |
return "Please log in to upload files."
|
67 |
|
68 |
# Save the uploaded file
|
|
|
70 |
with open(file_path, "wb") as f:
|
71 |
f.write(file.read())
|
72 |
|
73 |
+
return f"File '{file.name}' uploaded successfully by {decoded_username}."
|
74 |
|
75 |
# Function to retrieve the list of users
|
76 |
def get_users():
|
|
|
78 |
if os.path.exists(PASSWORD_FILE):
|
79 |
with open(PASSWORD_FILE, "r") as f:
|
80 |
for line in f:
|
81 |
+
encoded_username, encoded_password = line.strip().split(":")
|
82 |
+
users[decode_data(encoded_username)] = decode_data(encoded_password) # Decoding for verification
|
83 |
return users
|
84 |
|
85 |
# Gradio Interface
|