Spaces:
Running
Running
File size: 1,074 Bytes
29de627 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | 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
|