Arwaaaa commited on
Commit
509e67d
·
1 Parent(s): 14bda6c
Files changed (2) hide show
  1. src/main.py +3 -1
  2. src/routes/files.py +53 -0
src/main.py CHANGED
@@ -3,7 +3,7 @@ from fastapi import FastAPI
3
  from fastapi.middleware.cors import CORSMiddleware
4
  from src.config import settings
5
  from src.database import init_db
6
- from src.routes import sessions
7
  from src.web_socket import websocket_endpoint
8
 
9
  app = FastAPI(title="Real-time Audio Streaming Server")
@@ -20,6 +20,8 @@ os.makedirs(settings.AUDIO_STORAGE_DIR, exist_ok=True)
20
  init_db()
21
 
22
  app.include_router(sessions.router)
 
 
23
 
24
  # WebSocket route
25
  app.websocket("/ws/audio")(websocket_endpoint)
 
3
  from fastapi.middleware.cors import CORSMiddleware
4
  from src.config import settings
5
  from src.database import init_db
6
+ from src.routes import sessions,files
7
  from src.web_socket import websocket_endpoint
8
 
9
  app = FastAPI(title="Real-time Audio Streaming Server")
 
20
  init_db()
21
 
22
  app.include_router(sessions.router)
23
+ app.include_router(files.router)
24
+
25
 
26
  # WebSocket route
27
  app.websocket("/ws/audio")(websocket_endpoint)
src/routes/files.py ADDED
@@ -0,0 +1,53 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ from fastapi import APIRouter
3
+ from fastapi.responses import FileResponse, JSONResponse
4
+ from src.config import settings
5
+ import sqlite3
6
+
7
+ router = APIRouter()
8
+
9
+ # --- AUDIO FILES ---
10
+ @router.get("/files")
11
+ def list_files():
12
+ if not os.path.exists(settings.AUDIO_STORAGE_DIR):
13
+ return {"files": []}
14
+ return {"files": os.listdir(settings.AUDIO_STORAGE_DIR)}
15
+
16
+ @router.get("/files/{filename}")
17
+ def get_file(filename: str):
18
+ filepath = os.path.join(settings.AUDIO_STORAGE_DIR, filename)
19
+ if os.path.exists(filepath):
20
+ return FileResponse(filepath, media_type="audio/wav", filename=filename)
21
+ return JSONResponse(content={"error": "File not found"}, status_code=404)
22
+
23
+ # --- DATABASE VIEW ---
24
+ @router.get("/db/tables")
25
+ def list_tables():
26
+ if not os.path.exists(settings.DB_PATH):
27
+ return {"tables": []}
28
+
29
+ conn = sqlite3.connect(settings.DB_PATH)
30
+ cursor = conn.cursor()
31
+ cursor.execute("SELECT name FROM sqlite_master WHERE type='table';")
32
+ tables = [row[0] for row in cursor.fetchall()]
33
+ conn.close()
34
+ return {"tables": tables}
35
+
36
+ @router.get("/db/{table_name}")
37
+ def get_table_data(table_name: str, limit: int = 50):
38
+ if not os.path.exists(settings.DB_PATH):
39
+ return {"error": "DB not found"}
40
+
41
+ conn = sqlite3.connect(settings.DB_PATH)
42
+ cursor = conn.cursor()
43
+
44
+ try:
45
+ cursor.execute(f"SELECT * FROM {table_name} LIMIT {limit}")
46
+ rows = cursor.fetchall()
47
+ columns = [description[0] for description in cursor.description]
48
+ except sqlite3.Error as e:
49
+ conn.close()
50
+ return {"error": str(e)}
51
+
52
+ conn.close()
53
+ return {"columns": columns, "rows": rows}