refactor: simplify aria2 status route and remove wrapper functions
- Merge GET and POST aria2 status handlers into one route - Remove redundant calculate_file_sha256 wrapper and use path_utils directly - Remove redundant _normalize_archive_image, _normalize_filename, and _similarity wrappers - Replace urllib with requests for remote JSON fetching in model_list - Update metadata timestamps
This commit is contained in:
+10
-11
@@ -4258,22 +4258,21 @@ class ModelResolverExtension:
|
||||
return web.json_response(result, status=status)
|
||||
|
||||
@routes.get("/model_resolver/aria2/status")
|
||||
@routes.post("/model_resolver/aria2/status")
|
||||
@json_api_endpoint("aria2 status")
|
||||
async def aria2_status_route(request):
|
||||
"""Report aria2 availability using saved settings."""
|
||||
"""Report aria2 availability using saved settings, with optional override via POST."""
|
||||
settings = await asyncio.to_thread(load_resolver_settings)
|
||||
if request.method == "POST":
|
||||
try:
|
||||
payload = await request.json()
|
||||
if isinstance(payload, dict) and "aria2c_path" in payload:
|
||||
settings = dict(settings)
|
||||
settings["aria2c_path"] = payload.get("aria2c_path", "")
|
||||
except Exception:
|
||||
pass
|
||||
return web.json_response(await asyncio.to_thread(get_aria2_status, settings))
|
||||
|
||||
@routes.post("/model_resolver/aria2/status")
|
||||
@json_api_endpoint("aria2 status POST")
|
||||
async def aria2_status_check_route(request):
|
||||
"""Report aria2 availability using an optional unsaved aria2c path."""
|
||||
payload = await request.json()
|
||||
settings = await asyncio.to_thread(load_resolver_settings)
|
||||
if isinstance(payload, dict) and "aria2c_path" in payload:
|
||||
settings = dict(settings)
|
||||
settings["aria2c_path"] = payload.get("aria2c_path", "")
|
||||
return web.json_response(await asyncio.to_thread(get_aria2_status, settings))
|
||||
|
||||
@routes.post("/model_resolver/aria2/start")
|
||||
@json_api_endpoint("aria2 start", return_success_on_error=True)
|
||||
|
||||
+2
-7
@@ -24,7 +24,7 @@ log = create_module_logger(__name__)
|
||||
|
||||
from .resolver import invalidate_local_hash_match_cache, normalize_sha256
|
||||
from .scanner import invalidate_model_files_cache
|
||||
from .path_utils import is_path_within, get_path_identity, write_json_atomic, read_json_safe, get_comfy_root_path, calculate_file_sha256 as _calculate_file_sha256, get_filename_from_path
|
||||
from .path_utils import is_path_within, get_path_identity, write_json_atomic, read_json_safe, get_comfy_root_path, calculate_file_sha256, get_filename_from_path
|
||||
from .type_utils import as_dict, as_list, first_non_empty, format_size_bytes as format_bytes, DEFAULT_BROWSER_USER_AGENT, extract_response_file_size, normalize_category_to_model_type
|
||||
|
||||
try:
|
||||
@@ -375,11 +375,6 @@ def read_completed_metadata_sha256(file_path: str) -> str:
|
||||
return normalize_sha256(payload.get("sha256") or payload.get("hash"))
|
||||
|
||||
|
||||
def calculate_file_sha256(file_path: str) -> str:
|
||||
"""Calculate SHA256 for an existing local file."""
|
||||
return _calculate_file_sha256(file_path) or ""
|
||||
|
||||
|
||||
def build_lora_manager_metadata(
|
||||
dest_path: str,
|
||||
metadata: Optional[Dict[str, Any]] = None,
|
||||
@@ -1714,7 +1709,7 @@ def download_model(
|
||||
if not existing_sha256:
|
||||
sha256_source = "file"
|
||||
log.info(f"File exists, verifying SHA256: {dest_path}")
|
||||
existing_sha256 = calculate_file_sha256(dest_path)
|
||||
existing_sha256 = calculate_file_sha256(dest_path) or ""
|
||||
except Exception as e:
|
||||
error_msg = (
|
||||
f"File already exists and its SHA256 could not be verified: {dest_path}"
|
||||
|
||||
@@ -942,8 +942,6 @@ def _normalize_archive_mirrors(file_info: Dict[str, Any]) -> List[Dict[str, Any]
|
||||
return non_civitai + civitai
|
||||
|
||||
|
||||
def _normalize_archive_image(image: Dict[str, Any]) -> Dict[str, Any]:
|
||||
return normalize_model_image(image)
|
||||
|
||||
|
||||
|
||||
@@ -988,7 +986,7 @@ def _normalize_archive_version(
|
||||
if not isinstance(raw_images, list):
|
||||
raw_images = [raw_images]
|
||||
images = [
|
||||
_normalize_archive_image(image)
|
||||
normalize_model_image(image)
|
||||
for image in raw_images
|
||||
if isinstance(image, dict) and (image.get("url") or image.get("imageUrl") or image.get("src"))
|
||||
]
|
||||
|
||||
+15
-25
@@ -5,13 +5,12 @@ Search the ComfyUI Manager model-list.json database with fuzzy matching.
|
||||
"""
|
||||
|
||||
import os
|
||||
import json
|
||||
import hashlib
|
||||
import shutil
|
||||
import tempfile
|
||||
from datetime import datetime, timezone
|
||||
from typing import Dict, Any, Optional, List
|
||||
from urllib.request import Request, urlopen
|
||||
import requests
|
||||
|
||||
from ..matcher import normalize_filename, calculate_similarity
|
||||
|
||||
@@ -39,15 +38,13 @@ def _utc_now_iso() -> str:
|
||||
|
||||
|
||||
def _fetch_json_url(url: str) -> Dict[str, Any]:
|
||||
request = Request(
|
||||
url,
|
||||
headers={
|
||||
"Accept": "application/json",
|
||||
"User-Agent": "comfyui-model-resolver",
|
||||
},
|
||||
)
|
||||
with urlopen(request, timeout=HTTP_TIMEOUT) as response:
|
||||
return json.loads(response.read().decode("utf-8"))
|
||||
headers = {
|
||||
"Accept": "application/json",
|
||||
"User-Agent": "comfyui-model-resolver",
|
||||
}
|
||||
response = requests.get(url, headers=headers, timeout=HTTP_TIMEOUT)
|
||||
response.raise_for_status()
|
||||
return response.json()
|
||||
|
||||
|
||||
|
||||
@@ -173,15 +170,8 @@ def update_model_list_from_remote() -> Dict[str, Any]:
|
||||
}
|
||||
|
||||
|
||||
def _normalize_filename(filename: str) -> str:
|
||||
"""Normalize filename for comparison."""
|
||||
return normalize_filename(filename)
|
||||
|
||||
|
||||
def _similarity(a: str, b: str) -> float:
|
||||
"""Calculate similarity between two strings."""
|
||||
return calculate_similarity(a, b)
|
||||
|
||||
|
||||
def search_model_list(
|
||||
filename: str, exact_only: bool = False
|
||||
@@ -204,7 +194,7 @@ def search_model_list(
|
||||
|
||||
filename_lower = filename.lower()
|
||||
filename_base = os.path.splitext(filename_lower)[0]
|
||||
filename_norm = _normalize_filename(filename)
|
||||
filename_norm = normalize_filename(filename)
|
||||
|
||||
# 1. Exact match first (always try this)
|
||||
for model in models:
|
||||
@@ -241,7 +231,7 @@ def search_model_list(
|
||||
if filename_base in model_base or model_base in filename_base:
|
||||
url = model.get("url", "")
|
||||
if url:
|
||||
score = _similarity(filename_norm, _normalize_filename(model_filename))
|
||||
score = calculate_similarity(filename_norm, normalize_filename(model_filename))
|
||||
return {
|
||||
"source": "model_list",
|
||||
"filename": model_filename,
|
||||
@@ -263,8 +253,8 @@ def search_model_list(
|
||||
if not model_filename:
|
||||
continue
|
||||
|
||||
model_norm = _normalize_filename(model_filename)
|
||||
score = _similarity(filename_norm, model_norm)
|
||||
model_norm = normalize_filename(model_filename)
|
||||
score = calculate_similarity(filename_norm, model_norm)
|
||||
|
||||
if score > best_score and score > 0.5: # Require 50% similarity
|
||||
url = model.get("url", "")
|
||||
@@ -300,7 +290,7 @@ def search_model_list_multiple(filename: str, limit: int = 5) -> List[Dict[str,
|
||||
if not models:
|
||||
return []
|
||||
|
||||
filename_norm = _normalize_filename(filename)
|
||||
filename_norm = normalize_filename(filename)
|
||||
results = []
|
||||
|
||||
for model in models:
|
||||
@@ -308,8 +298,8 @@ def search_model_list_multiple(filename: str, limit: int = 5) -> List[Dict[str,
|
||||
if not model_filename:
|
||||
continue
|
||||
|
||||
model_norm = _normalize_filename(model_filename)
|
||||
score = _similarity(filename_norm, model_norm)
|
||||
model_norm = normalize_filename(model_filename)
|
||||
score = calculate_similarity(filename_norm, model_norm)
|
||||
|
||||
if score > 0.4: # Minimum 40% similarity
|
||||
url = model.get("url", "")
|
||||
|
||||
@@ -16,7 +16,7 @@ from ..log_system import create_module_logger
|
||||
log = create_module_logger(__name__)
|
||||
|
||||
|
||||
from ..path_utils import METADATA_DIR, read_json_safe, write_json_atomic
|
||||
from ..path_utils import METADATA_DIR, read_json_safe, write_json_atomic, save_catalog_with_backup
|
||||
POPULAR_MODELS_FILE = os.path.join(METADATA_DIR, "popular-models.json")
|
||||
MODEL_ALIASES_FILE = os.path.join(METADATA_DIR, "model-aliases.json")
|
||||
BASE_MODELS_FILE = os.path.join(METADATA_DIR, "base-models.json")
|
||||
|
||||
File diff suppressed because one or more lines are too long
@@ -5,5 +5,5 @@
|
||||
"sha": "5d9308f50a08cf3cfb47e6a25cc525425714d3ad",
|
||||
"size": 274526,
|
||||
"model_count": 538,
|
||||
"updated_at": "2026-06-22T01:35:07+00:00"
|
||||
"updated_at": "2026-07-03T22:59:41+00:00"
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user