Skip to content

armgit / peach

Public Imported from GitHub
0

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

fix(organize): 允许同一文件仅改大小写并保留回滚

longmeidao892738be3234
diff --git a/src/peach/organize.py b/src/peach/organize.py
index f355f10bd..83a57592c 100644
--- a/src/peach/organize.py
+++ b/src/peach/organize.py
@@ -393,7 +393,9 @@ def _move_one(connection: sqlite3.Connection, asset_id: int,
local_target = _media_path(new_path)
if not local_source.exists():
return "源文件不存在"
- if local_target.exists():
+ case_only = old_path.casefold() == new_path.casefold()
+ if local_target.exists() and not (
+ case_only and isinstance(local_source, Path) and local_source.samefile(local_target)):
return SKIP_TARGET_EXISTS
try:
_rename(old_path, new_path)
@@ -441,7 +443,8 @@ def apply_plan(connection: sqlite3.Connection, rows: Sequence[dict], *,
old_path, new_path = str(row["current_path"]), str(row["target_path"])
current = connection.execute('SELECT path,disposal FROM asset WHERE id=?', (int(row['asset_id']),)).fetchone()
landed = (current is not None and current[0] == new_path
- and _media_path(new_path).is_file() and not _media_path(old_path).exists())
+ and _media_path(new_path).is_file()
+ and (old_path.casefold() == new_path.casefold() or not _media_path(old_path).exists()))
if current is None or current[1] is not None:
outcome = '资产已不在馆藏,未执行整理'
else:
@@ -475,7 +478,8 @@ def rollback_batch(connection: sqlite3.Connection, log_path: Path | str, *,
current = connection.execute('SELECT path FROM asset WHERE id=?', (int(entry['asset_id']),)).fetchone()
landed = (current is not None and current[0] == entry['old_path']
and _media_path(entry['old_path']).is_file()
- and not _media_path(entry['new_path']).exists())
+ and (entry['old_path'].casefold() == entry['new_path'].casefold()
+ or not _media_path(entry['new_path']).exists()))
outcome = 'ok' if landed else _move_one(connection, int(entry["asset_id"]),
str(entry["new_path"]), str(entry["old_path"]))
if outcome == "ok":
diff --git a/tests/test_organize.py b/tests/test_organize.py
index 1dfe1f08f..98ee930c4 100644
--- a/tests/test_organize.py
+++ b/tests/test_organize.py
@@ -375,6 +375,34 @@ class ApplyTests(unittest.TestCase):
self.assertEqual((result['moved'], result['failed']), (0, 1))
self.assertTrue(original.is_file())
+ def test_case_only_rename_updates_spelling_and_can_be_rolled_back(self):
+ self._file('old/abc-123.mp4')
+ original = r'R:\media\old\abc-123.mp4'
+ target = r'R:\media\old\ABC-123.mp4'
+ _seed(self.db_path, [dict(id=1, path=original, name='abc-123.mp4', code='ABC-123')])
+ plan = _plan(self.db_path, file_template='{number}')
+ result = self._apply(plan)
+ self.assertEqual((result['moved'], result['failed']), (1, 0))
+ self.assertEqual([path.name for path in (self.media / 'old').iterdir()], ['ABC-123.mp4'])
+ with closing(sqlite3.connect(self.db_path)) as connection:
+ self.assertEqual(connection.execute('SELECT path,name FROM asset').fetchone(),
+ (target, 'ABC-123.mp4'))
+ result = organize.rollback_batch(connection, organize.latest_batch(self.holder))
+ self.assertEqual((result['restored'], result['failed']), (1, 0))
+ self.assertEqual(connection.execute('SELECT path,name FROM asset').fetchone(),
+ (original, 'abc-123.mp4'))
+ self.assertEqual([path.name for path in (self.media / 'old').iterdir()], ['abc-123.mp4'])
+
+ def test_case_only_move_committed_before_journal_is_not_renamed_again(self):
+ self._file('old/ABC-123.mp4')
+ original = r'R:\media\old\abc-123.mp4'
+ target = r'R:\media\old\ABC-123.mp4'
+ _seed(self.db_path, [dict(id=1, path=target, name='ABC-123.mp4', code='ABC-123')])
+ plan = {'rows': [dict(asset_id=1, action='rename', current_path=original, target_path=target)]}
+ with mock.patch.object(organize, '_rename', side_effect=AssertionError('已提交的行不应再次改名')):
+ result = self._apply(plan)
+ self.assertEqual((result['moved'], result['failed']), (1, 0))
+
def test_scoped_symbol_cleanup_and_rollback_preserve_unselected_files(self):
self._file('合集/★标题⧹⧹片段.mp4')
self._file('合集2/★标题.mp4')