refactor: unify SHA256 extraction paths

This commit is contained in:
Dariusz L
2026-08-07 03:36:06 +02:00
parent f451d0df92
commit 2d0576546f
5 changed files with 23 additions and 30 deletions
+1 -5
View File
@@ -768,11 +768,7 @@ def _same_file(file_info: Dict[str, Any], mirror: Dict[str, Any]) -> bool:
if file_name and mirror_name and file_name == mirror_name:
return True
file_sha = (
file_info.get("sha256")
or (file_info.get("hashes") or {}).get("SHA256")
or (file_info.get("hashes") or {}).get("sha256")
)
file_sha = extract_file_sha256(file_info)
mirror_sha = mirror.get("sha256") or mirror.get("hash")
return bool(file_sha and mirror_sha and str(file_sha).lower() == str(mirror_sha).lower())
+3 -6
View File
@@ -44,6 +44,7 @@ from ..type_utils import (
build_model_result,
check_credential_http,
check_credential_preconditions,
extract_file_sha256,
extract_file_size,
extract_trained_words,
first_non_empty,
@@ -171,7 +172,7 @@ def build_civitai_result_payload(
) -> Dict[str, Any]:
"""Build the shared CivitAI result fields used by search entry points."""
hashes = file_info.get("hashes") if isinstance(file_info.get("hashes"), dict) else {}
sha256 = file_info.get("sha256") or hashes.get("SHA256") or hashes.get("sha256")
sha256 = extract_file_sha256(file_info)
payload = {
"model_id": model_id,
"version_id": version_id,
@@ -1661,11 +1662,7 @@ def get_model_info_by_hash(
file_info
for file_info in files
if isinstance(file_info, dict)
and normalize_sha256(
file_info.get("sha256")
or (file_info.get("hashes") or {}).get("SHA256")
or (file_info.get("hashes") or {}).get("sha256")
)
and normalize_sha256(extract_file_sha256(file_info))
== file_hash.lower()
),
None,
+9 -13
View File
@@ -27,6 +27,7 @@ from ..path_utils import get_filename_from_path
from ..progress import get_progress_reporter
from ..type_utils import (
build_model_result,
extract_file_sha256,
extract_file_size,
get_generic_filename_tokens,
normalize_alphanumeric_key,
@@ -254,12 +255,7 @@ def _find_hash_match_row(
file_info = _build_file_info(row["file_data"])
if not file_info:
continue
hashes = file_info.get("hashes") or {}
file_sha256 = normalize_sha256(
file_info.get("sha256")
or hashes.get("SHA256")
or hashes.get("sha256")
)
file_sha256 = normalize_sha256(extract_file_sha256(file_info))
if file_sha256 == requested_sha256:
full_row = _load_full_rows_for_versions(
conn, [row["version_id"]]
@@ -405,6 +401,11 @@ def _build_result_from_row(
version_id = row["version_id"]
files = _load_version_files(conn, version_id)
primary_file = select_primary_model_file(files)
primary_hashes = (
primary_file.get("hashes")
if primary_file and isinstance(primary_file.get("hashes"), dict)
else {}
)
filename = (
primary_file.get("name")
if primary_file
@@ -412,16 +413,11 @@ def _build_result_from_row(
if isinstance(version_data.get("files"), list) and version_data.get("files")
else ""
)
primary_hashes = (
primary_file.get("hashes")
if primary_file and isinstance(primary_file.get("hashes"), dict)
else {}
)
primary_sha256 = (
primary_file.get("sha256")
normalize_sha256(extract_file_sha256(primary_file))
if primary_file
else ""
) or primary_hashes.get("SHA256") or primary_hashes.get("sha256")
)
tags = model_data.get("tags", [])
if not isinstance(tags, list):
+2 -6
View File
@@ -1,4 +1,4 @@
import { getSha256Field, normalizeSha256 } from '../utils/hash_utils.js';
import { firstValidSha256, getSha256Field, normalizeSha256 } from '../utils/hash_utils.js';
import { normalizeSourceKey } from '../utils/source_labels.js';
export const searchHashMethods = {
@@ -20,11 +20,7 @@ export const searchHashMethods = {
fileHashes.sha256
];
for (const candidate of candidates) {
const hash = normalizeSha256(candidate);
if (hash) return hash;
}
return '';
return firstValidSha256(...candidates);
},
collectHashLabelMapHashes(missing = {}, results = null) {
+8
View File
@@ -13,3 +13,11 @@ export function normalizeSha256(value = '') {
text = text.replace(/^sha256[:=]/i, '').trim().toLowerCase();
return /^[a-f0-9]{64}$/.test(text) ? text : '';
}
export function firstValidSha256(...values) {
for (const value of values) {
const hash = normalizeSha256(value);
if (hash) return hash;
}
return '';
}