From 1e5693d3da11fec80ebbc64e2b7085f415323c51 Mon Sep 17 00:00:00 2001 From: justuser-31 Date: Sat, 31 Jan 2026 18:29:25 +0300 Subject: [PATCH] =?UTF-8?q?=D0=94=D0=BE=D1=80=D0=B0=D0=B1=D0=BE=D1=82?= =?UTF-8?q?=D0=BA=D0=B8,=20=D1=80=D0=B0=D0=B7=D1=80=D0=B0=D0=B1=D0=BE?= =?UTF-8?q?=D1=82=D0=BA=D0=B0=20call2api?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- call2api.py.py | 92 +++++++++++++++++++++++++++++++++++++++++------- handlers.py | 21 ++++++++--- requirements.txt | 2 ++ 3 files changed, 98 insertions(+), 17 deletions(-) create mode 100644 requirements.txt diff --git a/call2api.py.py b/call2api.py.py index 127fe28..6d8ef79 100644 --- a/call2api.py.py +++ b/call2api.py.py @@ -1,19 +1,85 @@ +from typing import Optional + import requests -url = "http://127.0.0.1:8000/create_account" # Replace with actual endpoint URL -payload = { - 'token': 'test', - 'username': 'test', - 'password': 'test', - 'drive_quota': '1024' # in MB -} +def create_account( + api_url: str, api_token: str, username: str, password: str, drive_quota: int +) -> dict: + """ + Call the create account API endpoint. -headers = { - 'Content-Type': 'application/x-www-form-urlencoded' -} + Args: + api_url: Base URL of the API server (e.g., 'http://localhost:8000') + api_token: Security token for API authentication + username: Username for the new account + password: Password for the new account + drive_quota: Storage quota in MB -response = requests.post(url, data=payload, headers=headers) + Returns: + Dictionary with 'status_code' and 'message' keys + """ + url = f"{api_url.rstrip('/')}/create_account" + data = { + "token": api_token, + "username": username, + "password": password, + "drive_quota": str(drive_quota), + } -print("Status Code:", response.status_code) -print("Response Text:", response.text) \ No newline at end of file + try: + response = requests.post(url, data=data) + return {"status_code": response.status_code, "message": response.text} + except requests.RequestException as e: + return {"status_code": -1, "message": f"Request failed: {str(e)}"} + + +def set_quota(api_url: str, api_token: str, username: str, drive_quota: int) -> dict: + """ + Call the set quota API endpoint. + + Args: + api_url: Base URL of the API server (e.g., 'http://localhost:8000') + api_token: Security token for API authentication + username: Username whose quota should be updated + drive_quota: New storage quota in MB + + Returns: + Dictionary with 'status_code' and 'message' keys + """ + url = f"{api_url.rstrip('/')}/set_quota" + data = {"token": api_token, "username": username, "drive_quota": str(drive_quota)} + + try: + response = requests.post(url, data=data) + return {"status_code": response.status_code, "message": response.text} + except requests.RequestException as e: + return {"status_code": -1, "message": f"Request failed: {str(e)}"} + + +def delete_account(api_url: str, api_token: str, username: str) -> dict: + """ + Call the delete account API endpoint. + + Args: + api_url: Base URL of the API server (e.g., 'http://localhost:8000') + api_token: Security token for API authentication + username: Username of the account to delete + + Returns: + Dictionary with 'status_code' and 'message' keys + """ + url = f"{api_url.rstrip('/')}/delete_account" + data = {"token": api_token, "username": username} + + try: + response = requests.post(url, data=data) + return {"status_code": response.status_code, "message": response.text} + except requests.RequestException as e: + return {"status_code": -1, "message": f"Request failed: {str(e)}"} + + +# print(set_quota("http://127.0.0.1:8000", "test", "test", 1000)) +# print(delete_account("http://127.0.0.1:8000", "test", "test")) +# print(create_account("http://proxy.del.pw:50020", "test", "test", "test", 100)) +print(create_account("http://proxy.del.pw:50020", "test", "ritmas", "ritmas", 100)) diff --git a/handlers.py b/handlers.py index 7b3ec48..b412003 100644 --- a/handlers.py +++ b/handlers.py @@ -112,6 +112,7 @@ class FileServerHandler(BaseHTTPRequestHandler): self.send_html(content) def handle_login_page(self): + cursor = database.DB_CONN.cursor() if self.path == "/login" and self.command == "POST": content_length = int(self.headers.get("Content-Length", 0)) post_data = self.rfile.read(content_length).decode("utf-8") @@ -119,7 +120,6 @@ class FileServerHandler(BaseHTTPRequestHandler): username = params.get("username", [""])[0] password = params.get("password", [""])[0] - cursor = database.DB_CONN.cursor() cursor.execute( "SELECT password_hash FROM users WHERE username = ?", (username,) ) @@ -146,10 +146,16 @@ class FileServerHandler(BaseHTTPRequestHandler): self.send_html(content, 401) return + # Check if user already authorized username = self.get_session_user() if username: - self.send_redirect("/files") - return + # Check if user really exist + cursor.execute("SELECT username FROM users WHERE username = ?", (username,)) + username_row = cursor.fetchone() + if username_row: + self.send_redirect("/files") + return + content = self.render_login_form() self.send_html(content) @@ -191,6 +197,13 @@ class FileServerHandler(BaseHTTPRequestHandler): used_mb = get_user_used_space(username) cursor = database.DB_CONN.cursor() + cursor.execute("SELECT quota_mb FROM users WHERE username = ?", (username,)) + available_mb_row = cursor.fetchone() + # If user not authorized + if not available_mb_row: + self.send_redirect("/login") + return + available_mb = available_mb_row[0] cursor.execute( "UPDATE users SET used_mb = ? WHERE username = ?", (used_mb, username) ) @@ -230,7 +243,7 @@ class FileServerHandler(BaseHTTPRequestHandler):

SimpliestFS

My Files

- Quota: {CONFIG["storage"]["default_quota_mb"]} MB | Used: {used_mb:.2f} MB + Quota: {available_mb} MB | Used: {used_mb:.2f} MB
diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..6c9fdba --- /dev/null +++ b/requirements.txt @@ -0,0 +1,2 @@ +PyYAML +requests