From e04ef671e9500a5146ed5bdae915c6703b5f5fab Mon Sep 17 00:00:00 2001 From: Will Miao <13051207myq@gmail.com> Date: Tue, 22 Jul 2025 22:04:39 +0800 Subject: [PATCH] feat: Update metadata handling to use current timestamp for model modifications --- py/services/lora_cache.py | 65 ------------------------------------ py/utils/metadata_manager.py | 7 ++-- py/utils/models.py | 7 +--- 3 files changed, 5 insertions(+), 74 deletions(-) delete mode 100644 py/services/lora_cache.py diff --git a/py/services/lora_cache.py b/py/services/lora_cache.py deleted file mode 100644 index 4bb00189..00000000 --- a/py/services/lora_cache.py +++ /dev/null @@ -1,65 +0,0 @@ -import asyncio -from typing import List, Dict -from dataclasses import dataclass -from operator import itemgetter -from natsort import natsorted - -@dataclass -class LoraCache: - """Cache structure for LoRA data""" - raw_data: List[Dict] - sorted_by_name: List[Dict] - sorted_by_date: List[Dict] - folders: List[str] - - def __post_init__(self): - self._lock = asyncio.Lock() - - async def resort(self, name_only: bool = False): - """Resort all cached data views""" - async with self._lock: - self.sorted_by_name = natsorted( - self.raw_data, - key=lambda x: x['model_name'].lower() # Case-insensitive sort - ) - if not name_only: - self.sorted_by_date = sorted( - self.raw_data, - key=itemgetter('modified'), - reverse=True - ) - # Update folder list - all_folders = set(l['folder'] for l in self.raw_data) - self.folders = sorted(list(all_folders), key=lambda x: x.lower()) - - async def update_preview_url(self, file_path: str, preview_url: str) -> bool: - """Update preview_url for a specific lora in all cached data - - Args: - file_path: The file path of the lora to update - preview_url: The new preview URL - - Returns: - bool: True if the update was successful, False if the lora wasn't found - """ - async with self._lock: - # Update in raw_data - for item in self.raw_data: - if item['file_path'] == file_path: - item['preview_url'] = preview_url - break - else: - return False # Lora not found - - # Update in sorted lists (references to the same dict objects) - for item in self.sorted_by_name: - if item['file_path'] == file_path: - item['preview_url'] = preview_url - break - - for item in self.sorted_by_date: - if item['file_path'] == file_path: - item['preview_url'] = preview_url - break - - return True \ No newline at end of file diff --git a/py/utils/metadata_manager.py b/py/utils/metadata_manager.py index 7719ffdf..694a4c0c 100644 --- a/py/utils/metadata_manager.py +++ b/py/utils/metadata_manager.py @@ -1,3 +1,4 @@ +from datetime import datetime import os import json import shutil @@ -196,7 +197,7 @@ class MetadataManager: model_name=base_name, file_path=normalize_path(file_path), size=os.path.getsize(real_path), - modified=os.path.getmtime(real_path), + modified=datetime.now().timestamp(), sha256=sha256, base_model="Unknown", preview_url=normalize_path(preview_url), @@ -211,7 +212,7 @@ class MetadataManager: model_name=base_name, file_path=normalize_path(file_path), size=os.path.getsize(real_path), - modified=os.path.getmtime(real_path), + modified=datetime.now().timestamp(), sha256=sha256, base_model="Unknown", preview_url=normalize_path(preview_url), @@ -222,7 +223,7 @@ class MetadataManager: ) # Try to extract model-specific metadata - await MetadataManager._enrich_metadata(metadata, real_path) + # await MetadataManager._enrich_metadata(metadata, real_path) # Save the created metadata await MetadataManager.save_metadata(file_path, metadata, create_backup=False) diff --git a/py/utils/models.py b/py/utils/models.py index a3d043b1..3947e671 100644 --- a/py/utils/models.py +++ b/py/utils/models.py @@ -11,7 +11,7 @@ class BaseModelMetadata: model_name: str # The model's name defined by the creator file_path: str # Full path to the model file size: int # File size in bytes - modified: float # Last modified timestamp + modified: float # Timestamp when the model was added to the management system sha256: str # SHA256 hash of the file base_model: str # Base model type (SD1.5/SD2.1/SDXL/etc.) preview_url: str # Preview image URL @@ -73,11 +73,6 @@ class BaseModelMetadata: return result - @property - def modified_datetime(self) -> datetime: - """Convert modified timestamp to datetime object""" - return datetime.fromtimestamp(self.modified) - def update_civitai_info(self, civitai_data: Dict) -> None: """Update Civitai information""" self.civitai = civitai_data