feat(civitai): enhance model version handling with cache lookup

This commit is contained in:
Will Miao
2025-10-09 14:10:00 +08:00
parent 5dcf0a1e48
commit b1dd733493

View File

@@ -825,18 +825,30 @@ class ModelCivitaiHandler:
status=400, status=400,
) )
cache = await self._service.scanner.get_cached_data()
version_index = cache.version_index
for version in versions: for version in versions:
model_file = self._find_model_file(version.get("files", [])) if isinstance(version.get("files"), Iterable) else None version_id = None
if model_file: version_id_raw = version.get("id")
hashes = model_file.get("hashes", {}) if isinstance(model_file, Mapping) else {} if version_id_raw is not None:
sha256 = hashes.get("SHA256") if isinstance(hashes, Mapping) else None try:
if sha256: version_id = int(str(version_id_raw))
version["existsLocally"] = self._service.has_hash(sha256) except (TypeError, ValueError):
if version["existsLocally"]: version_id = None
version["localPath"] = self._service.get_path_by_hash(sha256)
version["modelSizeKB"] = model_file.get("sizeKB") if isinstance(model_file, Mapping) else None cache_entry = version_index.get(version_id) if (version_id is not None and version_index) else None
version["existsLocally"] = cache_entry is not None
if cache_entry and isinstance(cache_entry, Mapping):
local_path = cache_entry.get("file_path")
if local_path:
version["localPath"] = local_path
else: else:
version["existsLocally"] = False version.pop("localPath", None)
model_file = self._find_model_file(version.get("files", [])) if isinstance(version.get("files"), Iterable) else None
if model_file and isinstance(model_file, Mapping):
version["modelSizeKB"] = model_file.get("sizeKB")
return web.json_response(versions) return web.json_response(versions)
except Exception as exc: except Exception as exc:
self._logger.error("Error fetching %s model versions: %s", self._service.model_type, exc) self._logger.error("Error fetching %s model versions: %s", self._service.model_type, exc)