Peach: Local-first video metadata scraper and media manager.
fix(media): 保护只读挂载的改名与索引恢复入口
longmeidao031c64a7926a
diff --git a/scripts/clean_names.py b/scripts/clean_names.pyindex f4fd6932a..aaf148f54 100644--- a/scripts/clean_names.py+++ b/scripts/clean_names.py@@ -36,6 +36,7 @@ if str(SRC_DIR) not in sys.path:from peach.review_csv import write_rowsfrom peach.catalog_rules import is_jav_code, normalise_code_keyfrom peach.config import DATABASE_PATH, GENERATED_DIR, LOG_DIR+from peach.media_sources import READ_ONLY_LOCATIONS, SCANNABLE_LOCATIONS, require_writable_locationfrom peach.scripting import (add_ledger_write_args, open_for_write, open_readonly, verify_after_write,)@@ -176,7 +177,7 @@ def build_parser() -> argparse.ArgumentParser:add_ledger_write_args(parser, db_default=DEFAULT_DB)parser.add_argument("--out", type=Path, default=DEFAULT_OUT)parser.add_argument("--log-dir", type=Path, default=DEFAULT_LOG_DIR)- parser.add_argument("--location", choices=("local", "115", "pikpak"))+ parser.add_argument("--location", choices=SCANNABLE_LOCATIONS)return parser@@ -235,6 +236,8 @@ def build_plan(rows: list[tuple]) -> list[dict]:plan: list[dict] = []for aid, location, path, name, code, *evidence in rows:+ if location in READ_ONLY_LOCATIONS:+ continueallow_compact = bool(evidence and evidence[0])normalized = normalise_code_key(code)canonical = (@@ -333,6 +336,7 @@ def run(args_ns: argparse.Namespace) -> int:fail += 1continuetry:+ require_writable_location(item["location"])if rename_needed:_rename_path(src, dst)except Exception as exc:diff --git a/src/peach/web_media_repair.py b/src/peach/web_media_repair.pyindex dd2aa7dfc..c92154466 100644--- a/src/peach/web_media_repair.py+++ b/src/peach/web_media_repair.py@@ -26,6 +26,7 @@ from pathlib import Path, PureWindowsPathfrom . import media_libraries, media_probe, mp4recover, settings_filefrom .mp4repair import RepairUnavailable+from .media_sources import READ_ONLY_LOCATIONSfrom .platform import translate_ledger_path#: 判定结论的落盘位置,放在边车自己的目录里:它们同生同灭,清缓存时一起走。@@ -167,6 +168,10 @@ def _record_replacement(contract, asset_id: int, source: Path, ffprobe: Path) ->def _recover(contract, asset_id: int, raw: str, tools: tuple[Path, Path, Path]) -> bool:+ with contract.read_connection() as connection:+ row = connection.execute("SELECT location FROM asset WHERE id=?", (asset_id,)).fetchone()+ if row is None or row['location'] in READ_ONLY_LOCATIONS:+ return Falseuntrunc, ffmpeg, ffprobe = toolssource = translate_ledger_path(raw)references = [translate_ledger_path(path) for path indiff --git a/tests/test_scripts.py b/tests/test_scripts.pyindex 6d518770b..36dc1ad31 100644--- a/tests/test_scripts.py+++ b/tests/test_scripts.py@@ -1887,6 +1887,11 @@ class OperationalScriptTests(unittest.TestCase):"/tmp/peach/ABW-234.mp4",)+ def test_filename_cleanup_ignores_read_only_mounts(self):+ self.assertEqual(self.clean_names.build_plan([+ (1, 'guangya', r'G:\media\abw-0234.mp4', 'abw-0234.mp4', 'ABW-234'),+ ]), [])+def test_filename_cleanup_apply_renames_files_updates_ledger_and_validates_backup(self):with tempfile.TemporaryDirectory() as tmp:root = Path(tmp)diff --git a/tests/test_web_media_repair.py b/tests/test_web_media_repair.pyindex c355477a2..636e2b471 100644--- a/tests/test_web_media_repair.py+++ b/tests/test_web_media_repair.py@@ -141,6 +141,16 @@ class MediaRepairRunTests(unittest.TestCase):self.assertEqual(self.store.repaired[0], 2, "放得最多的那部没排在最前面")+ def test_read_only_mount_never_replaces_the_source_file(self):+ with self.contract.write_transaction() as connection:+ connection.execute("UPDATE asset SET location='guangya' WHERE id=6")+ with mock.patch.object(rm_repair.mp4recover, 'recover') as recover, \+ mock.patch.object(rm_repair.mp4recover, 'replace_original') as replace:+ self.assertFalse(rm_repair._recover(self.contract, 6, 'G:\\media\\cut.mp4',+ (Path('untrunc'), Path('ffmpeg'), Path('ffprobe'))))+ recover.assert_not_called()+ replace.assert_not_called()+def test_a_library_has_to_be_named(self):with self.assertRaisesRegex(ValueError, "先选一个媒体库"):rm_repair.w_media_repair(self.contract, {})