Spaces:
Sleeping
Sleeping
Download backend/database.py from akborana4/devportal2: direct link, hf CLI and curl.
- Browser
- Download file 1.07 kB
-
https://huggingface.co/spaces/akborana4/devportal2/resolve/main/backend/database.py
- Command line
-
hf download hf://spaces/akborana4/devportal2/backend/database.py
-
curl -L -o database.py https://huggingface.co/spaces/akborana4/devportal2/resolve/main/backend/database.py
1.07 kB
| import sqlite3 | |
| import os | |
| DB_FILE = "devportal.db" | |
| USERS_DIR = "user_spaces" | |
| def init_db(): | |
| conn = sqlite3.connect(DB_FILE) | |
| c = conn.cursor() | |
| # Create users table | |
| c.execute('''CREATE TABLE IF NOT EXISTS users | |
| (username TEXT PRIMARY KEY, password TEXT, token TEXT, settings TEXT)''') | |
| conn.commit() | |
| conn.close() | |
| # Ensure the master users directory exists | |
| if not os.path.exists(USERS_DIR): | |
| os.makedirs(USERS_DIR) | |
| def get_user_dir(token: str) -> str: | |
| """Returns the absolute path to a specific user's secure folder based on their auth token.""" | |
| conn = sqlite3.connect(DB_FILE) | |
| c = conn.cursor() | |
| c.execute("SELECT username FROM users WHERE token=?", (token,)) | |
| row = c.fetchone() | |
| conn.close() | |
| if not row: | |
| return None | |
| username = row[0] | |
| user_path = os.path.abspath(os.path.join(USERS_DIR, username)) | |
| # Auto-create if it got deleted | |
| if not os.path.exists(user_path): | |
| os.makedirs(user_path, exist_ok=True) | |
| return user_path | |