diff --git a/app/api/v1/audio.py b/app/api/v1/audio.py index 5abbd25..c699535 100644 --- a/app/api/v1/audio.py +++ b/app/api/v1/audio.py @@ -1,11 +1,15 @@ import os import uuid import asyncio -from fastapi import APIRouter, UploadFile, File, HTTPException, Query +import json +from fastapi import APIRouter, UploadFile, File, HTTPException, Query, Depends from fastapi.responses import FileResponse from pydantic import BaseModel -from typing import Optional +from typing import Optional, List from app.config import settings +from app.api.v1.auth import get_current_user +from app.api.v1.projects import get_optional_user +from app.models.user import get_db_connection router = APIRouter() @@ -52,11 +56,12 @@ class PythonToolRequest(BaseModel): wave_type: Optional[str] = "sine" @router.post("/upload") -async def upload_audio(file: UploadFile = File(...)): +async def upload_audio(file: UploadFile = File(...), current_user: Optional[dict] = Depends(get_optional_user)): + user_id = current_user["user_id"] if current_user else "anonymous" ext = os.path.splitext(file.filename)[1] if not ext: ext = ".wav" - file_id = f"{uuid.uuid4()}{ext}" + file_id = f"user_{user_id}_{uuid.uuid4()}{ext}" file_path = os.path.join(settings.UPLOADS_DIR, file_id) with open(file_path, "wb") as f: diff --git a/app/api/v1/projects.py b/app/api/v1/projects.py index 9462484..03ee03f 100644 --- a/app/api/v1/projects.py +++ b/app/api/v1/projects.py @@ -126,3 +126,56 @@ async def list_cloud_projects(current_user: dict = Depends(get_current_user)): "updated_at": r["updated_at"] } for r in rows ] + +@router.get("/cloud/{project_id}") +async def get_cloud_project(project_id: str, current_user: dict = Depends(get_current_user)): + user_id = current_user["user_id"] + conn = get_db_connection() + cursor = conn.cursor() + cursor.execute("SELECT name, data_json FROM projects WHERE id = ? AND user_id = ? AND is_temp = 0", (project_id, user_id)) + row = cursor.fetchone() + conn.close() + + if not row: + raise HTTPException(status_code=404, detail="Không tìm thấy dự án") + + return { + "id": project_id, + "name": row["name"], + "data_json": row["data_json"] + } + +@router.delete("/cloud/{project_id}") +async def delete_cloud_project(project_id: str, current_user: dict = Depends(get_current_user)): + user_id = current_user["user_id"] + conn = get_db_connection() + cursor = conn.cursor() + cursor.execute("DELETE FROM projects WHERE id = ? AND user_id = ? AND is_temp = 0", (project_id, user_id)) + conn.commit() + conn.close() + return {"success": True, "message": "Đã xóa dự án thành công"} + +@router.put("/cloud/{project_id}") +async def update_cloud_project(project_id: str, req: SaveProjectRequest, current_user: dict = Depends(get_current_user)): + user_id = current_user["user_id"] + conn = get_db_connection() + cursor = conn.cursor() + + cursor.execute("SELECT id FROM projects WHERE id = ? AND user_id = ? AND is_temp = 0", (project_id, user_id)) + exists = cursor.fetchone() + if not exists: + conn.close() + raise HTTPException(status_code=404, detail="Không tìm thấy dự án để cập nhật") + + new_size_bytes = len(req.data_json.encode("utf-8")) + now = time.time() + + cursor.execute(""" + UPDATE projects + SET name = ?, data_json = ?, size_bytes = ?, updated_at = ? + WHERE id = ? AND user_id = ? + """, (req.name, req.data_json, new_size_bytes, now, project_id, user_id)) + + conn.commit() + conn.close() + return {"success": True, "message": "Đã cập nhật dự án thành công"} diff --git a/app/storage/sonicforge.db b/app/storage/sonicforge.db index 330f740..efec6d9 100644 Binary files a/app/storage/sonicforge.db and b/app/storage/sonicforge.db differ