Create data_routers.py
Browse files- storage/data_routers.py +120 -0
storage/data_routers.py
ADDED
|
@@ -0,0 +1,120 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import io
|
| 3 |
+
import json
|
| 4 |
+
import shutil
|
| 5 |
+
|
| 6 |
+
import sqlite3
|
| 7 |
+
|
| 8 |
+
from pathlib import Path
|
| 9 |
+
|
| 10 |
+
from fastapi import APIRouter, UploadFile, File, Query, HTTPException
|
| 11 |
+
from fastapi.responses import FileResponse, JSONResponse
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
from storage.files.file_manager import FileManager
|
| 15 |
+
from storage.common import validate_token
|
| 16 |
+
|
| 17 |
+
router = APIRouter(prefix="/data", tags=["Data Manager"])
|
| 18 |
+
DATA_ROOT = Path("/data")
|
| 19 |
+
HF_TOKEN = os.getenv("HF_TOKEN")
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
@router.get("/data_tree", tags=["Data Manager"])
|
| 23 |
+
def get_data_tree(
|
| 24 |
+
token: str = Query(..., description="Token required for authorization")
|
| 25 |
+
):
|
| 26 |
+
"""
|
| 27 |
+
Return a formatted tree of folders and files under /data.
|
| 28 |
+
|
| 29 |
+
Behavior:
|
| 30 |
+
- Validate token.
|
| 31 |
+
- Walk the /data directory and build a recursive tree.
|
| 32 |
+
- Each entry includes: name, type (file/directory), and children if directory.
|
| 33 |
+
"""
|
| 34 |
+
|
| 35 |
+
validate_token(token)
|
| 36 |
+
|
| 37 |
+
def build_tree(path: Path):
|
| 38 |
+
if path.is_file():
|
| 39 |
+
return {
|
| 40 |
+
"name": path.name,
|
| 41 |
+
"type": "file"
|
| 42 |
+
}
|
| 43 |
+
|
| 44 |
+
# Directory case
|
| 45 |
+
children = []
|
| 46 |
+
for child in sorted(path.iterdir(), key=lambda p: p.name.lower()):
|
| 47 |
+
children.append(build_tree(child))
|
| 48 |
+
|
| 49 |
+
return {
|
| 50 |
+
"name": path.name,
|
| 51 |
+
"type": "directory",
|
| 52 |
+
"children": children
|
| 53 |
+
}
|
| 54 |
+
|
| 55 |
+
if not DATA_ROOT.exists():
|
| 56 |
+
return {"error": "/data does not exist"}
|
| 57 |
+
|
| 58 |
+
return build_tree(DATA_ROOT)
|
| 59 |
+
|
| 60 |
+
@router.post("/create_data_item", tags=["Data Manager"])
|
| 61 |
+
def create_data_item(
|
| 62 |
+
path: str = Query(..., description="Full path starting with /data/"),
|
| 63 |
+
item_type: str = Query(..., description="directory or file"),
|
| 64 |
+
token: str = Query(..., description="Token required for authorization")
|
| 65 |
+
):
|
| 66 |
+
"""
|
| 67 |
+
Create a directory or file under /data.
|
| 68 |
+
|
| 69 |
+
Restrictions:
|
| 70 |
+
- Path must start with /data/.
|
| 71 |
+
- Writing to /data/db or /data/media (or any of their subpaths) is forbidden.
|
| 72 |
+
- item_type must be 'directory' or 'file'.
|
| 73 |
+
|
| 74 |
+
Behavior:
|
| 75 |
+
- Validate token.
|
| 76 |
+
- Check path validity and protection.
|
| 77 |
+
- Create directory or empty file.
|
| 78 |
+
- Raise error if path already exists.
|
| 79 |
+
"""
|
| 80 |
+
|
| 81 |
+
validate_token(token)
|
| 82 |
+
|
| 83 |
+
target = Path(path)
|
| 84 |
+
|
| 85 |
+
# Validaci贸n b谩sica
|
| 86 |
+
if not path.startswith("/data/"):
|
| 87 |
+
raise HTTPException(status_code=400, detail="Path must start with /data/")
|
| 88 |
+
|
| 89 |
+
if item_type not in ("directory", "file"):
|
| 90 |
+
raise HTTPException(status_code=400, detail="item_type must be 'directory' or 'file'")
|
| 91 |
+
|
| 92 |
+
# Protecci贸n de carpetas sensibles
|
| 93 |
+
protected = ["/data/db", "/data/media"]
|
| 94 |
+
|
| 95 |
+
for p in protected:
|
| 96 |
+
if path == p or path.startswith(p + "/"):
|
| 97 |
+
raise HTTPException(
|
| 98 |
+
status_code=403,
|
| 99 |
+
detail=f"Access to protected path '{p}' is not allowed"
|
| 100 |
+
)
|
| 101 |
+
|
| 102 |
+
# No permitir sobrescritura
|
| 103 |
+
if target.exists():
|
| 104 |
+
raise HTTPException(status_code=409, detail="Path already exists")
|
| 105 |
+
|
| 106 |
+
try:
|
| 107 |
+
if item_type == "directory":
|
| 108 |
+
target.mkdir(parents=True, exist_ok=False)
|
| 109 |
+
else:
|
| 110 |
+
target.parent.mkdir(parents=True, exist_ok=True)
|
| 111 |
+
with open(target, "wb") as f:
|
| 112 |
+
f.write(b"")
|
| 113 |
+
except Exception as exc:
|
| 114 |
+
raise HTTPException(status_code=500, detail=f"Failed to create item: {exc}")
|
| 115 |
+
|
| 116 |
+
return {
|
| 117 |
+
"status": "ok",
|
| 118 |
+
"created": str(target),
|
| 119 |
+
"type": item_type
|
| 120 |
+
}
|