Update storage/media_routers.py
Browse files- storage/media_routers.py +48 -1
storage/media_routers.py
CHANGED
|
@@ -1,12 +1,15 @@
|
|
| 1 |
import os
|
| 2 |
import io
|
| 3 |
import shutil
|
|
|
|
|
|
|
|
|
|
| 4 |
from pathlib import Path
|
| 5 |
|
| 6 |
from fastapi import APIRouter, UploadFile, File, Query, HTTPException
|
| 7 |
from fastapi.responses import FileResponse, JSONResponse
|
| 8 |
|
| 9 |
-
|
| 10 |
from storage.files.file_manager import FileManager
|
| 11 |
|
| 12 |
router = APIRouter(prefix="/media", tags=["Media Manager"])
|
|
@@ -63,6 +66,50 @@ async def upload_db_file(
|
|
| 63 |
}
|
| 64 |
)
|
| 65 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
@router.delete("/clear_media", tags=["Media Manager"])
|
| 67 |
def clear_media(token: str = Query(..., description="Token required for authorization")):
|
| 68 |
"""
|
|
|
|
| 1 |
import os
|
| 2 |
import io
|
| 3 |
import shutil
|
| 4 |
+
|
| 5 |
+
import sqlite3
|
| 6 |
+
|
| 7 |
from pathlib import Path
|
| 8 |
|
| 9 |
from fastapi import APIRouter, UploadFile, File, Query, HTTPException
|
| 10 |
from fastapi.responses import FileResponse, JSONResponse
|
| 11 |
|
| 12 |
+
|
| 13 |
from storage.files.file_manager import FileManager
|
| 14 |
|
| 15 |
router = APIRouter(prefix="/media", tags=["Media Manager"])
|
|
|
|
| 66 |
}
|
| 67 |
)
|
| 68 |
|
| 69 |
+
@router.post("/execute_query", tags=["Database Manager"])
|
| 70 |
+
async def execute_query(
|
| 71 |
+
db_filename: str = Query(..., description="SQLite .db file name"),
|
| 72 |
+
query: str = Query(..., description="SQL query to execute"),
|
| 73 |
+
token: str = Query(..., description="Token required for authorization")
|
| 74 |
+
):
|
| 75 |
+
"""
|
| 76 |
+
Execute a SQL query against a specified SQLite database file in /data/db.
|
| 77 |
+
|
| 78 |
+
Steps:
|
| 79 |
+
- Validate the token.
|
| 80 |
+
- Ensure the requested .db file exists inside /data/db.
|
| 81 |
+
- Connect to the database using sqlite3.
|
| 82 |
+
- Execute the provided query.
|
| 83 |
+
- Return the results as a list of dictionaries (column: value).
|
| 84 |
+
- Capture and return errors if query execution fails.
|
| 85 |
+
"""
|
| 86 |
+
validate_token(token)
|
| 87 |
+
|
| 88 |
+
db_file_path = DB_PATH / db_filename
|
| 89 |
+
|
| 90 |
+
if not db_file_path.exists() or not db_file_path.is_file():
|
| 91 |
+
raise HTTPException(status_code=404, detail=f"Database file {db_filename} not found")
|
| 92 |
+
|
| 93 |
+
try:
|
| 94 |
+
conn = sqlite3.connect(db_file_path)
|
| 95 |
+
conn.row_factory = sqlite3.Row # para devolver columnas por nombre
|
| 96 |
+
cur = conn.cursor()
|
| 97 |
+
|
| 98 |
+
cur.execute(query)
|
| 99 |
+
rows = cur.fetchall()
|
| 100 |
+
|
| 101 |
+
# Convert rows to list of dicts
|
| 102 |
+
results = [dict(row) for row in rows]
|
| 103 |
+
|
| 104 |
+
conn.commit()
|
| 105 |
+
conn.close()
|
| 106 |
+
except sqlite3.Error as e:
|
| 107 |
+
raise HTTPException(status_code=400, detail=f"SQL error: {e}")
|
| 108 |
+
except Exception as e:
|
| 109 |
+
raise HTTPException(status_code=500, detail=f"Unexpected error: {e}")
|
| 110 |
+
|
| 111 |
+
return JSONResponse(content={"results": results})
|
| 112 |
+
|
| 113 |
@router.delete("/clear_media", tags=["Media Manager"])
|
| 114 |
def clear_media(token: str = Query(..., description="Token required for authorization")):
|
| 115 |
"""
|