@@ -449,6 +449,22 @@ def keep_original_images(self):
449449 def has_error (self ):
450450 return self .state == File .State .ERROR
451451
452+ def _handle_conflict (self , new_path , old_path ):
453+ config = get_config ()
454+ strategy = config .setting ['move_conflict_strategy' ]
455+ if strategy not in ("skip" , "rename" , "overwrite" ):
456+ strategy = "rename"
457+
458+ if not os .path .exists (new_path ):
459+ return new_path
460+
461+ if strategy == "skip" :
462+ log .warning ("Destination exists, skipping %r" , old_path )
463+ return None
464+ elif strategy == "rename" :
465+ return get_available_filename (new_path , old_path )
466+ return new_path
467+
452468 def save (self ):
453469 self .set_pending ()
454470 run_file_pre_save_processors (self )
@@ -676,16 +692,9 @@ def _rename(self, old_filename, metadata, settings):
676692 new_dirname = os .path .dirname (new_filename )
677693 if not os .path .isdir (new_dirname ):
678694 os .makedirs (new_dirname )
679- config = get_config ()
680- strategy = config .setting .get ('move_conflict_strategy' , 'rename' )
681- if os .path .exists (new_filename ):
682- if strategy == "skip" :
683- log .warning ("Destination exists, skipping move of %r" , old_filename )
684- return old_filename
685- elif strategy == "rename" :
686- new_filename = get_available_filename (new_filename , old_filename )
687- elif strategy == "overwrite" :
688- pass
695+ new_filename = self ._handle_conflict (new_filename , old_filename )
696+ if new_filename is None :
697+ return old_filename
689698 log .debug ("Moving file %r => %r" , old_filename , new_filename )
690699 move_ensure_casing (old_filename , new_filename )
691700 return new_filename
@@ -705,6 +714,7 @@ def _save_images(self, dirname, metadata):
705714 images = metadata .images
706715 for image in images :
707716 image .save (dirname , metadata , counters )
717+
708718 def _move_additional_files (self , old_filename , new_filename , config ):
709719 """Move extra files, like images, playlists…"""
710720 if config .setting ['move_files' ] and config .setting ['move_additional_files' ]:
@@ -740,21 +750,14 @@ def _get_additional_files_moves(self, old_path, new_path, patterns):
740750 break # we are done with this file
741751
742752 def _apply_additional_files_moves (self , moves ):
743- config = get_config ()
744- strategy = config .setting .get ('move_conflict_strategy' , 'rename' )
745753 for old_file_path , new_file_path in moves :
746754 # FIXME we shouldn't do this from a thread!
747755 if self .tagger .files .get (decode_filename (old_file_path )):
748756 log .debug ("File loaded in the tagger, not moving %r" , old_file_path )
749757 continue
750- if os .path .exists (new_file_path ):
751- if strategy == "skip" :
752- log .warning ("File %r exists, skipping %r" , new_file_path , old_file_path )
753- continue
754- elif strategy == "rename" :
755- new_file_path = get_available_filename (new_file_path )
756- elif strategy == "overwrite" :
757- pass
758+ new_file_path = self ._handle_conflict (new_file_path , old_file_path )
759+ if new_file_path is None :
760+ continue
758761 log .debug ("Moving %r to %r" , old_file_path , new_file_path )
759762 try :
760763 shutil .move (old_file_path , new_file_path )
0 commit comments