Merge pull request #762 from tcely/patch-15

Optimize `rename_files`
This commit is contained in:
meeb 2025-02-24 17:40:45 +11:00 committed by GitHub
commit c7a04a2a90
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -1525,6 +1525,8 @@ class Media(models.Model):
old_file_str = other_path.name
new_file_str = new_stem + old_file_str[len(old_stem):]
new_file_path = Path(new_prefix_path / new_file_str)
if new_file_path == other_path:
continue
log.debug(f'Considering replace for: {self!s}\n\t{other_path!s}\n\t{new_file_path!s}')
# it should exist, but check anyway
if other_path.exists():
@ -1536,6 +1538,8 @@ class Media(models.Model):
old_file_str = fuzzy_path.name
new_file_str = new_stem + old_file_str[len(fuzzy_stem):]
new_file_path = Path(new_prefix_path / new_file_str)
if new_file_path == fuzzy_path:
continue
log.debug(f'Considering rename for: {self!s}\n\t{fuzzy_path!s}\n\t{new_file_path!s}')
# it quite possibly was renamed already
if fuzzy_path.exists() and not new_file_path.exists():
@ -1549,8 +1553,9 @@ class Media(models.Model):
# try to remove empty dirs
parent_dir = old_video_path.parent
stop_dir = self.source.directory_path
try:
while parent_dir.is_dir():
while parent_dir.is_relative_to(stop_dir):
parent_dir.rmdir()
log.info(f'Removed empty directory: {parent_dir!s}')
parent_dir = parent_dir.parent