mirror of
https://github.com/willmiao/ComfyUI-Lora-Manager.git
synced 2026-03-24 06:32:12 -03:00
feat(civitai): implement URL rewriting for Civitai previews and enhance download handling, fixes #499
This commit is contained in:
48
py/utils/civitai_utils.py
Normal file
48
py/utils/civitai_utils.py
Normal file
@@ -0,0 +1,48 @@
|
||||
"""Utilities for working with Civitai assets."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from urllib.parse import urlparse, urlunparse
|
||||
|
||||
|
||||
def rewrite_preview_url(source_url: str | None, media_type: str | None = None) -> tuple[str | None, bool]:
|
||||
"""Rewrite Civitai preview URLs to use optimized renditions.
|
||||
|
||||
Args:
|
||||
source_url: Original preview URL from the Civitai API.
|
||||
media_type: Optional media type hint (e.g. ``"image"`` or ``"video"``).
|
||||
|
||||
Returns:
|
||||
A tuple of the potentially rewritten URL and a flag indicating whether the
|
||||
replacement occurred. When the URL is not rewritten, the original value is
|
||||
returned with ``False``.
|
||||
"""
|
||||
if not source_url:
|
||||
return source_url, False
|
||||
|
||||
try:
|
||||
parsed = urlparse(source_url)
|
||||
except ValueError:
|
||||
return source_url, False
|
||||
|
||||
if parsed.netloc.lower() != "image.civitai.com":
|
||||
return source_url, False
|
||||
|
||||
replacement = "/width=450,optimized=true"
|
||||
if (media_type or "").lower() == "video":
|
||||
replacement = "/transcode=true,width=450,optimized=true"
|
||||
|
||||
if "/original=true" not in parsed.path:
|
||||
return source_url, False
|
||||
|
||||
updated_path = parsed.path.replace("/original=true", replacement, 1)
|
||||
if updated_path == parsed.path:
|
||||
return source_url, False
|
||||
|
||||
rewritten = urlunparse(parsed._replace(path=updated_path))
|
||||
print(rewritten)
|
||||
return rewritten, True
|
||||
|
||||
|
||||
__all__ = ["rewrite_preview_url"]
|
||||
|
||||
Reference in New Issue
Block a user