Update storage/media_routers.py
Browse files- storage/media_routers.py +37 -0
storage/media_routers.py
CHANGED
|
@@ -13,6 +13,7 @@ router = APIRouter(prefix="/media", tags=["Media Manager"])
|
|
| 13 |
MEDIA_ROOT = Path("/data/media")
|
| 14 |
file_manager = FileManager(MEDIA_ROOT)
|
| 15 |
HF_TOKEN = os.getenv("HF_TOKEN")
|
|
|
|
| 16 |
|
| 17 |
def validate_token(token: str):
|
| 18 |
"""
|
|
@@ -25,6 +26,42 @@ def validate_token(token: str):
|
|
| 25 |
if token != HF_TOKEN:
|
| 26 |
raise HTTPException(status_code=401, detail="Invalid token")
|
| 27 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
|
| 29 |
@router.delete("/clear_media", tags=["Media Manager"])
|
| 30 |
def clear_media(token: str = Query(..., description="Token required for authorization")):
|
|
|
|
| 13 |
MEDIA_ROOT = Path("/data/media")
|
| 14 |
file_manager = FileManager(MEDIA_ROOT)
|
| 15 |
HF_TOKEN = os.getenv("HF_TOKEN")
|
| 16 |
+
DB_PATH = Path("/data/db")
|
| 17 |
|
| 18 |
def validate_token(token: str):
|
| 19 |
"""
|
|
|
|
| 26 |
if token != HF_TOKEN:
|
| 27 |
raise HTTPException(status_code=401, detail="Invalid token")
|
| 28 |
|
| 29 |
+
@router.post("/upload_db_file", tags=["Database Manager"])
|
| 30 |
+
async def upload_db_file(
|
| 31 |
+
file: UploadFile = File(...),
|
| 32 |
+
token: str = Query(..., description="Token required for authorization")
|
| 33 |
+
):
|
| 34 |
+
"""
|
| 35 |
+
Upload a file to the /data/db folder.
|
| 36 |
+
|
| 37 |
+
Steps:
|
| 38 |
+
- Validate the token.
|
| 39 |
+
- Ensure /data/db exists (create it if missing).
|
| 40 |
+
- Save the uploaded file inside /data/db using its original filename.
|
| 41 |
+
- Return a JSON response confirming the upload.
|
| 42 |
+
"""
|
| 43 |
+
validate_token(token)
|
| 44 |
+
|
| 45 |
+
# Crear carpeta /data/db si no existe
|
| 46 |
+
DB_PATH.mkdir(parents=True, exist_ok=True)
|
| 47 |
+
|
| 48 |
+
final_path = DB_PATH / file.filename
|
| 49 |
+
|
| 50 |
+
try:
|
| 51 |
+
# Leer contenido en memoria y guardar
|
| 52 |
+
file_bytes = await file.read()
|
| 53 |
+
with open(final_path, "wb") as f:
|
| 54 |
+
f.write(file_bytes)
|
| 55 |
+
except Exception as exc:
|
| 56 |
+
raise HTTPException(status_code=500, detail=f"Failed to save file: {exc}")
|
| 57 |
+
|
| 58 |
+
return JSONResponse(
|
| 59 |
+
status_code=200,
|
| 60 |
+
content={
|
| 61 |
+
"status": "ok",
|
| 62 |
+
"saved_to": str(final_path)
|
| 63 |
+
}
|
| 64 |
+
)
|
| 65 |
|
| 66 |
@router.delete("/clear_media", tags=["Media Manager"])
|
| 67 |
def clear_media(token: str = Query(..., description="Token required for authorization")):
|