Explorar el Código

fix bug where files with same hash were both indexed in the same op

Olivier Massot hace 4 años
padre
commit
a3dca68480
Se han modificado 1 ficheros con 9 adiciones y 4 borrados
  1. 9 4
      core/indexer.py

+ 9 - 4
core/indexer.py

@@ -55,7 +55,7 @@ class Indexer(Thread):
         buffer = deque()
 
         # -- Walk through music folders
-        # Index new files
+        # Put new files in buffer
         for music_folder in music_folders:
             music_folder_path = Path(music_folder.path)
 
@@ -88,7 +88,7 @@ class Indexer(Thread):
                         buffer.append(track.id)
                     del index[filename]
 
-        # Index missing files
+        # Put missing files in buffer
         for filename, track in index.items():
             if track.id in buffer:
                 continue
@@ -103,7 +103,7 @@ class Indexer(Thread):
         while buffer:
             filename_or_id = buffer.pop()
             try:
-                track = self.index(track_repo, filename_or_id)
+                track = self.index(track_repo, filename_or_id, tracks)
                 tracks.append(track)
             except AlreadyIndexed:
                 pass
@@ -123,9 +123,11 @@ class Indexer(Thread):
             logger.info(f"{len(tracks)} tracks indexed")
 
     @staticmethod
-    def index(track_repo, filename_or_track_id):
+    def index(track_repo, filename_or_track_id, previously_indexed=None):
         """ index a media file from the filesystem or a track id """
 
+        previously_indexed = previously_indexed or []
+
         if type(filename_or_track_id) is int:
             track = track_repo.get_by_id(filename_or_track_id)
             filename = Path(track.path)
@@ -144,6 +146,9 @@ class Indexer(Thread):
 
             track_hash = hash_file(filename)
 
+            if any(t.hash == track_hash for t in previously_indexed):
+                raise AlreadyIndexed(f"File already indexed")
+
             track = track_repo.get_by_hash(track_hash)
             if not track:
                 track = Track()