FreeWebHost / app.py
szili2011's picture
Update app.py
a0775f6 verified
raw
history blame contribute delete
No virus
4.37 kB
import gradio as gr
import os
import hashlib
import base64
# Constants and Settings
UPLOAD_FOLDER = "uploads"
PASSWORD_FILE = "password_file.txt"
ADMIN_USERNAME = "admin"
ADMIN_PASSWORD_HASH = hashlib.sha256("admin_password".encode()).hexdigest()
app_settings = {
"file_uploads_enabled": True
}
# Ensure the upload directory exists
os.makedirs(UPLOAD_FOLDER, exist_ok=True)
# Function to encode data in a "Very Hard Encoded Language"
def encode_data(data):
encoded_bytes = base64.b64encode(data.encode('utf-8'))
return encoded_bytes.decode('utf-8')
# Function to decode data
def decode_data(encoded_data):
decoded_bytes = base64.b64decode(encoded_data.encode('utf-8'))
return decoded_bytes.decode('utf-8')
# Function to register a user
def register(username, password):
# Encode username and password
encoded_username = encode_data(username)
hashed_password = hashlib.sha256(password.encode()).hexdigest()
encoded_password = encode_data(hashed_password)
# Write the encoded username and password to the file
with open(PASSWORD_FILE, "a") as f:
f.write(f"{encoded_username}:{encoded_password}\n")
return f"User '{username}' registered successfully!"
# Function to log in a user
def login(username, password):
hashed_password = hashlib.sha256(password.encode()).hexdigest()
encoded_password = encode_data(hashed_password)
encoded_username = encode_data(username)
if os.path.exists(PASSWORD_FILE):
with open(PASSWORD_FILE, "r") as f:
users = f.readlines()
for user in users:
stored_encoded_username, stored_encoded_password = user.strip().split(":")
if stored_encoded_username == encoded_username and stored_encoded_password == encoded_password:
return "Login successful!"
return "Invalid username or password."
# Function to upload a file
def upload_file(file, username):
if not app_settings['file_uploads_enabled']:
return "File uploads are disabled by admin."
# Decode username to verify
decoded_username = decode_data(encode_data(username)) # Placeholder, verify appropriately
if decoded_username not in get_users():
return "Please log in to upload files."
# Save the uploaded file
if isinstance(file, str): # Check if it's a filename string
file_path = os.path.join(UPLOAD_FOLDER, file)
with open(file_path, "wb") as f:
f.write(file) # Write the file data
return f"File '{file}' uploaded successfully by {decoded_username}."
else:
return "Invalid file format."
# Function to retrieve the list of users
def get_users():
users = {}
if os.path.exists(PASSWORD_FILE):
with open(PASSWORD_FILE, "r") as f:
for line in f:
encoded_username, encoded_password = line.strip().split(":")
users[decode_data(encoded_username)] = decode_data(encoded_password) # Decoding for verification
return users
# Gradio Interface
with gr.Blocks() as demo:
gr.Markdown("# Website Publisher with Admin Control")
# Registration Tab
with gr.Tab("Register"):
username_register = gr.Textbox(label="Username")
password_register = gr.Textbox(label="Password", type="password")
register_btn = gr.Button("Register")
register_output = gr.Textbox(label="Output", interactive=False)
register_btn.click(register, inputs=[username_register, password_register], outputs=register_output)
# Login Tab
with gr.Tab("Login"):
username_login = gr.Textbox(label="Username")
password_login = gr.Textbox(label="Password", type="password")
login_btn = gr.Button("Login")
login_output = gr.Textbox(label="Output", interactive=False)
login_btn.click(login, inputs=[username_login, password_login], outputs=login_output)
# Upload Tab
with gr.Tab("Upload"):
username_upload = gr.Textbox(label="Username")
upload_file_input = gr.File(label="Upload your website files")
upload_btn = gr.Button("Upload")
upload_output = gr.Textbox(label="Output", interactive=False)
upload_btn.click(upload_file, inputs=[upload_file_input, username_upload], outputs=upload_output)
# Launch the Gradio app with public link
demo.launch(share=True)