akborana4 commited on
Commit
29de627
·
verified ·
1 Parent(s): be77680

Create backend/database.py

Browse files
Files changed (1) hide show
  1. backend/database.py +39 -0
backend/database.py ADDED
@@ -0,0 +1,39 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import sqlite3
2
+ import os
3
+
4
+ DB_FILE = "devportal.db"
5
+ USERS_DIR = "user_spaces"
6
+
7
+ def init_db():
8
+ conn = sqlite3.connect(DB_FILE)
9
+ c = conn.cursor()
10
+ # Create users table
11
+ c.execute('''CREATE TABLE IF NOT EXISTS users
12
+ (username TEXT PRIMARY KEY, password TEXT, token TEXT, settings TEXT)''')
13
+ conn.commit()
14
+ conn.close()
15
+
16
+ # Ensure the master users directory exists
17
+ if not os.path.exists(USERS_DIR):
18
+ os.makedirs(USERS_DIR)
19
+
20
+ def get_user_dir(token: str) -> str:
21
+ """Returns the absolute path to a specific user's secure folder based on their auth token."""
22
+ conn = sqlite3.connect(DB_FILE)
23
+ c = conn.cursor()
24
+ c.execute("SELECT username FROM users WHERE token=?", (token,))
25
+ row = c.fetchone()
26
+ conn.close()
27
+
28
+ if not row:
29
+ return None
30
+
31
+ username = row[0]
32
+ user_path = os.path.abspath(os.path.join(USERS_DIR, username))
33
+
34
+ # Auto-create if it got deleted
35
+ if not os.path.exists(user_path):
36
+ os.makedirs(user_path, exist_ok=True)
37
+
38
+ return user_path
39
+