Checkpoint

This commit is contained in:
Will Miao
2025-02-04 10:50:57 +08:00
parent c9390c9d32
commit 5ed037b219
3 changed files with 49 additions and 63 deletions

View File

@@ -4,7 +4,6 @@ import logging
from aiohttp import web
from typing import Dict, List
from ..services.civitai_client import CivitaiClient
from ..utils.file_utils import update_civitai_metadata, load_metadata
from ..config import config
from ..services.lora_scanner import LoraScanner
from operator import itemgetter

View File

@@ -62,48 +62,37 @@ class LoraFileHandler(FileSystemEventHandler):
logger.info(f"Processing {len(changes)} file changes")
async with self.scanner._cache._lock:
# 获取当前缓存
cache = await self.scanner.get_cached_data()
needs_resort = False
new_folders = set() # 用于收集新的文件夹
for action, file_path in changes:
try:
if action == 'add':
# 扫描新文件
lora_data = await self.scanner.scan_single_lora(file_path)
if lora_data:
cache.raw_data.append(lora_data)
new_folders.add(lora_data['folder']) # 收集新文件夹
needs_resort = True
elif action == 'remove':
# 从缓存中移除
cache.raw_data = [
item for item in cache.raw_data
if item['file_path'] != file_path
]
cache = await self.scanner.get_cached_data() # 先完成可能的初始化
needs_resort = False
new_folders = set() # 用于收集新的文件夹
for action, file_path in changes:
try:
if action == 'add':
# 扫描新文件
lora_data = await self.scanner.scan_single_lora(file_path)
if lora_data:
cache.raw_data.append(lora_data)
new_folders.add(lora_data['folder']) # 收集新文件夹
needs_resort = True
except Exception as e:
logger.error(f"Error processing {action} for {file_path}: {e}")
elif action == 'remove':
# 从缓存中移除
cache.raw_data = [
item for item in cache.raw_data
if item['file_path'] != file_path
]
needs_resort = True
except Exception as e:
logger.error(f"Error processing {action} for {file_path}: {e}")
if needs_resort:
await cache.resort()
if needs_resort:
cache.sorted_by_name = sorted(
self.scanner._cache.raw_data,
key=lambda x: x['model_name'].lower() # Case-insensitive sort
)
cache.sorted_by_date = sorted(
self.scanner._cache.raw_data,
key=itemgetter('modified'),
reverse=True
)
# 更新文件夹列表,包括新添加的文件夹
all_folders = set(cache.folders) | new_folders
cache.folders = sorted(list(all_folders))
# 更新文件夹列表,包括新添加的文件夹
all_folders = set(cache.folders) | new_folders
cache.folders = sorted(list(all_folders))
except Exception as e:
logger.error(f"Error in process_changes: {e}")

View File

@@ -91,28 +91,26 @@ class LoraScanner:
# 确保缓存已初始化
cache = await self.get_cached_data()
async with cache._lock:
# Select sorted data based on sort_by parameter
data = (cache.sorted_by_date if sort_by == 'date'
else cache.sorted_by_name)
# Select sorted data based on sort_by parameter
data = (cache.sorted_by_date if sort_by == 'date'
else cache.sorted_by_name)
# Apply folder filter if specified
if folder is not None:
data = [item for item in data if item['folder'] == folder]
# Calculate pagination
total_items = len(data)
start_idx = (page - 1) * page_size
end_idx = min(start_idx + page_size, total_items)
return {
'items': data[start_idx:end_idx],
'total': total_items,
'page': page,
'page_size': page_size,
'total_pages': (total_items + page_size - 1) // page_size
}
# Apply folder filter if specified
if folder is not None:
data = [item for item in data if item['folder'] == folder]
# Calculate pagination
total_items = len(data)
start_idx = (page - 1) * page_size
end_idx = min(start_idx + page_size, total_items)
return {
'items': data[start_idx:end_idx],
'total': total_items,
'page': page,
'page_size': page_size,
'total_pages': (total_items + page_size - 1) // page_size
}
def invalidate_cache(self):
"""Invalidate the current cache"""
@@ -176,8 +174,8 @@ class LoraScanner:
"""
if self._cache is None:
return False
return self._cache.update_preview_url(file_path, preview_url)
return await self._cache.update_preview_url(file_path, preview_url)
async def scan_single_lora(self, file_path: str) -> Optional[Dict]:
"""Scan a single LoRA file and return its metadata"""