Add preview URL update mechanism for LoRA scanner cache

This commit is contained in:
Will Miao
2025-02-02 23:30:06 +08:00
parent 4b247995d1
commit 8e8b80ddcf
3 changed files with 80 additions and 8 deletions

View File

@@ -31,6 +31,10 @@ def _find_preview_file(base_name: str, dir_path: str) -> str:
return full_pattern.replace(os.sep, "/")
return ""
def normalize_path(path: str) -> str:
"""Normalize file path to use forward slashes"""
return path.replace(os.sep, "/") if path else path
async def get_file_info(file_path: str) -> LoraMetadata:
"""Get basic file information as LoraMetadata object"""
base_name = os.path.splitext(os.path.basename(file_path))[0]
@@ -41,21 +45,25 @@ async def get_file_info(file_path: str) -> LoraMetadata:
return LoraMetadata(
file_name=base_name,
model_name=base_name,
file_path=file_path,
file_path=normalize_path(file_path),
size=os.path.getsize(file_path),
modified=os.path.getmtime(file_path),
sha256=await calculate_sha256(file_path),
base_model="Unknown", # Will be updated later
from_civitai=True,
preview_url=preview_url,
preview_url=normalize_path(preview_url),
)
async def save_metadata(file_path: str, metadata: LoraMetadata) -> None:
"""Save metadata to .metadata.json file"""
metadata_path = f"{os.path.splitext(file_path)[0]}.metadata.json"
try:
metadata_dict = metadata.to_dict()
metadata_dict['file_path'] = normalize_path(metadata_dict['file_path'])
metadata_dict['preview_url'] = normalize_path(metadata_dict['preview_url'])
with open(metadata_path, 'w', encoding='utf-8') as f:
json.dump(metadata.to_dict(), f, indent=2, ensure_ascii=False)
json.dump(metadata_dict, f, indent=2, ensure_ascii=False)
except Exception as e:
print(f"Error saving metadata to {metadata_path}: {str(e)}")
@@ -66,16 +74,31 @@ async def load_metadata(file_path: str) -> Optional[LoraMetadata]:
if os.path.exists(metadata_path):
with open(metadata_path, 'r', encoding='utf-8') as f:
data = json.load(f)
needs_update = False
if data['file_path'] != normalize_path(data['file_path']):
data['file_path'] = normalize_path(data['file_path'])
needs_update = True
preview_url = data.get('preview_url', '')
# preview_path = os.path.join(loras_root, preview_url) if preview_url else ''
if not preview_url or not os.path.exists(preview_url):
base_name = os.path.splitext(os.path.basename(file_path))[0]
dir_path = os.path.dirname(file_path)
data['preview_url'] = _find_preview_file(base_name, dir_path)
if data['preview_url']:
with open(metadata_path, 'w', encoding='utf-8') as f:
json.dump(data, f, indent=2, ensure_ascii=False)
new_preview_url = normalize_path(_find_preview_file(base_name, dir_path))
if new_preview_url != preview_url:
data['preview_url'] = new_preview_url
needs_update = True
elif preview_url != normalize_path(preview_url):
data['preview_url'] = normalize_path(preview_url)
needs_update = True
if needs_update:
with open(metadata_path, 'w', encoding='utf-8') as f:
json.dump(data, f, indent=2, ensure_ascii=False)
return LoraMetadata.from_dict(data)
except Exception as e:
print(f"Error loading metadata from {metadata_path}: {str(e)}")
return None