From dac07308c34725b5b7c05b4f804452db427a635c Mon Sep 17 00:00:00 2001 From: Will Miao <13051207myq@gmail.com> Date: Tue, 18 Feb 2025 20:55:31 +0800 Subject: [PATCH] Refactor Lora cache update logic and enhance context menu item handling --- routes/api_routes.py | 2 ++ services/lora_scanner.py | 34 ++++++++++++++++------------- static/css/components/menu.css | 10 --------- static/js/components/ContextMenu.js | 18 ++++++--------- 4 files changed, 28 insertions(+), 36 deletions(-) diff --git a/routes/api_routes.py b/routes/api_routes.py index 686743ad..a4cf2f7c 100644 --- a/routes/api_routes.py +++ b/routes/api_routes.py @@ -330,6 +330,8 @@ 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) + async def fetch_all_civitai(self, request: web.Request) -> web.Response: """Fetch CivitAI metadata for all loras in the background""" try: diff --git a/services/lora_scanner.py b/services/lora_scanner.py index 52ade509..85336eb9 100644 --- a/services/lora_scanner.py +++ b/services/lora_scanner.py @@ -381,26 +381,31 @@ class LoraScanner: shutil.move(source_preview, target_preview) break - # Update cache folders - cache = await self.get_cached_data() - cache.raw_data = [ - item for item in cache.raw_data - if item['file_path'] != source_path - ] - if lora_data: - cache.raw_data.append(lora_data) - all_folders = set(cache.folders) - all_folders.add(lora_data['folder']) - cache.folders = sorted(list(all_folders), key=lambda x: x.lower()) - - # Resort cache - await cache.resort() + # Update cache + await self.update_single_lora_cache(source_path, lora_data) return True except Exception as e: logger.error(f"Error moving model: {e}", exc_info=True) return False + + async def update_single_lora_cache(self, file_path: str, metadata: Dict) -> bool: + cache = await self.get_cached_data() + cache.raw_data = [ + item for item in cache.raw_data + if item['file_path'] != file_path + ] + if metadata: + metadata['folder'] = self._calculate_folder(file_path) + cache.raw_data.append(metadata) + all_folders = set(cache.folders) + all_folders.add(metadata['folder']) + cache.folders = sorted(list(all_folders), key=lambda x: x.lower()) + + # Resort cache + await cache.resort() + async def _update_metadata_paths(self, metadata_path: str, lora_path: str) -> Dict: """Update file paths in metadata file""" @@ -423,7 +428,6 @@ class LoraScanner: with open(metadata_path, 'w', encoding='utf-8') as f: json.dump(metadata, f, indent=2, ensure_ascii=False) - metadata['folder'] = self._calculate_folder(lora_path) return metadata except Exception as e: diff --git a/static/css/components/menu.css b/static/css/components/menu.css index e53399ba..819cfe72 100644 --- a/static/css/components/menu.css +++ b/static/css/components/menu.css @@ -26,16 +26,6 @@ color: var(--lora-text); } -.context-menu-item.disabled { - opacity: 0.5; - cursor: not-allowed; -} - -.context-menu-item.disabled:hover { - background: var(--lora-surface); - color: var(--text-color); -} - .context-menu-separator { height: 1px; background-color: var(--border-color); diff --git a/static/js/components/ContextMenu.js b/static/js/components/ContextMenu.js index 4cbb9c01..7cb13884 100644 --- a/static/js/components/ContextMenu.js +++ b/static/js/components/ContextMenu.js @@ -22,11 +22,6 @@ export class LoraContextMenu { this.menu.addEventListener('click', (e) => { const menuItem = e.target.closest('.context-menu-item'); if (!menuItem || !this.currentCard) return; - - // Don't process disabled items - if (menuItem.classList.contains('disabled')) { - return; - } const action = menuItem.dataset.action; if (!action) return; @@ -39,7 +34,13 @@ export class LoraContextMenu { case 'civitai': // Only trigger if the card is from civitai if (this.currentCard.dataset.from_civitai === 'true') { - this.currentCard.querySelector('.fa-globe')?.click(); + if (this.currentCard.dataset.meta === '{}') { + showToast('Please fetch metadata from CivitAI first', 'info'); + } else { + this.currentCard.querySelector('.fa-globe')?.click(); + } + } else { + showToast('No CivitAI information available', 'info'); } break; case 'copyname': @@ -66,11 +67,6 @@ export class LoraContextMenu { showMenu(x, y, card) { this.currentCard = card; this.menu.style.display = 'block'; - - // Update civitai menu item state - const civitaiItem = this.menu.querySelector('[data-action="civitai"]'); - const fromCivitai = card.dataset.from_civitai === 'true'; - civitaiItem.classList.toggle('disabled', !fromCivitai); // 获取菜单尺寸 const menuRect = this.menu.getBoundingClientRect();