nex_animation/backend/app/api/deps.py

130 lines
6.0 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

"""API 层公共依赖:实体查找、统一响应、素材上传与前置检查刷新。"""
from __future__ import annotations
from fastapi import HTTPException, UploadFile
from sqlalchemy.orm import Session
from app.constants import AssetKind
from app.models import Asset, Character, Project, Scene, Shot, Task, Timeline, VoiceAsset
from app.services import dispatch, events, preflight, storage, status
# 资产库允许上传的素材类型(图1 模块①:人物与声音资产)
OWNER_KINDS: dict[str, set[str]] = {
AssetKind.HUMAN: {"portrait", "turnaround", "costume", "expression", "voice_sample", "reference"},
AssetKind.SCENE: {"scene_image", "reference"},
AssetKind.SHOT: {"keyframe", "last_frame", "reference_image", "reference_video"},
AssetKind.PROJECT: {"bgm", "sfx", "reference"},
"voice": {"voice_sample", "reference"},
}
def get_project(db: Session, project_id: int) -> Project:
project = db.get(Project, project_id)
if project is None:
raise HTTPException(404, "项目不存在")
return project
def get_shot(db: Session, project_id: int, shot_id: int) -> Shot:
shot = db.get(Shot, shot_id)
if shot is None or shot.project_id != project_id:
raise HTTPException(404, "镜头不存在")
return shot
def get_character(db: Session, project_id: int, character_id: int) -> Character:
row = db.get(Character, character_id)
if row is None or row.project_id != project_id:
raise HTTPException(404, "角色不存在")
return row
def get_scene(db: Session, project_id: int, scene_id: int) -> Scene:
row = db.get(Scene, scene_id)
if row is None or row.project_id != project_id:
raise HTTPException(404, "场景不存在")
return row
def get_voice(db: Session, project_id: int, voice_id: int) -> VoiceAsset:
row = db.get(VoiceAsset, voice_id)
if row is None or row.project_id != project_id:
raise HTTPException(404, "声线资产不存在")
return row
def get_task(db: Session, task_id: int) -> Task:
task = db.get(Task, task_id)
if task is None:
raise HTTPException(404, "任务不存在")
return task
def get_timeline(db: Session, project_id: int, timeline_id: int | None = None) -> Timeline:
if timeline_id:
timeline = db.get(Timeline, timeline_id)
if timeline is None or timeline.project_id != project_id:
raise HTTPException(404, "时间线不存在")
return timeline
timeline = status.current_timeline(db, get_project(db, project_id))
if timeline is None:
raise HTTPException(404, "尚未建立时间线,请先排布时间线")
return timeline
def summary(db: Session, project: Project) -> dict:
"""统一的「操作后摘要」:状态、统计与图2 流程进度。"""
status.refresh(db, project, commit=False)
db.commit()
return {"status": project.status, "current_stage": project.current_stage,
"stage_label": status.STAGE_LABELS.get(project.current_stage, project.current_stage),
"stats": status.stats_of(db, project), "workflow": status.workflow_of(db, project)}
def done(db: Session, project: Project, result: dict | None = None, **extra) -> dict:
return {"ok": True, "result": result or {}, "summary": summary(db, project), **extra}
def recheck(db: Session, project: Project) -> dict:
"""素材/参数变更后重跑前置检查并回写镜头 draft↔ready。"""
db.flush()
db.expire(project, ["shots"]) # 刚新增/删除的分镜也要参与检查
readiness = preflight.project_readiness(db, project)
preflight.apply_status(db, project, readiness)
db.commit()
return readiness
async def save_asset_file(db: Session, file: UploadFile, *, project: Project, owner_type: str, owner_id: int,
kind: str, name: str = "", note: str = "", stem: str = "asset") -> Asset:
allowed = OWNER_KINDS.get(owner_type, set())
if kind not in allowed:
raise HTTPException(400, f"该资产位只接受 {', '.join(sorted(allowed)) or '(无)'},收到 {kind}")
# 「reference」是按用途命名的通用槽位;声线的 reference 实际是参考音频,
# 不能沿用默认的图片扩展名白名单。
media_group = "audio" if owner_type == "voice" and kind == "reference" else ""
info = await storage.save_upload(file, project_id=project.id, kind=kind, stem=stem,
media_group=media_group)
asset = storage.register_asset(db, project_id=project.id, owner_type=owner_type, owner_id=owner_id,
kind=kind, path=info["path"], name=name or info["original_name"],
note=note, source="upload", info=info)
db.commit()
events.log_event(db, "asset.uploaded", project_id=project.id,
shot_id=owner_id if owner_type == "shot" else None,
message=f"上传{AssetKind.LABELS.get(kind, kind)}:{asset.name}",
entity_type="asset", entity_id=asset.id,
data={"kind": kind, "owner_type": owner_type, "owner_id": owner_id, "path": info["path"]})
return asset
def enqueue_generation(db: Session, project: Project, *, shot_ids: list[int] | None = None,
force: bool = False, allow_unready: bool = False, priority: int = 5,
actor: str = "studio", with_voiceover: bool = True) -> dict:
"""生成分镜任务清单并入队(图2:任务清单 → 任务队列调度)。"""
plan = dispatch.plan_task_list(db, project, shot_ids=shot_ids, force=force,
allow_unready=allow_unready, include_voiceover=with_voiceover)
created = dispatch.create_generation_tasks(db, project, plan, shot_ids=shot_ids, force=force,
allow_unready=allow_unready, priority=priority, actor=actor,
with_voiceover=with_voiceover)
return {"plan": plan, "created": created}