szili2011 commited on
Commit
541b3e0
1 Parent(s): d75ef57

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -15
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 the password in a "Very Hard Encoded Language"
19
- def encode_password(password):
20
- encoded_bytes = base64.b64encode(password.encode('utf-8'))
21
  return encoded_bytes.decode('utf-8')
22
 
23
- # Function to decode the password
24
- def decode_password(encoded_password):
25
- decoded_bytes = base64.b64decode(encoded_password.encode('utf-8'))
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 = encode_password(hashed_password)
32
 
 
33
  with open(PASSWORD_FILE, "a") as f:
34
- f.write(f"{username}:{encoded_password}\n")
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 = encode_password(hashed_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
- stored_username, stored_encoded_password = user.strip().split(":")
49
- if stored_username == username and stored_encoded_password == encoded_password:
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
- if username not in get_users():
 
 
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 {username}."
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
- username, encoded_password = line.strip().split(":")
76
- users[username] = decode_password(encoded_password) # Decoding for verification
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