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:
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:

View File

@@ -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:

View File

@@ -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);

View File

@@ -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();