54 lines
1.7 KiB
Python
54 lines
1.7 KiB
Python
from fastapi import APIRouter, UploadFile, File, HTTPException
|
|
from app.core.config import settings
|
|
import shutil
|
|
import uuid
|
|
from pathlib import Path
|
|
|
|
router = APIRouter()
|
|
|
|
@router.post("/")
|
|
async def upload_material(file: UploadFile = File(...)):
|
|
if not file.filename.lower().endswith(('.mp4', '.mov', '.avi')):
|
|
raise HTTPException(400, "Invalid format")
|
|
|
|
file_id = str(uuid.uuid4())
|
|
ext = Path(file.filename).suffix
|
|
save_path = settings.UPLOAD_DIR / "materials" / f"{file_id}{ext}"
|
|
|
|
# Save file
|
|
with open(save_path, "wb") as buffer:
|
|
shutil.copyfileobj(file.file, buffer)
|
|
|
|
# Calculate size
|
|
size_mb = save_path.stat().st_size / (1024 * 1024)
|
|
|
|
return {
|
|
"id": file_id,
|
|
"name": file.filename,
|
|
"path": f"uploads/materials/{file_id}{ext}",
|
|
"size_mb": size_mb,
|
|
"type": "video"
|
|
}
|
|
|
|
@router.get("/")
|
|
async def list_materials():
|
|
materials_dir = settings.UPLOAD_DIR / "materials"
|
|
files = []
|
|
if materials_dir.exists():
|
|
for f in materials_dir.glob("*"):
|
|
try:
|
|
stat = f.stat()
|
|
files.append({
|
|
"id": f.stem,
|
|
"name": f.name,
|
|
"path": f"uploads/materials/{f.name}",
|
|
"size_mb": stat.st_size / (1024 * 1024),
|
|
"type": "video",
|
|
"created_at": stat.st_ctime
|
|
})
|
|
except Exception:
|
|
continue
|
|
# Sort by creation time desc
|
|
files.sort(key=lambda x: x.get("created_at", 0), reverse=True)
|
|
return {"materials": files}
|