mirror of
https://github.com/willmiao/ComfyUI-Lora-Manager.git
synced 2026-03-21 21:22:11 -03:00
feat: normalize LoRA preview URLs for browser accessibility
Add _normalize_preview_url method to ensure preview URLs are properly formatted for browser access. The method handles absolute paths by converting them to static URLs via config.get_preview_static_url, while preserving API paths and other valid URLs. This ensures consistent preview image display across different URL formats. Update _enrich_lora_entry to apply URL normalization to preview URLs obtained from both hash-based lookups and version entries. Add comprehensive test coverage for absolute path normalization scenarios.
This commit is contained in:
@@ -637,7 +637,9 @@ class RecipeScanner:
|
||||
try:
|
||||
if hash_value:
|
||||
lora['inLibrary'] = self._lora_scanner.has_hash(hash_value)
|
||||
lora['preview_url'] = self._lora_scanner.get_preview_url_by_hash(hash_value)
|
||||
lora['preview_url'] = self._normalize_preview_url(
|
||||
self._lora_scanner.get_preview_url_by_hash(hash_value)
|
||||
)
|
||||
lora['localPath'] = self._lora_scanner.get_path_by_hash(hash_value)
|
||||
elif version_entry:
|
||||
lora['inLibrary'] = True
|
||||
@@ -650,16 +652,34 @@ class RecipeScanner:
|
||||
if version_entry.get('sha256') and not lora.get('hash'):
|
||||
lora['hash'] = version_entry.get('sha256')
|
||||
|
||||
preview_url = version_entry.get('preview_url')
|
||||
preview_url = self._normalize_preview_url(version_entry.get('preview_url'))
|
||||
if preview_url:
|
||||
lora.setdefault('preview_url', preview_url)
|
||||
else:
|
||||
lora.setdefault('inLibrary', False)
|
||||
|
||||
if lora.get('preview_url'):
|
||||
lora['preview_url'] = self._normalize_preview_url(lora['preview_url'])
|
||||
except Exception as exc: # pragma: no cover - defensive logging
|
||||
logger.debug("Error enriching lora entry %s: %s", hash_value, exc)
|
||||
|
||||
return lora
|
||||
|
||||
def _normalize_preview_url(self, preview_url: Optional[str]) -> Optional[str]:
|
||||
"""Return a preview URL that is reachable from the browser."""
|
||||
|
||||
if not preview_url or not isinstance(preview_url, str):
|
||||
return preview_url
|
||||
|
||||
normalized = preview_url.strip()
|
||||
if normalized.startswith("/api/lm/previews?path="):
|
||||
return normalized
|
||||
|
||||
if os.path.isabs(normalized):
|
||||
return config.get_preview_static_url(normalized)
|
||||
|
||||
return normalized
|
||||
|
||||
async def get_local_lora(self, name: str) -> Optional[Dict[str, Any]]:
|
||||
"""Lookup a local LoRA model by name."""
|
||||
|
||||
|
||||
@@ -247,3 +247,29 @@ def test_enrich_uses_version_index_when_hash_missing(recipe_scanner):
|
||||
assert enriched["localPath"] == file_path
|
||||
assert enriched["file_name"] == Path(file_path).stem
|
||||
assert enriched["preview_url"] == registered["preview_url"]
|
||||
|
||||
|
||||
def test_enrich_formats_absolute_preview_paths(recipe_scanner, tmp_path):
|
||||
scanner, stub = recipe_scanner
|
||||
version_id = 88
|
||||
preview_path = tmp_path / "loras" / "version-entry.preview.jpeg"
|
||||
preview_path.parent.mkdir(parents=True, exist_ok=True)
|
||||
preview_path.write_text("preview")
|
||||
model_path = tmp_path / "loras" / "version-entry.safetensors"
|
||||
model_path.write_text("weights")
|
||||
|
||||
stub.register_model(
|
||||
"absolute-preview",
|
||||
{
|
||||
"sha256": "feedface",
|
||||
"file_path": str(model_path),
|
||||
"preview_url": str(preview_path),
|
||||
"civitai": {"id": version_id},
|
||||
},
|
||||
)
|
||||
|
||||
lora = {"hash": "", "file_name": "", "modelVersionId": version_id, "strength": 0.5}
|
||||
|
||||
enriched = scanner._enrich_lora_entry(dict(lora))
|
||||
|
||||
assert enriched["preview_url"] == config.get_preview_static_url(str(preview_path))
|
||||
|
||||
Reference in New Issue
Block a user