Peach: Local-first video metadata scraper and media manager.
test(storage): 补齐删除入口的来源声明与路径校验
longmeidao8b48410b050b
diff --git a/src/peach/web_batch.py b/src/peach/web_batch.pyindex e61f0bd25..815e475ed 100644--- a/src/peach/web_batch.py+++ b/src/peach/web_batch.py@@ -331,6 +331,20 @@ def _finish_purge(outcome):return outcome+def _purge_media_path(row, online_roots: dict[str, tuple[Path, ...]]) -> Path:+ """核对永久删除目标的路径映射和来源可读性,失败时拒绝删除。"""+ media = row["path"]+ translated = translate_ledger_path(media)+ if is_unmapped(translated):+ raise OSError("媒体路径未映射到可访问的挂载目录")+ if PureWindowsPath(str(media)).drive:+ location = row["location"] if "location" in row.keys() else None+ if not any(within_root(translated, root) and root_online(root)+ for root in online_roots.get(location, ())):+ raise OSError("媒体来源未挂载或不可读取")+ return translated++def purge_assets(connection, rows, *, missing_only: bool = False):"""删除媒体和账本行,调用方提交后清理隔离文件。@@ -346,24 +360,13 @@ def purge_assets(connection, rows, *, missing_only: bool = False):translated = Noneif media:try:- translated = translate_ledger_path(media)+ if online_roots is None and PureWindowsPath(str(media)).drive:+ online_roots = _online_source_roots()+ translated = _purge_media_path(row, online_roots or {})except OSError as error:blocked.append({"id": row["id"], "path": media,"reason": error.strerror or str(error)})continue- if is_unmapped(translated):- blocked.append({"id": row["id"], "path": media,- "reason": "媒体路径未映射到可访问的挂载目录"})- continue- if PureWindowsPath(str(media)).drive:- if online_roots is None:- online_roots = _online_source_roots()- location = row["location"] if "location" in row.keys() else None- if not any(within_root(translated, root) and root_online(root)- for root in online_roots.get(location, ())):- blocked.append({"id": row["id"], "path": media,- "reason": "媒体来源未挂载或不可读取"})- continuemedia_stat = Noneif media:try:diff --git a/tests/test_fastapi_api.py b/tests/test_fastapi_api.pyindex c42e658c7..2aeea514c 100644--- a/tests/test_fastapi_api.py+++ b/tests/test_fastapi_api.py@@ -205,9 +205,11 @@ class FastApiContractTests(unittest.IsolatedAsyncioTestCase):async def asyncSetUp(self):self.enterContext(patch.object(routes_auth, "_ATTEMPTS", OrderedDict()))self.tmp = tempfile.TemporaryDirectory()- self.root = Path(self.tmp.name)+ self.root = Path(self.tmp.name).resolve()self.db = self.root / "ledger.db"self.media_root = self.root / "media"+ self.enterContext(patch("peach.web_batch.LOCATION_ROOT_DECLARATIONS",+ {"local": (str(self.media_root),)}))self.snapshot_root = self.root / "snapshots"self.poster_root = self.root / "posters"self.avatar_root = self.root / "avatars"diff --git a/tests/test_web_orphan_records.py b/tests/test_web_orphan_records.pyindex 7d4f39d54..3f08c732d 100644--- a/tests/test_web_orphan_records.py+++ b/tests/test_web_orphan_records.py@@ -8,6 +8,7 @@ import tempfileimport unittestfrom contextlib import closingfrom pathlib import Path+from unittest.mock import patchfrom peach import record_rehome, web_batchfrom peach import web_contract as rm_web@@ -21,6 +22,8 @@ class OrphanRecordsTests(unittest.TestCase):self.tmp = tempfile.TemporaryDirectory()self.addCleanup(self.tmp.cleanup)self.root = Path(self.tmp.name).resolve()+ self.enterContext(patch.object(web_batch, "LOCATION_ROOT_DECLARATIONS",+ {"local": (str(self.root),)}))self.db = fresh_ledger(self.root)gone, here = self.root / "gone", self.root / "here"rows = [