Refactor Lora cache update logic and enhance context menu item handling

This commit is contained in:
Will Miao
2025-02-18 20:55:31 +08:00
parent 80b860e98a
commit dac07308c3
4 changed files with 28 additions and 36 deletions

View File

@@ -330,6 +330,8 @@ class ApiRoutes:
with open(metadata_path, 'w', encoding='utf-8') as f: with open(metadata_path, 'w', encoding='utf-8') as f:
json.dump(local_metadata, f, indent=2, ensure_ascii=False) 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: async def fetch_all_civitai(self, request: web.Request) -> web.Response:
"""Fetch CivitAI metadata for all loras in the background""" """Fetch CivitAI metadata for all loras in the background"""
try: try:

View File

@@ -381,20 +381,8 @@ class LoraScanner:
shutil.move(source_preview, target_preview) shutil.move(source_preview, target_preview)
break break
# Update cache folders # Update cache
cache = await self.get_cached_data() await self.update_single_lora_cache(source_path, lora_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()
return True return True
@@ -402,6 +390,23 @@ class LoraScanner:
logger.error(f"Error moving model: {e}", exc_info=True) logger.error(f"Error moving model: {e}", exc_info=True)
return False 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: async def _update_metadata_paths(self, metadata_path: str, lora_path: str) -> Dict:
"""Update file paths in metadata file""" """Update file paths in metadata file"""
try: try:
@@ -423,7 +428,6 @@ class LoraScanner:
with open(metadata_path, 'w', encoding='utf-8') as f: with open(metadata_path, 'w', encoding='utf-8') as f:
json.dump(metadata, f, indent=2, ensure_ascii=False) json.dump(metadata, f, indent=2, ensure_ascii=False)
metadata['folder'] = self._calculate_folder(lora_path)
return metadata return metadata
except Exception as e: except Exception as e:

View File

@@ -26,16 +26,6 @@
color: var(--lora-text); 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 { .context-menu-separator {
height: 1px; height: 1px;
background-color: var(--border-color); background-color: var(--border-color);

View File

@@ -23,11 +23,6 @@ export class LoraContextMenu {
const menuItem = e.target.closest('.context-menu-item'); const menuItem = e.target.closest('.context-menu-item');
if (!menuItem || !this.currentCard) return; if (!menuItem || !this.currentCard) return;
// Don't process disabled items
if (menuItem.classList.contains('disabled')) {
return;
}
const action = menuItem.dataset.action; const action = menuItem.dataset.action;
if (!action) return; if (!action) return;
@@ -39,8 +34,14 @@ export class LoraContextMenu {
case 'civitai': case 'civitai':
// Only trigger if the card is from civitai // Only trigger if the card is from civitai
if (this.currentCard.dataset.from_civitai === 'true') { if (this.currentCard.dataset.from_civitai === 'true') {
if (this.currentCard.dataset.meta === '{}') {
showToast('Please fetch metadata from CivitAI first', 'info');
} else {
this.currentCard.querySelector('.fa-globe')?.click(); this.currentCard.querySelector('.fa-globe')?.click();
} }
} else {
showToast('No CivitAI information available', 'info');
}
break; break;
case 'copyname': case 'copyname':
this.currentCard.querySelector('.fa-copy')?.click(); this.currentCard.querySelector('.fa-copy')?.click();
@@ -67,11 +68,6 @@ export class LoraContextMenu {
this.currentCard = card; this.currentCard = card;
this.menu.style.display = 'block'; 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(); const menuRect = this.menu.getBoundingClientRect();