From fafec56f0903827ab992df2f9067841412cbdd60 Mon Sep 17 00:00:00 2001 From: Will Miao <13051207myq@gmail.com> Date: Fri, 11 Apr 2025 05:52:56 +0800 Subject: [PATCH] refactor: Rename update_single_lora_cache to update_single_model_cache for consistency --- py/routes/api_routes.py | 6 ++-- py/services/lora_scanner.py | 58 +------------------------------------ 2 files changed, 4 insertions(+), 60 deletions(-) diff --git a/py/routes/api_routes.py b/py/routes/api_routes.py index 0dff5524..af5aef44 100644 --- a/py/routes/api_routes.py +++ b/py/routes/api_routes.py @@ -396,7 +396,7 @@ class ApiRoutes: with open(metadata_path, 'w', encoding='utf-8') as f: json.dump(local_metadata, f, indent=2, ensure_ascii=False) - await self.scanner.update_single_lora_cache(local_metadata['file_path'], local_metadata['file_path'], local_metadata) + await self.scanner.update_single_model_cache(local_metadata['file_path'], local_metadata['file_path'], local_metadata) async def fetch_all_civitai(self, request: web.Request) -> web.Response: """Fetch CivitAI metadata for all loras in the background""" @@ -745,7 +745,7 @@ class ApiRoutes: json.dump(metadata, f, indent=2, ensure_ascii=False) # Update cache - await self.scanner.update_single_lora_cache(file_path, file_path, metadata) + await self.scanner.update_single_model_cache(file_path, file_path, metadata) # If model_name was updated, resort the cache if 'model_name' in metadata_updates: @@ -1132,7 +1132,7 @@ class ApiRoutes: # Update the scanner cache if metadata: - await self.scanner.update_single_lora_cache(file_path, new_file_path, metadata) + await self.scanner.update_single_model_cache(file_path, new_file_path, metadata) # Update recipe files and cache if hash is available if hash_value: diff --git a/py/services/lora_scanner.py b/py/services/lora_scanner.py index c4e10c3d..fee5f30b 100644 --- a/py/services/lora_scanner.py +++ b/py/services/lora_scanner.py @@ -315,7 +315,7 @@ class LoraScanner(ModelScanner): break # Update cache - await self.update_single_lora_cache(source_path, target_lora, metadata) + await self.update_single_model_cache(source_path, target_lora, metadata) return True @@ -323,62 +323,6 @@ class LoraScanner(ModelScanner): logger.error(f"Error moving model: {e}", exc_info=True) return False - async def update_single_lora_cache(self, original_path: str, new_path: str, metadata: Dict) -> bool: - cache = await self.get_cached_data() - - # Find the existing item to remove its tags from count - existing_item = next((item for item in cache.raw_data if item['file_path'] == original_path), None) - if existing_item and 'tags' in existing_item: - for tag in existing_item.get('tags', []): - if tag in self._tags_count: - self._tags_count[tag] = max(0, self._tags_count[tag] - 1) - if self._tags_count[tag] == 0: - del self._tags_count[tag] - - # Remove old path from hash index if exists - self._hash_index.remove_by_path(original_path) - - # Remove the old entry from raw_data - cache.raw_data = [ - item for item in cache.raw_data - if item['file_path'] != original_path - ] - - if metadata: - # If this is an update to an existing path (not a move), ensure folder is preserved - if original_path == new_path: - # Find the folder from existing entries or calculate it - existing_folder = next((item['folder'] for item in cache.raw_data - if item['file_path'] == original_path), None) - if existing_folder: - metadata['folder'] = existing_folder - else: - metadata['folder'] = self._calculate_folder(new_path) - else: - # For moved files, recalculate the folder - metadata['folder'] = self._calculate_folder(new_path) - - # Add the updated metadata to raw_data - cache.raw_data.append(metadata) - - # Update hash index with new path - if 'sha256' in metadata: - self._hash_index.add_entry(metadata['sha256'].lower(), new_path) - - # Update folders list - all_folders = set(item['folder'] for item in cache.raw_data) - cache.folders = sorted(list(all_folders), key=lambda x: x.lower()) - - # Update tags count with the new/updated tags - if 'tags' in metadata: - for tag in metadata.get('tags', []): - self._tags_count[tag] = self._tags_count.get(tag, 0) + 1 - - # Resort cache - await cache.resort() - - return True - async def _update_metadata_paths(self, metadata_path: str, lora_path: str) -> Dict: """Update file paths in metadata file""" try: