feat: 优化图片资源删除管理,生图任务列表管理
This commit is contained in:
@@ -7,6 +7,7 @@ from __future__ import annotations
|
||||
import uuid
|
||||
|
||||
from schemas.suite import TextMaterial
|
||||
from services.image_assets import get_image_asset_service
|
||||
from services.tasks import TaskImage
|
||||
|
||||
|
||||
@@ -16,6 +17,8 @@ def texts_to_raw(texts: list[TextMaterial]) -> dict:
|
||||
for t in texts:
|
||||
if t.kind == "title" and t.content:
|
||||
raw["title"] = t.content
|
||||
elif t.kind == "title_ru" and t.content:
|
||||
raw["title_ru"] = t.content
|
||||
elif t.kind == "price" and t.content:
|
||||
raw["price"] = t.content
|
||||
elif t.kind == "brand" and t.content:
|
||||
@@ -67,6 +70,9 @@ async def append_generated_asset(
|
||||
|
||||
pid = uuid.UUID(product_id)
|
||||
async with get_session_factory()() as db:
|
||||
product = await db.get(Product, pid)
|
||||
if product is None:
|
||||
raise RuntimeError("商品已删除,生成结果不再回写")
|
||||
sort_order: int | None = None
|
||||
if after_asset_id:
|
||||
try:
|
||||
@@ -104,16 +110,30 @@ async def append_generated_asset(
|
||||
sort_order=sort_order,
|
||||
type="img",
|
||||
source_url="",
|
||||
stored_url=image.url,
|
||||
status="uploaded",
|
||||
)
|
||||
db.add(asset)
|
||||
await db.flush()
|
||||
|
||||
product = await db.get(Product, pid)
|
||||
if product is not None:
|
||||
counts = dict(product.asset_counts or {})
|
||||
counts["generated"] = int(counts.get("generated") or 0) + 1
|
||||
product.asset_counts = counts
|
||||
image_assets = get_image_asset_service()
|
||||
original_url = image.source_url or image.url
|
||||
await image_assets.adopt_generated_versions(
|
||||
asset,
|
||||
original_url=original_url,
|
||||
current_url=image.url,
|
||||
watermark_config=image.watermark_config,
|
||||
)
|
||||
|
||||
counts = dict(product.asset_counts or {})
|
||||
counts["generated"] = int(counts.get("generated") or 0) + 1
|
||||
product.asset_counts = counts
|
||||
await db.commit()
|
||||
# task 的 suites/ 临时对象已迁入正式素材路径,提交成功后立即回收。
|
||||
try:
|
||||
await image_assets.delete_temporary_urls([image.source_url, image.url])
|
||||
except Exception: # noqa: BLE001
|
||||
# 当前素材已可用;临时对象残留可由删除商品时的前缀清理兜底。
|
||||
pass
|
||||
image.source_url = asset.original_url or ""
|
||||
image.url = asset.stored_url or ""
|
||||
return str(asset.id)
|
||||
|
||||
Reference in New Issue
Block a user