feat(assets): protect referenced stickers from deletion
This commit is contained in:
@@ -1399,6 +1399,26 @@ def _zip_asset_entry(zf: zipfile.ZipFile, pkg_id: str) -> str:
|
||||
return sorted(candidates)[0]
|
||||
|
||||
|
||||
def _asset_reference_names(asset_id: str) -> list[str]:
|
||||
usages: list[str] = []
|
||||
data_sources = (
|
||||
(DESIGN_TEMPLATES_DIR, "template.json"),
|
||||
(PROJECTS_DIR, "project.json"),
|
||||
)
|
||||
for root, filename in data_sources:
|
||||
for item in _list_dirs(root):
|
||||
path = item / filename
|
||||
if not path.exists():
|
||||
continue
|
||||
try:
|
||||
data = json.loads(path.read_text(encoding="utf-8"))
|
||||
except Exception:
|
||||
continue
|
||||
if asset_id in json.dumps(data, ensure_ascii=False):
|
||||
usages.append(str(data.get("name") or item.name))
|
||||
return usages
|
||||
|
||||
|
||||
def _remap_import_asset_ids(document: dict, asset_map: dict[str, str]) -> None:
|
||||
elements = document.get("elements")
|
||||
if not isinstance(elements, list):
|
||||
@@ -1796,6 +1816,10 @@ def delete_asset(asset_id: str) -> None:
|
||||
meta = _read_asset_meta(d)
|
||||
if not meta:
|
||||
raise HTTPException(status_code=404, detail="asset not found")
|
||||
usages = _asset_reference_names(asset_id)
|
||||
if usages:
|
||||
names = "、".join(dict.fromkeys(usages))
|
||||
raise HTTPException(status_code=409, detail=f"素材仍被以下设计引用,无法删除:{names}")
|
||||
shutil.rmtree(d, ignore_errors=True)
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user