@@ -180,20 +180,42 @@ async def scan(self, library_path: Path, full_scan: bool = False) -> dict:
180180 if processed % 50 == 0 :
181181 await asyncio .sleep (0 )
182182
183- # Handle deleted files - only delete files that were under this library_path
183+ # Handle missing files - only check files that were under this library_path
184184 library_prefix = str (library_path )
185- deleted_paths = set (
185+ missing_paths = set (
186186 p for p in existing_paths .keys ()
187187 if p .startswith (library_prefix )
188188 ) - found_paths
189189
190- if deleted_paths :
191- logger .info (f"Removing { len (deleted_paths )} deleted files from database ..." )
190+ if missing_paths :
191+ logger .info (f"Found { len (missing_paths )} missing files, searching for relocated files ..." )
192192 if self .scan_state :
193- self .scan_state .set_cleanup (len (deleted_paths ))
194-
195- for path_str in deleted_paths :
193+ self .scan_state .set_cleanup (len (missing_paths ))
194+
195+ # Build filename -> path map for relocated file search
196+ filename_to_path : dict [str , str ] = {}
197+ for path_str in found_paths :
198+ filename = Path (path_str ).name .lower ()
199+ # Only use first occurrence to avoid ambiguity
200+ if filename not in filename_to_path :
201+ filename_to_path [filename ] = path_str
202+
203+ results ["relocated" ] = 0
204+ for path_str in missing_paths :
196205 track = existing_paths [path_str ]
206+ filename = Path (path_str ).name .lower ()
207+
208+ # Check if file exists at a new location
209+ if filename in filename_to_path :
210+ new_path = filename_to_path [filename ]
211+ # Verify it's not already tracked by another record
212+ if new_path not in existing_paths :
213+ logger .info (f"RELOCATED: { Path (path_str ).name } -> { new_path } " )
214+ track .file_path = new_path
215+ results ["relocated" ] += 1
216+ continue
217+
218+ # File truly deleted
197219 logger .info (f"DELETED: { Path (path_str ).name } " )
198220 await self .db .delete (track )
199221 results ["deleted" ] += 1
@@ -204,7 +226,7 @@ async def scan(self, library_path: Path, full_scan: bool = False) -> dict:
204226
205227 await self .db .commit ()
206228
207- logger .info (f"Scan complete: { results ['new' ]} new, { results ['updated' ]} updated, { results ['deleted' ]} deleted, { results ['unchanged' ]} unchanged" )
229+ logger .info (f"Scan complete: { results ['new' ]} new, { results ['updated' ]} updated, { results ['relocated' ] } relocated, { results [ ' deleted' ]} deleted, { results ['unchanged' ]} unchanged" )
208230
209231 return results
210232
0 commit comments