Skip to content

armgit / peach

Public Imported from GitHub
0

Peach: Local-first video metadata scraper and media manager.

fix(scan): 移除媒体目录后,资源同步能清掉其下的失效记录

longmeidao9b35e09eae69
diff --git a/src/peach/web_batch.py b/src/peach/web_batch.py
index 47011626c..a80e7fb74 100644
--- a/src/peach/web_batch.py
+++ b/src/peach/web_batch.py
@@ -340,8 +340,13 @@ def _finish_purge(outcome):
return outcome
-def _purge_media_path(row, online_roots: dict[str, tuple[Path, ...]]) -> Path:
- """核对永久删除目标的路径映射和来源可读性,失败时拒绝删除。"""
+def _purge_media_path(row, online_roots: dict[str, tuple[Path, ...]], *,
+ missing_only: bool = False) -> Path:
+ """核对永久删除目标的路径映射和来源可读性,失败时拒绝删除。
+
+ `missing_only` 只删账本行:媒体目录已从设置里移除时,这一行不在任何来源根下,
+ 所在的盘可读就交给调用方 `stat`,文件确实不在才删。要删媒体文件的仍只认在线来源根。
+ """
media = row["path"]
from .smb_media import resolve
if remote := resolve(media):
@@ -351,12 +356,23 @@ def _purge_media_path(row, online_roots: dict[str, tuple[Path, ...]]) -> Path:
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("媒体来源未挂载或不可读取")
+ if any(within_root(translated, root) and root_online(root)
+ for root in online_roots.get(location, ())):
+ return translated
+ if missing_only and not _inside_declared_root(translated) and root_online(Path(translated.anchor)):
+ return translated
+ raise OSError("媒体来源未挂载或不可读取")
return translated
+def _inside_declared_root(path: Path) -> bool:
+ """路径是否落在设置里任一来源根下,包括此刻离线的根。SMB 根下的行由 `resolve` 先接走。"""
+ from .smb_media import resolve
+ return any(within_root(path, translate_ledger_path(declaration))
+ for declarations in LOCATION_ROOT_DECLARATIONS.values()
+ for declaration in declarations if resolve(declaration) is None)
+
+
def purge_assets(connection, rows, *, missing_only: bool = False):
"""删除媒体和账本行,调用方提交后清理隔离文件。
@@ -374,7 +390,7 @@ def purge_assets(connection, rows, *, missing_only: bool = False):
try:
if online_roots is None and PureWindowsPath(str(media)).drive:
online_roots = _online_source_roots()
- translated = _purge_media_path(row, online_roots or {})
+ translated = _purge_media_path(row, online_roots or {}, missing_only=missing_only)
except OSError as error:
blocked.append({"id": row["id"], "path": media,
"reason": error.strerror or str(error)})
diff --git a/tests/test_web_resource_sync.py b/tests/test_web_resource_sync.py
index 87a17bb46..d6b4c8d9a 100644
--- a/tests/test_web_resource_sync.py
+++ b/tests/test_web_resource_sync.py
@@ -326,6 +326,45 @@ class PurgeMissingTests(unittest.TestCase):
self.assertEqual((result['purged'], result['vanished']), (2, 1))
self.assertEqual(self.ids(), [1, 2, 4])
+ def _add_removed_folder_row(self):
+ """这一行原先在一个媒体目录下,那个目录已从设置里移除、盘上也删掉了;所在的盘还在。"""
+ (self.root / "local").mkdir()
+ with self.contract.write_transaction() as connection:
+ connection.execute("INSERT INTO asset(id,location,path,name,medium,size) "
+ "VALUES(5,'115','R:\\strm\\CESD-499.strm','CESD-499.strm','video',10)")
+
+ def test_rows_under_a_removed_media_folder_are_cleaned_with_the_rest(self):
+ self._add_removed_folder_row()
+ with self._synced():
+ scan = finish_scan(self.contract)
+ self.assertEqual(scan["missing"], 3)
+ result = rm_sync.w_resource_sync_apply(
+ self.contract, {"confirm": True, "clean_cache": False, "scan_id": scan["scan_id"]})
+ self.assertEqual((result["purged"], result["vanished"], result["blocked_count"]), (2, 1, 0))
+ self.assertEqual(self.ids(), [1, 2, 4])
+
+ def test_rows_under_a_removed_media_folder_stay_when_their_disk_cannot_be_read(self):
+ self._add_removed_folder_row()
+ readable = web_batch.root_online
+ with self._synced(), mock.patch.object(
+ web_batch, "root_online", lambda path: path != Path(path.anchor) and readable(path)):
+ scan = finish_scan(self.contract)
+ result = rm_sync.w_resource_sync_apply(
+ self.contract, {"confirm": True, "clean_cache": False, "scan_id": scan["scan_id"]})
+ self.assertEqual(result["blocked"], [{"id": 5, "name": "CESD-499.strm", "reason": "媒体来源未挂载或不可读取"}])
+ self.assertEqual(self.ids(), [1, 2, 4, 5])
+
+ def test_deleting_media_outside_every_media_folder_is_still_refused(self):
+ self._add_removed_folder_row()
+ kept = self.root / "local" / "strm" / "CESD-499.strm"
+ kept.parent.mkdir()
+ kept.write_bytes(b"x")
+ with self._synced(), self.contract.write_transaction() as connection:
+ row = connection.execute("SELECT id,location,path,snapshot_path FROM asset WHERE id=5").fetchone()
+ outcome = web_batch.purge_assets(connection, [row])
+ self.assertEqual((outcome["purged"], len(outcome["blocked"])), (0, 1))
+ self.assertTrue(kept.is_file())
+
def test_apply_resumes_after_a_committed_chunk_with_cumulative_receipt(self):
saved = {}
def remember(**fields):