nex_animation/backend/app/services/versions.py

326 lines
15 KiB
Python
Raw Permalink 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.

"""素材版本管理:把任意历史版本设为当前版本,或删除某个版本。
平台母版(`PlatformMedia` + `PlatformMediaVersion`)与项目资产
(`Asset` + `AssetVersion`)的版本表字段同构,差别只在父表、外键列与事件归属,
因此用一个 `Store` 描述符抹平差异,两侧接口共享同一份实现与同一套提示文案。
为什么「设为当前版本」不能只改 `current_version`:
`current_version` 只是给列表/预览看的数字,真正决定下游行为的是父行的
`path/checksum/size_bytes/width/height/duration_ms/mime`——
引入项目时 `_copy_media()` 复制的是 `media.path`,生成参考图取的是 `row.path`。
所以切换当前版本必须把这一整组字段一起换掉,否则页面上写着 v3、实际出片还是 v1。
为什么删除版本要格外小心:
同一个物理文件可能被多行同时引用(父行指向它、另一条版本行也指向它),
一旦误删文件,素材会变成「有记录、无画面」。因此删除前先全库扫一遍引用,
仍被引用的文件保留并如实告知前端原因。
"""
from __future__ import annotations
import mimetypes
from collections.abc import Callable
from dataclasses import dataclass
from typing import Any
from sqlalchemy import or_, select, text
from sqlalchemy.orm import Session
from app.models import (Asset, AssetVersion, PlatformMedia, PlatformMediaVersion, PlatformVoice, VoiceAsset)
from app.services import events, storage
from app.services import ffmpeg as ff
class VersionError(Exception):
"""版本操作的业务失败。API 层原样转成 HTTPException(status_code, detail)。"""
def __init__(self, status_code: int, detail: str):
super().__init__(detail)
self.status_code = status_code
self.detail = detail
@dataclass(frozen=True)
class Store:
"""一套「可版本化素材」的坐标:父表、版本表、外键列与事件口径。"""
key: str # "platform" / "project"
label: str # 面向用户的称呼
parent_model: type
version_model: type
owner_column: Any # PlatformMediaVersion.media_id / AssetVersion.asset_id
event_prefix: str # platform.media / asset
entity_type: str # platform_media / asset
serialize: Callable[[Any], dict]
project_of: Callable[[Any], int | None] = staticmethod(lambda row: None)
PLATFORM_MEDIA = Store(
key="platform", label="平台素材", parent_model=PlatformMedia, version_model=PlatformMediaVersion,
owner_column=PlatformMediaVersion.media_id, event_prefix="platform.media", entity_type="platform_media",
serialize=storage.platform_media_dict,
)
PROJECT_ASSET = Store(
key="project", label="项目资产", parent_model=Asset, version_model=AssetVersion,
owner_column=AssetVersion.asset_id, event_prefix="asset", entity_type="asset",
serialize=storage.asset_dict, project_of=lambda row: getattr(row, "project_id", None),
)
STORES = {store.key: store for store in (PLATFORM_MEDIA, PROJECT_ASSET)}
def store_for(key: str) -> Store:
store = STORES.get((key or "").strip().lower())
if store is None:
raise VersionError(404, f"不支持的素材类型 {key}")
return store
# ---------------------------------------------------------------- 内部工具
def _parent(db: Session, store: Store, obj_id: int):
parent = db.get(store.parent_model, obj_id)
if parent is None:
raise VersionError(404, f"{store.label} #{obj_id} 不存在")
return parent
def _rows(db: Session, store: Store, parent) -> list:
rows = db.query(store.version_model).filter(store.owner_column == parent.id).order_by(
store.version_model.version_no.asc()).all()
if not rows:
raise VersionError(400, f"{_name(parent)} 还没有版本记录,请先上传一版")
return rows
def _name(parent) -> str:
return str(getattr(parent, "name", "") or f"#{getattr(parent, 'id', '')}")
def _label(version_no) -> str:
return f"v{version_no}"
def _nos(rows) -> str:
return "、".join(_label(row.version_no) for row in rows)
def _probe_info(path: str) -> dict:
"""版本行缺少的宽高/时长/mime 现测一次——历史版本行没有这些列。"""
info: dict = {}
if not path:
return info
if ff.available() and storage.exists(path):
try:
probe = ff.probe(storage.to_absolute(path))
if probe.width:
info["width"] = int(probe.width)
if probe.height:
info["height"] = int(probe.height)
if probe.duration:
info["duration_ms"] = int(round(probe.duration * 1000))
except Exception: # noqa: BLE001 探测失败不阻断切版本
pass
if not info.get("mime"):
info["mime"] = mimetypes.guess_type(path)[0] or ""
return info
def _version_info(version) -> dict:
stored = {"width": getattr(version, "width", None), "height": getattr(version, "height", None),
"duration_ms": getattr(version, "duration_ms", None),
"mime": getattr(version, "mime", "") or ""}
if not (stored["width"] and stored["height"]) or not stored["mime"]:
probed = _probe_info(version.path)
for field_name, value in probed.items():
if value in (None, "", 0) and field_name != "mime":
continue
if stored.get(field_name) in (None, "", 0):
stored[field_name] = value
return stored
def _apply(db: Session, store: Store, parent, version) -> None:
"""把「当前版本」整体切到指定版本行:数字与文件指针必须同步。"""
info = _version_info(version)
parent.current_version = int(version.version_no)
parent.path = version.path
parent.checksum = version.checksum or parent.checksum
parent.size_bytes = int(version.size_bytes or 0) or parent.size_bytes
# 宽高/时长测不出来时保留旧值(总比写 0 强),能测到就一定覆盖,
# 否则切版本后画面尺寸还停在上一版,生成时会按错误的比例出图。
parent.width = info.get("width") or parent.width
parent.height = info.get("height") or parent.height
parent.duration_ms = info.get("duration_ms") or parent.duration_ms
if info.get("mime"):
parent.mime = info["mime"]
db.flush()
def _path_variants(path: str | None) -> list[str]:
if not path:
return []
values = [str(path), storage.to_relative(path), str(storage.to_absolute(path))]
out: list[str] = []
for value in values:
if value and value not in out:
out.append(value)
return out
def _repoint_voice_reference(db: Session, old_path: str | None, new_path: str | None) -> list[str]:
"""当前版本一换,声线的参考音频必须跟着换。
`PlatformVoice.provider_voice_id` / `VoiceAsset.provider_voice_id` 存的是
「具体某一版的文件路径」,而不是「当前版本」这个概念(IndexTTS2 直接拿它当
克隆参考音频)。换当前版本时若只改素材父行,页面显示 v3、配音还是 v1 的音色,
所以这里把指向旧文件的声线一并挪到新文件上,并如实回给前端。
"""
variants = _path_variants(old_path)
if not variants or not new_path or storage.to_relative(old_path) == storage.to_relative(new_path):
return []
touched: list[str] = []
for model, label in ((PlatformVoice, "平台声线"), (VoiceAsset, "项目声线")):
rows = db.scalars(select(model).where(or_(*[model.provider_voice_id == value for value in variants]))).all()
for row in rows:
row.provider_voice_id = new_path
touched.append(f"{label} #{row.id}")
if touched:
db.flush()
return touched
def _file_referrers(db: Session, path: str | None) -> list[str]:
"""全库找还有哪些记录指向这个文件(被删的那行已经 flush 掉,不会自己挡自己)。"""
variants = _path_variants(path)
if not variants:
return []
found: list[str] = []
def scan(model, column, label):
stmt = select(model).where(or_(*[column == value for value in variants]))
for row in db.scalars(stmt).all():
extra = f" {_label(row.version_no)}" if hasattr(row, "version_no") else ""
found.append(f"{label} #{row.id}{extra}")
scan(PlatformMedia, PlatformMedia.path, "平台素材")
scan(Asset, Asset.path, "项目资产")
scan(PlatformMediaVersion, PlatformMediaVersion.path, "平台素材版本")
scan(AssetVersion, AssetVersion.path, "项目资产版本")
scan(PlatformVoice, PlatformVoice.provider_voice_id, "平台声线")
scan(VoiceAsset, VoiceAsset.provider_voice_id, "项目声线")
# 生成台账里存的是当时的输入快照,删了文件就没法复现历史出片。
like = " OR ".join(f"asset_paths LIKE :p{i} OR params_json LIKE :p{i}" for i in range(len(variants)))
params = {f"p{i}": f"%{value}%" for i, value in enumerate(variants)}
for row in db.execute(text(f"SELECT id FROM generation_records WHERE {like} LIMIT 5"), params):
found.append(f"生成记录 #{row[0]}")
return found
# ---------------------------------------------------------------- 对外能力
def set_current(db: Session, store: Store, obj_id: int, version_no: int, *,
actor: str = "system") -> dict:
"""将指定版本设为当前版本(会同步换掉父行指向的文件)。"""
parent = _parent(db, store, obj_id)
rows = _rows(db, store, parent)
try:
wanted = int(version_no)
except (TypeError, ValueError):
raise VersionError(400, f"版本号 {version_no} 不是整数")
target = next((row for row in rows if int(row.version_no) == wanted), None)
if target is None:
raise VersionError(404, f"{_name(parent)} 没有 {_label(wanted)};可用版本:{_nos(rows)}")
if not storage.exists(target.path):
raise VersionError(400, f"{_name(parent)} 的 {_label(wanted)} 文件已丢失({target.path}),"
f"无法设为当前版本;建议删除该版本后重新上传")
previous = int(parent.current_version or 0)
changed = previous != int(target.version_no)
old_path = parent.path
_apply(db, store, parent, target)
# 只有真的换了版本才动声线:没变化时下面会 rollback,写一半更难看。
repointed = _repoint_voice_reference(db, old_path, parent.path) if changed else []
payload = {
"scope": store.key, "id": parent.id, "kind": getattr(parent, "kind", ""),
"name": _name(parent), "previous_version": previous,
"current_version": int(parent.current_version), "changed": changed,
"path": parent.path, "url": storage.url_for(parent.path),
"version_count": len(rows), "voice_references_updated": repointed,
"media": store.serialize(parent),
}
if payload["changed"]:
events.log_event(db, f"{store.event_prefix}.version.current", project_id=store.project_of(parent),
message=f"{_name(parent)}:{_label(previous)} → 当前版本改为 {_label(target.version_no)}"
+ (f";参考音频跟随到新版本({'、'.join(repointed)})" if repointed else ""),
entity_type=store.entity_type, entity_id=parent.id,
data={"version_no": int(target.version_no), "previous_version": previous,
"path": parent.path, "voice_references_updated": repointed}, actor=actor)
db.commit()
else:
db.rollback() # 重复点击「设为当前」不该制造事件,也不该产生写入
return payload
def delete_version(db: Session, store: Store, obj_id: int, version_no: int, *,
actor: str = "system") -> dict:
"""删除指定版本;删的是当前版本时自动回落到剩余的最新版本。"""
parent = _parent(db, store, obj_id)
rows = _rows(db, store, parent)
try:
wanted = int(version_no)
except (TypeError, ValueError):
raise VersionError(400, f"版本号 {version_no} 不是整数")
target = next((row for row in rows if int(row.version_no) == wanted), None)
if target is None:
raise VersionError(404, f"{_name(parent)} 没有 {_label(wanted)};可用版本:{_nos(rows)}")
if len(rows) <= 1:
raise VersionError(400, f"{_name(parent)} 只剩 {_label(wanted)} 一个版本,"
f"删除后素材就没有可用文件了;如需清理请直接删除该素材")
path = target.path
was_current = int(parent.current_version or 0) == wanted
remaining = [row for row in rows if int(row.version_no) != wanted]
db.delete(target)
db.flush()
# 直接删子表行不会自动从父行已加载的 versions 关系里摘掉,
# 不过期的话响应里的版本列表会带着刚删掉的那一条。
db.expire(parent, ["versions"])
fallback = None
repointed: list[str] = []
if was_current:
fallback = max(remaining, key=lambda row: int(row.version_no))
_apply(db, store, parent, fallback)
repointed = _repoint_voice_reference(db, path, parent.path)
# 当前版本回落后,父行已经指向别的文件;若仍有别的记录共用这个路径就不动磁盘。
referrers = _file_referrers(db, path)
file_removed = False
kept_reason = ""
if referrers:
kept_reason = "文件仍被 " + "、".join(referrers[:4]) + " 引用,已保留"
else:
file_removed = storage.delete_media_file(path)
payload = {
"scope": store.key, "id": parent.id, "kind": getattr(parent, "kind", ""),
"name": _name(parent), "deleted_version": wanted, "was_current": was_current,
"current_version": int(parent.current_version),
"fallback_version": int(fallback.version_no) if fallback is not None else None,
"versions": [int(row.version_no) for row in remaining], "version_count": len(remaining),
"file_removed": file_removed, "file_kept_reason": kept_reason,
"voice_references_updated": repointed,
"path": parent.path, "url": storage.url_for(parent.path),
"media": store.serialize(parent),
}
events.log_event(db, f"{store.event_prefix}.version.deleted", project_id=store.project_of(parent),
message=f"{_name(parent)}:删除版本 {_label(wanted)}"
+ (f",当前版本回落到 {_label(payload['current_version'])}" if was_current else "")
+ (f";参考音频跟随到新版本({'、'.join(repointed)})" if repointed else "")
+ (",文件已清理" if file_removed else ""),
entity_type=store.entity_type, entity_id=parent.id,
data={"version_no": wanted, "was_current": was_current, "path": path,
"file_removed": file_removed, "kept_reason": kept_reason,
"voice_references_updated": repointed},
actor=actor)
db.commit()
return payload