Add guarded asset import execution
这个提交存在于:
父节点
6d2c413b4c
当前提交
fb20d820fc
共有 9 个文件被更改,包括 86 次插入 和 36 次删除
|
|
@ -1,4 +1,4 @@
|
|||
"""Dry-run planning for ImageStudio asset pack imports."""
|
||||
"""Planning and guarded execution skeleton for ImageStudio asset pack imports."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
|
|
@ -24,13 +24,6 @@ class AssetImportService:
|
|||
self.storage = VfsStorage(config_service)
|
||||
|
||||
def plan_import_pack(self, package_uri: str, *, dry_run: bool = True) -> dict[str, Any]:
|
||||
if not dry_run:
|
||||
raise ApiError(
|
||||
ErrorCode.VALIDATION_FAILED,
|
||||
"asset pack import execution is not enabled; run with dry_run=true",
|
||||
{"field": "dry_run"},
|
||||
)
|
||||
|
||||
root, relative = self.storage.split_uri(package_uri)
|
||||
package_path = self.storage.resolve_uri(package_uri)
|
||||
if not package_path.exists():
|
||||
|
|
@ -39,16 +32,26 @@ class AssetImportService:
|
|||
files, total_bytes = _path_file_stats(package_path)
|
||||
manifest_candidates = _manifest_candidates(root, relative, package_path)
|
||||
manifest_summary = _manifest_summary(package_path, manifest_candidates)
|
||||
if not dry_run and not manifest_summary["valid"]:
|
||||
raise ApiError(
|
||||
ErrorCode.VALIDATION_FAILED,
|
||||
"asset pack manifest is invalid",
|
||||
{"field": "manifest", "errors": manifest_summary["errors"]},
|
||||
)
|
||||
return {
|
||||
"dry_run": True,
|
||||
"status": "planned",
|
||||
"dry_run": dry_run,
|
||||
"status": "planned" if dry_run else "applied",
|
||||
"package_uri": package_uri,
|
||||
"files": files,
|
||||
"bytes": total_bytes,
|
||||
"manifest_candidates": [item["uri"] for item in manifest_candidates],
|
||||
"manifest": manifest_summary,
|
||||
"import_enabled": False,
|
||||
"execution_enabled": False,
|
||||
"import_enabled": manifest_summary["valid"],
|
||||
"execution_enabled": True,
|
||||
"applied": {
|
||||
"materials": 0 if dry_run else manifest_summary["materials"],
|
||||
"styles": 0 if dry_run else manifest_summary["styles"],
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -23,4 +23,8 @@ export interface AssetImportPlan {
|
|||
manifest: AssetManifestSummary
|
||||
import_enabled: boolean
|
||||
execution_enabled: boolean
|
||||
applied: {
|
||||
materials: number
|
||||
styles: number
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -239,6 +239,7 @@ const text = {
|
|||
storageMigrate: "\u8fc1\u79fb\u9884\u68c0",
|
||||
overwriteRisk: "\u8986\u76d6\u98ce\u9669",
|
||||
assetImportPack: "\u8d44\u4ea7\u5305\u9884\u68c0",
|
||||
assetImportApply: "\u5bfc\u5165\u8d44\u4ea7\u5305",
|
||||
manifest: "\u6e05\u5355",
|
||||
manifestErrors: "\u6e05\u5355\u9519\u8bef",
|
||||
valid: "\u6709\u6548",
|
||||
|
|
@ -249,6 +250,7 @@ const text = {
|
|||
fileCount: "\u6587\u4ef6\u6570",
|
||||
dryRun: "DryRun",
|
||||
running: "\u8fd0\u884c",
|
||||
applied: "\u5df2\u5e94\u7528",
|
||||
} as const
|
||||
|
||||
const navItems: Array<{ key: ViewKey; label: string }> = [
|
||||
|
|
@ -1733,6 +1735,20 @@ function planAssetImportPack() {
|
|||
false,
|
||||
)
|
||||
}
|
||||
|
||||
function applyAssetImportPack() {
|
||||
return runOperation(
|
||||
text.assetImportApply,
|
||||
async () => {
|
||||
assetImportPlan.value = await postJson<AssetImportPlan>(endpoints.assetImportPack, {
|
||||
package_uri: assetImportDraft.value.package_uri,
|
||||
dry_run: false,
|
||||
})
|
||||
return assetImportPlan.value
|
||||
},
|
||||
false,
|
||||
)
|
||||
}
|
||||
</script>
|
||||
|
||||
<template>
|
||||
|
|
@ -3019,6 +3035,9 @@ function planAssetImportPack() {
|
|||
<button type="button" :disabled="Boolean(busy) || !assetImportDraft.package_uri" @click="planAssetImportPack">
|
||||
{{ text.assetImportPack }}
|
||||
</button>
|
||||
<button type="button" :disabled="Boolean(busy) || !assetImportDraft.package_uri" @click="applyAssetImportPack">
|
||||
{{ text.assetImportApply }}
|
||||
</button>
|
||||
</div>
|
||||
<dl v-if="assetImportPlan">
|
||||
<div>
|
||||
|
|
@ -3049,6 +3068,14 @@ function planAssetImportPack() {
|
|||
<dt>{{ text.manifestErrors }}</dt>
|
||||
<dd>{{ assetImportPlan.manifest.errors[0]?.code ?? "0" }}</dd>
|
||||
</div>
|
||||
<div>
|
||||
<dt>{{ text.dryRun }}</dt>
|
||||
<dd>{{ assetImportPlan.dry_run }}</dd>
|
||||
</div>
|
||||
<div>
|
||||
<dt>{{ text.applied }}</dt>
|
||||
<dd>{{ assetImportPlan.applied.materials }} / {{ assetImportPlan.applied.styles }}</dd>
|
||||
</div>
|
||||
</dl>
|
||||
</aside>
|
||||
</main>
|
||||
|
|
|
|||
|
|
@ -696,7 +696,10 @@ database:
|
|||
def test_asset_import_pack_dry_run_is_safe(self):
|
||||
pack = self.root / "materials" / "pack"
|
||||
pack.mkdir(parents=True, exist_ok=True)
|
||||
(pack / "manifest.json").write_text('{"name":"demo"}', encoding="utf-8")
|
||||
(pack / "manifest.json").write_text(
|
||||
'{"name":"demo","materials":[{"name":"hat"}],"styles":[{"name":"daily"}]}',
|
||||
encoding="utf-8",
|
||||
)
|
||||
(pack / "image.bin").write_bytes(b"123")
|
||||
nested = pack / "nested"
|
||||
nested.mkdir(parents=True, exist_ok=True)
|
||||
|
|
@ -715,7 +718,9 @@ database:
|
|||
self.assertIn("storage://materials/pack/manifest.json", planned["data"]["manifest_candidates"])
|
||||
self.assertTrue(planned["data"]["manifest"]["valid"])
|
||||
self.assertEqual(planned["data"]["manifest"]["name"], "demo")
|
||||
self.assertFalse(planned["data"]["import_enabled"])
|
||||
self.assertTrue(planned["data"]["import_enabled"])
|
||||
self.assertTrue(planned["data"]["execution_enabled"])
|
||||
self.assertEqual(planned["data"]["applied"], {"materials": 0, "styles": 0})
|
||||
self.assertNotIn(str(self.root), str(planned["data"]))
|
||||
|
||||
execution = import_asset_pack(
|
||||
|
|
@ -723,8 +728,11 @@ database:
|
|||
{"package_uri": "storage://materials/pack", "dry_run": False},
|
||||
self.asset_import_service,
|
||||
)
|
||||
self.assertFalse(execution["ok"])
|
||||
self.assertEqual(execution["error_code"], "VALIDATION_FAILED")
|
||||
self.assertTrue(execution["ok"])
|
||||
self.assertFalse(execution["data"]["dry_run"])
|
||||
self.assertEqual(execution["data"]["status"], "applied")
|
||||
self.assertEqual(execution["data"]["applied"], {"materials": 1, "styles": 1})
|
||||
self.assertNotIn(str(self.root), str(execution["data"]))
|
||||
|
||||
unsafe = import_asset_pack(
|
||||
self.ctx,
|
||||
|
|
@ -747,6 +755,14 @@ database:
|
|||
self.assertEqual(invalid_manifest["data"]["manifest"]["errors"][0]["code"], "MANIFEST_INVALID_JSON")
|
||||
self.assertNotIn(str(self.root), str(invalid_manifest["data"]))
|
||||
|
||||
invalid_execution = import_asset_pack(
|
||||
self.ctx,
|
||||
{"package_uri": "storage://materials/bad-pack", "dry_run": False},
|
||||
self.asset_import_service,
|
||||
)
|
||||
self.assertFalse(invalid_execution["ok"])
|
||||
self.assertEqual(invalid_execution["error_code"], "VALIDATION_FAILED")
|
||||
|
||||
def test_media_signing_rejects_unsafe_or_missing_uri(self):
|
||||
unsafe = sign_media(
|
||||
self.ctx,
|
||||
|
|
|
|||
文件差异因一行或多行过长而隐藏
文件差异因一行或多行过长而隐藏
17
web/assets/index-Zmatr0EW.js
普通文件
17
web/assets/index-Zmatr0EW.js
普通文件
文件差异因一行或多行过长而隐藏
文件差异因一行或多行过长而隐藏
|
|
@ -1,3 +1,3 @@
|
|||
<script type="module" crossorigin src="/api/v1/plugins/extensions/astrbot_plugin_imagestudio/web/assets/index-zKJSSm0e.js"></script>
|
||||
<link rel="stylesheet" crossorigin href="/api/v1/plugins/extensions/astrbot_plugin_imagestudio/web/assets/index-BJt-KVrZ.css">
|
||||
<script type="module" crossorigin src="/api/v1/plugins/extensions/astrbot_plugin_imagestudio/web/assets/index-Zmatr0EW.js"></script>
|
||||
<link rel="stylesheet" crossorigin href="/api/v1/plugins/extensions/astrbot_plugin_imagestudio/web/assets/index-BIepwMFF.css">
|
||||
<div id="app"></div>
|
||||
|
|
|
|||
正在加载…
添加表格
在新工单中引用