141 lines
4.9 KiB
Python
141 lines
4.9 KiB
Python
"""收集故事及其生成记录的素材,解绑后仅删除本故事独占的未收藏素材。"""
|
|
|
|
from fastapi import HTTPException
|
|
from sqlalchemy import and_, or_, select, update
|
|
|
|
from app.models import (
|
|
Asset,
|
|
AssetReference,
|
|
Job,
|
|
JobAsset,
|
|
ShotCandidate,
|
|
ShotSelection,
|
|
Submission,
|
|
WorkflowRun,
|
|
WorkflowStep,
|
|
utcnow,
|
|
)
|
|
from app.services.asset_deletion import normalized_caches, remove_asset, require_unused_asset
|
|
from app.services.events import emit_asset
|
|
|
|
|
|
async def story_asset_ids(session, draft, job_ids):
|
|
def story_ids(model):
|
|
return select(model.id).where(model.draft_id == draft.id, model.owner_id == draft.owner_id)
|
|
|
|
ref = AssetReference
|
|
bindings = select(ref.asset_id).where(
|
|
ref.owner_id == draft.owner_id,
|
|
or_(
|
|
and_(ref.ref_kind == "draft", ref.ref_id == draft.id),
|
|
and_(ref.ref_kind == "run", ref.ref_id.in_(story_ids(WorkflowRun))),
|
|
and_(ref.ref_kind == "candidate", ref.ref_id.in_(story_ids(ShotCandidate))),
|
|
and_(ref.ref_kind == "selection", ref.ref_id.in_(story_ids(ShotSelection))),
|
|
),
|
|
)
|
|
# 手工候选没有生成任务;单独归档候选和更换选片会释放引用,但审计行保留素材归属。
|
|
history = bindings.union(
|
|
select(ShotCandidate.asset_id).where(
|
|
ShotCandidate.draft_id == draft.id, ShotCandidate.owner_id == draft.owner_id
|
|
),
|
|
select(ShotSelection.asset_id).where(
|
|
ShotSelection.draft_id == draft.id, ShotSelection.owner_id == draft.owner_id
|
|
),
|
|
)
|
|
ids = select(Asset.id).where(
|
|
Asset.owner_id == draft.owner_id,
|
|
Asset.library_scope == "private",
|
|
or_(Asset.source_job_id.in_(job_ids), Asset.id.in_(history)),
|
|
)
|
|
return set(await session.scalars(ids))
|
|
|
|
|
|
def exclusive_assets(owner_id, ids):
|
|
consumers = (
|
|
select(JobAsset.job_id)
|
|
.join(Job, Job.id == JobAsset.job_id)
|
|
.where(JobAsset.asset_id == Asset.id, Job.deleted_at.is_(None))
|
|
)
|
|
producer = select(Job.id).where(Job.id == Asset.source_job_id, Job.deleted_at.is_(None))
|
|
reused_job = (
|
|
select(WorkflowStep.id)
|
|
.join(WorkflowRun, WorkflowRun.id == WorkflowStep.run_id)
|
|
.where(
|
|
WorkflowStep.selected_job_id == Asset.source_job_id, WorkflowRun.deleted_at.is_(None)
|
|
)
|
|
)
|
|
return (
|
|
select(Asset)
|
|
.where(
|
|
Asset.id.in_(ids),
|
|
Asset.owner_id == owner_id,
|
|
Asset.library_scope == "private",
|
|
Asset.favorite.is_(False),
|
|
~consumers.exists(),
|
|
~producer.exists(),
|
|
~reused_job.exists(),
|
|
)
|
|
.with_for_update()
|
|
)
|
|
|
|
|
|
async def stage_story_assets(session, draft, asset_ids):
|
|
if not asset_ids:
|
|
return []
|
|
await session.flush()
|
|
assets = (await session.scalars(exclusive_assets(draft.owner_id, asset_ids))).all()
|
|
staged = set()
|
|
|
|
async def hide_if_unused(asset):
|
|
if asset.id in staged:
|
|
return
|
|
try:
|
|
await require_unused_asset(session, asset)
|
|
except HTTPException as exc:
|
|
if isinstance(exc.detail, dict) and exc.detail.get("code") == "asset_in_use":
|
|
return
|
|
raise
|
|
# 与收据一起提交,再动磁盘。文件占用或中途重启时,半清理的素材不能再次绑定、收藏。
|
|
asset.deleted_at = asset.deleted_at or utcnow()
|
|
staged.add(asset.id)
|
|
emit_asset(session, draft.owner_id, "asset.deleted", {"id": asset.id})
|
|
|
|
for asset in assets:
|
|
await hide_if_unused(asset)
|
|
if asset.id in staged and asset.kind == "video" and asset.origin != "normalized":
|
|
caches = await normalized_caches(session, asset)
|
|
if caches:
|
|
cache_ids = [cache.id for cache in caches]
|
|
for cache in await session.scalars(exclusive_assets(draft.owner_id, cache_ids)):
|
|
await hide_if_unused(cache)
|
|
if staged:
|
|
await session.execute(
|
|
update(Submission)
|
|
.where(
|
|
Submission.owner_id == draft.owner_id,
|
|
Submission.status != "purged",
|
|
Submission.resource_json["asset_id"].as_string().in_(staged),
|
|
)
|
|
.values(status="purged")
|
|
)
|
|
return sorted(staged)
|
|
|
|
|
|
async def purge_story_assets(session, settings, owner_id, asset_ids):
|
|
if not asset_ids:
|
|
return
|
|
# 独立执行所保存的收据,重试时直接跳过已删除的行和文件。
|
|
assets = await session.scalars(
|
|
select(Asset)
|
|
.where(
|
|
Asset.owner_id == owner_id,
|
|
Asset.id.in_(asset_ids),
|
|
Asset.library_scope == "private",
|
|
Asset.deleted_at.is_not(None),
|
|
Asset.favorite.is_(False),
|
|
)
|
|
.with_for_update()
|
|
)
|
|
for asset in assets:
|
|
await remove_asset(session, settings, asset, include_caches=False)
|