"""
self.send_html(content)
def handle_upload(self):
username = self.get_session_user()
if not username:
self.send_redirect("/login")
return
if self.command != "POST":
self.send_html("
Method not allowed
", 405)
return
content_type = self.headers.get("Content-Type", "")
if not content_type.startswith("multipart/form-data"):
self.send_html("
Invalid content type
", 400)
return
boundary = content_type.split("boundary=")[1]
content_length = int(self.headers.get("Content-Length", 0))
data = self.rfile.read(content_length)
parts = data.split(b"--" + boundary.encode())
file_data = None
filename = None
for part in parts:
if b'Content-Disposition: form-data; name="file"; filename="' in part:
header_end = part.find(b"\r\n\r\n")
if header_end == -1:
continue
headers = part[:header_end].decode("utf-8", errors="ignore")
filename_start = headers.find('filename="') + 10
filename_end = headers.find('"', filename_start)
filename = headers[filename_start:filename_end]
file_data = part[header_end + 4 : -2]
break
if not filename or not file_data:
self.send_html("
File not found in request
", 400)
return
file_size = len(file_data)
if not can_upload(username, file_size):
self.send_html("
Quota exceeded
", 403)
return
user_dir = get_user_dir(username)
filepath = os.path.join(user_dir, filename)
with open(filepath, "wb") as f:
f.write(file_data)
update_used_space(username, file_size / (1024 * 1024))
self.send_redirect("/files")
def handle_file_download(self, username, filename):
user_dir = os.path.join(get_storage_root(), username)
filepath = os.path.join(user_dir, filename)
if not os.path.exists(filepath) or not os.path.isfile(filepath):
self.send_html("
File not found
", 404)
return
mime_type, _ = mimetypes.guess_type(filename)
if not mime_type:
mime_type = "application/octet-stream"
# Maybe you don't want to transform your FS into CDN?
if CONFIG.get("server", {}).get("video_and_image_preview", True):
# Determine if the file should be displayed inline or downloaded
if mime_type.startswith(("image/", "video/", "audio/")):
disposition = "inline"
else:
disposition = f'attachment; filename="{filename}"'
else:
disposition = f'attachment; filename="{filename}"'
self.send_response(200)
self.send_header("Content-Type", mime_type)
self.send_header("Content-Disposition", disposition)
self.end_headers()
with open(filepath, "rb") as f:
self.wfile.write(f.read())
def handle_delete(self):
username = self.get_session_user()
if not username:
self.send_redirect("/login")
return
if self.command != "POST":
self.send_html("
", 400)
return
cursor = database.DB_CONN.cursor()
cursor.execute("SELECT id FROM users WHERE username = ?", (username,))
row = cursor.fetchone()
if not row:
self.send_html("
User not found
", 404)
return
user_dir = os.path.join(get_storage_root(), username)
if os.path.exists(user_dir):
for fname in os.listdir(user_dir):
os.remove(os.path.join(user_dir, fname))
os.rmdir(user_dir)
cursor.execute("DELETE FROM users WHERE username = ?", (username,))
database.DB_CONN.commit()
self.send_html("
Account deleted
")
def handle_static_file(self):
if self.path.startswith("/static/"):
file_path = self.path[1:] # Remove leading slash
try:
with open(file_path, "r") as file:
content = file.read()
if file_path.endswith(".css"):
self.send_response(200)
self.send_header("Content-type", "text/css")
self.end_headers()
self.wfile.write(content.encode())
else:
self.send_error(404)
except FileNotFoundError:
self.send_error(404)
return True
return False
def handle_favicon(self):
if self.path == "/favicon.ico":
file_path = os.path.join("static", self.path[1:])
try:
with open(file_path, "rb") as file: # Open in binary mode
content = file.read()
self.send_response(200)
self.send_header(
"Content-type", "image/x-icon"
) # Correct MIME type
self.end_headers()
self.wfile.write(content) # Write bytes directly
except FileNotFoundError:
self.send_error(404)
return True
return False
def do_GET(self):
parsed_path = urlparse(self.path)
path = parsed_path.path
if path == "/":
self.handle_main_page()
elif path == "/register":
self.handle_register_page()
elif path == "/login":
self.handle_login_page()
elif path == "/files":
self.handle_files_page()
elif path == "/logout":
self.handle_logout()
elif path == "/explore":
self.handle_explore()
elif path.startswith("/explore/") and not path.endswith("/"):
parts = path[len("/explore/") :].split("/", 1)
if len(parts) == 2:
username, filename = parts
self.handle_file_download(username, filename)
else:
username = parts[0]
self.handle_explore_user(username)
elif path.startswith("/static/") and not path.endswith("/"):
self.handle_static_file()
elif path == "/favicon.ico":
self.handle_favicon()
else:
self.send_html("