9c0474ef9c
CI/CD Pipeline / Dedup Check - skip PR tests when covered by push pipeline (push) Successful in 1s
CI/CD Pipeline / Check if frontend-only change (pull_request) Successful in 3s
CI/CD Pipeline / Dedup Check - skip PR tests when covered by push pipeline (pull_request) Successful in 3s
CI/CD Pipeline / Check push changed paths (push) Successful in 7s
CI/CD Pipeline / PR Build API Image (pull_request) Successful in 41s
CI/CD Pipeline / PR Build Worker Image (pull_request) Successful in 42s
CI/CD Pipeline / Production Browser E2E (pull_request) Has been skipped
CI/CD Pipeline / Build Staging API Image (push) Successful in 1m0s
CI/CD Pipeline / Build Staging Worker Image (push) Successful in 34s
CI/CD Pipeline / Validate - Python (mypy + alembic) (push) Successful in 2m32s
Preview Deploy / Deploy Preview Environment (pull_request) Successful in 2m50s
CI/CD Pipeline / PR Build Web Image (pull_request) Successful in 2m53s
CI/CD Pipeline / Integration Tests (push) Successful in 2m56s
CI/CD Pipeline / Build Staging Web Image (push) Successful in 2m43s
CI/CD Pipeline / CI Gate (pull_request) Successful in 1s
CI/CD Pipeline / Validate - Style (push) Successful in 3m19s
CI/CD Pipeline / Deploy Staging (Watchtower auto-deploy) (push) Successful in 50s
PR Automation / Auto Approve on CI Green (pull_request) Successful in 4m7s
CI/CD Pipeline / Validate - Security (push) Successful in 5m45s
CI/CD Pipeline / Staging E2E Tests (push) Failing after 1m54s
CI/CD Pipeline / Frontend Unit Tests (push) Successful in 6m6s
CI/CD Pipeline / ACR Image Cleanup (push) Successful in 2m13s
AI Code Review / AI Code Review (pull_request) Successful in 6m38s
CI/CD Pipeline / Unit Tests (push) Failing after 7m58s
CI/CD Pipeline / Production Browser E2E (push) Has been skipped
CI/CD Pipeline / Staging API Integration Tests (push) Has been cancelled
CI/CD Pipeline / Canary Release to Production (push) Has been cancelled
CI/CD Pipeline / Deploy Production (push) Failing after 198h32m29s
CI/CD Pipeline / CI Gate (push) Failing after 198h32m31s
CI/CD Pipeline / Build Production Worker Image (push) Failing after 198h32m31s
CI/CD Pipeline / Build Production Web Image (push) Failing after 198h32m31s
CI/CD Pipeline / Build Production API Image (push) Failing after 198h32m31s
PR Automation / Auto Merge on CI Green + Approved (pull_request) Failing after 198h36m16s
CI/CD Pipeline / Retag skipped Staging Worker Image (push) Failing after 198h37m22s
CI/CD Pipeline / Retag skipped Staging Web Image (push) Failing after 198h37m23s
CI/CD Pipeline / Canary Release to Production (pull_request) Failing after 198h39m3s
CI/CD Pipeline / Retag skipped Staging API Image (push) Failing after 198h37m23s
CI/CD Pipeline / ACR Image Cleanup (pull_request) Failing after 198h39m11s
CI/CD Pipeline / Staging API Integration Tests (pull_request) Failing after 198h39m14s
CI/CD Pipeline / Staging E2E Tests (pull_request) Failing after 198h39m20s
CI/CD Pipeline / Retag skipped Staging API Image (pull_request) Failing after 198h39m40s
CI/CD Pipeline / Deploy Staging (Watchtower auto-deploy) (pull_request) Failing after 198h39m27s
CI/CD Pipeline / Deploy Production (pull_request) Failing after 198h39m33s
CI/CD Pipeline / Build Production Worker Image (pull_request) Failing after 198h39m42s
CI/CD Pipeline / Build Production API Image (pull_request) Failing after 198h39m45s
CI/CD Pipeline / Retag skipped Staging Worker Image (pull_request) Failing after 198h39m39s
CI/CD Pipeline / Build Staging Worker Image (pull_request) Failing after 198h40m27s
CI/CD Pipeline / Build Staging API Image (pull_request) Failing after 198h40m28s
CI/CD Pipeline / Frontend Unit Tests (pull_request) Failing after 198h40m29s
CI/CD Pipeline / Integration Tests (pull_request) Failing after 198h40m29s
CI/CD Pipeline / Unit Tests (pull_request) Failing after 198h40m29s
CI/CD Pipeline / Validate - Security (pull_request) Failing after 198h40m30s
CI/CD Pipeline / Validate - Style (pull_request) Failing after 198h40m30s
CI/CD Pipeline / Frontend Lint (push) Failing after 198h40m30s
CI/CD Pipeline / PR Build Worker Image (push) Failing after 198h40m37s
CI/CD Pipeline / PR Build API Image (push) Failing after 198h40m37s
CI/CD Pipeline / Check push changed paths (pull_request) Failing after 198h40m37s
CI/CD Pipeline / Check if frontend-only change (push) Failing after 198h40m39s
CI/CD Pipeline / Retag skipped Staging Web Image (pull_request) Failing after 199h14m21s
CI/CD Pipeline / Build Production Web Image (pull_request) Failing after 199h14m26s
CI/CD Pipeline / Build Staging Web Image (pull_request) Failing after 199h15m10s
CI/CD Pipeline / Frontend Lint (pull_request) Failing after 199h15m11s
CI/CD Pipeline / Validate - Python (mypy + alembic) (pull_request) Failing after 199h15m11s
CI/CD Pipeline / PR Build Web Image (push) Failing after 199h15m19s
Squash merge PR #1783 into develop
154 lines
6.1 KiB
Python
Executable File
154 lines
6.1 KiB
Python
Executable File
from typing import Any
|
||
|
||
from app.auth import AuthenticatedUser, get_current_user
|
||
from app.dependencies import (
|
||
get_asset_library_repository,
|
||
get_asset_repository,
|
||
get_project_repository,
|
||
)
|
||
from app.schemas.asset_library import (
|
||
AssetLibraryResponse,
|
||
CreateAssetLibraryRequest,
|
||
EnsureDefaultLibraryRequest,
|
||
ListAssetLibrariesResponse,
|
||
)
|
||
from fastapi import APIRouter, Depends, HTTPException, Query, Response, status
|
||
|
||
from packages.application import (
|
||
CreateAssetLibraryCommand,
|
||
CreateAssetLibraryUseCase,
|
||
GetProjectUseCase,
|
||
ListAssetLibrariesUseCase,
|
||
)
|
||
from packages.domain import AssetLibraryKind
|
||
|
||
from ._helpers import check_project_access
|
||
|
||
router = APIRouter()
|
||
|
||
|
||
def _to_asset_library_response(item) -> AssetLibraryResponse:
|
||
return AssetLibraryResponse(
|
||
id=item.id,
|
||
project_id=item.project_id,
|
||
name=item.name,
|
||
kind=item.kind.value,
|
||
asset_count=item.asset_count,
|
||
total_size=item.total_size,
|
||
)
|
||
|
||
|
||
@router.get("", response_model=ListAssetLibrariesResponse)
|
||
def list_asset_libraries(
|
||
project_id: str | None = Query(None),
|
||
kind: str | None = Query(None, pattern="^(video|voice|image)$"),
|
||
authenticated_user: AuthenticatedUser = Depends(get_current_user),
|
||
asset_library_repository: Any = Depends(get_asset_library_repository),
|
||
project_repository: Any = Depends(get_project_repository),
|
||
) -> ListAssetLibrariesResponse:
|
||
user_id = authenticated_user.user.id
|
||
use_case = ListAssetLibrariesUseCase(asset_library_repository)
|
||
|
||
if project_id:
|
||
# If project_id provided, check access and filter by project
|
||
project = GetProjectUseCase(project_repository).execute(project_id)
|
||
if project is None:
|
||
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Project not found")
|
||
if not project.can_access(user_id):
|
||
raise HTTPException(status_code=status.HTTP_403_FORBIDDEN, detail="Access denied to project")
|
||
items = use_case.execute(project_id)
|
||
else:
|
||
# If no project_id, list all libraries from accessible projects
|
||
accessible_projects = project_repository.find_accessible_projects(user_id)
|
||
all_items = []
|
||
for proj in accessible_projects:
|
||
all_items.extend(use_case.execute(proj.id))
|
||
items = all_items
|
||
|
||
# 按 kind 过滤(可选)
|
||
if kind:
|
||
kind_enum = AssetLibraryKind(kind)
|
||
items = [item for item in items if item.kind == kind_enum]
|
||
|
||
return ListAssetLibrariesResponse(items=[_to_asset_library_response(item) for item in items])
|
||
|
||
|
||
@router.post("", response_model=AssetLibraryResponse)
|
||
def create_asset_library(
|
||
request: CreateAssetLibraryRequest,
|
||
authenticated_user: AuthenticatedUser = Depends(get_current_user),
|
||
asset_library_repository: Any = Depends(get_asset_library_repository),
|
||
project_repository: Any = Depends(get_project_repository),
|
||
) -> AssetLibraryResponse:
|
||
project = project_repository.find_by_id(request.project_id)
|
||
if project is None:
|
||
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Project not found")
|
||
if not project.can_access(authenticated_user.user.id):
|
||
raise HTTPException(status_code=status.HTTP_403_FORBIDDEN, detail="Access denied to project")
|
||
use_case = CreateAssetLibraryUseCase(asset_library_repository)
|
||
item = use_case.execute(
|
||
CreateAssetLibraryCommand(
|
||
project_id=request.project_id,
|
||
name=request.name,
|
||
kind=AssetLibraryKind(request.kind),
|
||
)
|
||
)
|
||
return _to_asset_library_response(item)
|
||
|
||
|
||
# 默认素材库名称映射
|
||
_DEFAULT_LIBRARY_NAMES = {
|
||
"video": "视频素材库",
|
||
"voice": "配音素材库",
|
||
"image": "图片素材库",
|
||
}
|
||
|
||
|
||
@router.post("/ensure-default", response_model=AssetLibraryResponse)
|
||
def ensure_default_library(
|
||
request: EnsureDefaultLibraryRequest,
|
||
authenticated_user: AuthenticatedUser = Depends(get_current_user),
|
||
asset_library_repository: Any = Depends(get_asset_library_repository),
|
||
project_repository: Any = Depends(get_project_repository),
|
||
) -> AssetLibraryResponse:
|
||
"""确保项目下指定 kind 的默认素材库存在,已存在则直接返回,不存在则自动创建。"""
|
||
project = project_repository.find_by_id(request.project_id)
|
||
if project is None:
|
||
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Project not found")
|
||
if not project.can_access(authenticated_user.user.id):
|
||
raise HTTPException(status_code=status.HTTP_403_FORBIDDEN, detail="Access denied to project")
|
||
|
||
kind = AssetLibraryKind(request.kind)
|
||
|
||
# Issue #1775: 幂等获取/创建——依赖唯一约束 uq_asset_libraries_project_kind,
|
||
# 并发创建冲突时回滚重查返回已有记录,不再依赖应用层"先查后插",也不会 500。
|
||
default_name = _DEFAULT_LIBRARY_NAMES.get(request.kind, f"{request.kind}素材库")
|
||
library = asset_library_repository.get_or_create_default_library(request.project_id, kind, name=default_name)
|
||
return _to_asset_library_response(library)
|
||
|
||
|
||
@router.delete("/{library_id}", status_code=status.HTTP_204_NO_CONTENT, response_class=Response)
|
||
def delete_asset_library(
|
||
library_id: str,
|
||
authenticated_user: AuthenticatedUser = Depends(get_current_user),
|
||
asset_library_repository: Any = Depends(get_asset_library_repository),
|
||
asset_repository: Any = Depends(get_asset_repository),
|
||
project_repository: Any = Depends(get_project_repository),
|
||
) -> None:
|
||
"""删除素材库,同时删除库内所有素材。"""
|
||
# 查找素材库
|
||
library = asset_library_repository.find_by_id(library_id)
|
||
if library is None:
|
||
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="素材库不存在")
|
||
|
||
# 权限校验:检查用户是否有项目访问权限
|
||
check_project_access(library.project_id, authenticated_user.user.id, project_repository)
|
||
|
||
# 删除库内所有素材(硬删除,素材库已删除,无需保留软删除状态)
|
||
assets_in_library = asset_repository.find_by_library(library_id)
|
||
for asset in assets_in_library:
|
||
asset_repository.delete(asset.id)
|
||
|
||
# 删除素材库本身
|
||
asset_library_repository.delete(library_id)
|