mirror of
https://github.com/comfyanonymous/ComfyUI.git
synced 2026-07-10 16:37:36 +08:00
* feat(assets): add namespaced model type tags * fix(assets): mark path-derived upload tags automatic * fix(assets): merge duplicate scan specs * test(assets): make duplicate path normalization portable * feat(assets): add loader_path as the authoritative loader locator (#14796) * fix(assets): filter model_type tags by bucket extension sets Buckets sharing a base directory (e.g. diffusion_models and a custom unet_gguf) tagged every file in the directory regardless of whether the bucket could load it, so .safetensors files were tagged model_type:unet_gguf and vice versa. Carry each bucket's registered extension set through get_comfy_models_folders and only emit a model_type tag when the file extension matches, keeping the empty-set match-all convention from folder_paths.filter_files_extensions. Files under a model base matching no bucket now keep only the models tag instead of every directory-matching model_type tag. * feat(assets): replace response file_path with persisted loader_path The old file_path response field was a namespaced storage locator (models/checkpoints/foo.safetensors): not an absolute path, not unique identity, and not the value a loader consumes. Nothing needs that shape on the wire (hash/ID-based locating is the long-term direction), so it is dropped rather than renamed; the storage-root matching stays internal, powering display_name. What loaders DO need is the in-root loader path (category dropped: models/checkpoints/foo/bar.safetensors -> foo/bar.safetensors). Serve it as a first-class loader_path field, persisted on asset_references (migration 0006) and written by every ingest pipeline at insert, so responses read the column verbatim. Like the model_type tags, loader_path is a seed-time derivative of the model folder registry, maintained by the same scan lifecycle (new files seed fresh values, pruning retires rows whose bucket disappeared). Rows predating the column serve a null loader_path; databases from before this stack already need recreating for the base branch's tag changes. loader_path resolves every registered base including extra_model_paths entries; display_name only the canonical storage roots. A file can therefore be loadable with no display name (extra-path models) or the reverse (unregistered files under the models root), and loader_path is null exactly when no loader can resolve the file. * test(assets): lock loader_path matrix (asymmetry, null, persist/read) Cover the behaviour that has no production change but is easy to regress: the extra-path asymmetry (loadable but no storage namespace), null loader_path persistence for orphan files, and the response reading the stored column with a compute fallback for un-backfilled rows. * fix(assets): persist subfolder-qualified loader_path for ingested outputs ingest_existing_file built its seed spec with the file's basename, so outputs saved into a subfolder persisted loader_path (and the user_metadata filename that preview URLs split for their subfolder param) as just the basename: the served locator pointed at a file that does not exist at that path. Scanner and seeder specs already derive fname via compute_loader_path; use the same derivation here. * fix(assets): only extension-matching buckets contribute a loader_path The model-base match in get_asset_category_and_relative_path ignored each bucket's extension set, so a file inside a registered base whose extension the bucket cannot load (e.g. a .txt uploaded into model_type:checkpoints) advertised a loader_path that no loader list would ever resolve, while the tag side of the same stack already excluded it. Apply the extension check used for backend tags (empty set accepts any extension), keeping loader_path null exactly when no loader can resolve the file. * fix(assets): refresh loader_path when re-ingesting an existing reference upsert_reference only wrote loader_path on the INSERT branch, so re-ingesting an existing reference (an output overwritten in place, or a file re-registered after its loader_path derivation changed) kept the stale or NULL value forever. Write it on the UPDATE branch too, with a null-safe change guard so a loader_path difference alone is enough to trigger the update, and identical values stay a no-op. * fix(assets): repair semantic merge breakage from #14796 and master Two textually-clean but semantically-broken merges: - routes.py lost its folder_paths import when #14796's import block superseded the base's, while the content-type hardening added via the base's master merge still calls folder_paths.is_dangerous_content_type. - master's SVG download-hardening test uploads with the pre-namespacing bare checkpoints tag, which this branch's destination validation rejects; use model_type:checkpoints. --------- Co-authored-by: guill <jacob.e.segal@gmail.com>
185 lines
6.2 KiB
Python
185 lines
6.2 KiB
Python
import logging
|
|
import os
|
|
import uuid
|
|
from typing import Callable
|
|
|
|
from aiohttp import web
|
|
|
|
import folder_paths
|
|
from app.assets.api.schemas_in import ParsedUpload, UploadError
|
|
from app.assets.helpers import validate_blake3_hash
|
|
|
|
|
|
def normalize_and_validate_hash(s: str) -> str:
|
|
"""Validate and normalize a hash string.
|
|
|
|
Returns canonical 'blake3:<hex>' or raises UploadError.
|
|
"""
|
|
try:
|
|
return validate_blake3_hash(s)
|
|
except ValueError:
|
|
raise UploadError(400, "INVALID_HASH", "hash must be like 'blake3:<hex>'")
|
|
|
|
|
|
async def parse_multipart_upload(
|
|
request: web.Request,
|
|
check_hash_exists: Callable[[str], bool],
|
|
) -> ParsedUpload:
|
|
"""
|
|
Parse a multipart/form-data upload request.
|
|
|
|
Args:
|
|
request: The aiohttp request
|
|
check_hash_exists: Callable(hash_str) -> bool to check if a hash exists
|
|
|
|
Returns:
|
|
ParsedUpload with parsed fields and temp file path
|
|
|
|
Raises:
|
|
UploadError: On validation or I/O errors
|
|
"""
|
|
if not (request.content_type or "").lower().startswith("multipart/"):
|
|
raise UploadError(
|
|
415, "UNSUPPORTED_MEDIA_TYPE", "Use multipart/form-data for uploads."
|
|
)
|
|
|
|
reader = await request.multipart()
|
|
|
|
file_present = False
|
|
file_client_name: str | None = None
|
|
tags_raw: list[str] = []
|
|
provided_name: str | None = None
|
|
user_metadata_raw: str | None = None
|
|
provided_hash: str | None = None
|
|
provided_hash_exists: bool | None = None
|
|
provided_mime_type: str | None = None
|
|
provided_preview_id: str | None = None
|
|
|
|
file_written = 0
|
|
tmp_path: str | None = None
|
|
|
|
while True:
|
|
field = await reader.next()
|
|
if field is None:
|
|
break
|
|
|
|
fname = getattr(field, "name", "") or ""
|
|
|
|
if fname == "hash":
|
|
try:
|
|
s = ((await field.text()) or "").strip().lower()
|
|
except Exception:
|
|
raise UploadError(
|
|
400, "INVALID_HASH", "hash must be like 'blake3:<hex>'"
|
|
)
|
|
|
|
if s:
|
|
provided_hash = normalize_and_validate_hash(s)
|
|
try:
|
|
provided_hash_exists = check_hash_exists(provided_hash)
|
|
except Exception as e:
|
|
logging.exception(
|
|
"check_hash_exists failed for hash=%s: %s", provided_hash, e
|
|
)
|
|
raise UploadError(
|
|
500,
|
|
"HASH_CHECK_FAILED",
|
|
"Backend error while checking asset hash.",
|
|
)
|
|
|
|
elif fname == "file":
|
|
file_present = True
|
|
file_client_name = (field.filename or "").strip()
|
|
|
|
if provided_hash and provided_hash_exists is True:
|
|
# Hash exists - drain file but don't write to disk
|
|
try:
|
|
while True:
|
|
chunk = await field.read_chunk(8 * 1024 * 1024)
|
|
if not chunk:
|
|
break
|
|
file_written += len(chunk)
|
|
except Exception:
|
|
raise UploadError(
|
|
500, "UPLOAD_IO_ERROR", "Failed to receive uploaded file."
|
|
)
|
|
continue
|
|
|
|
uploads_root = os.path.join(folder_paths.get_temp_directory(), "uploads")
|
|
unique_dir = os.path.join(uploads_root, uuid.uuid4().hex)
|
|
os.makedirs(unique_dir, exist_ok=True)
|
|
tmp_path = os.path.join(unique_dir, ".upload.part")
|
|
|
|
try:
|
|
with open(tmp_path, "wb") as f:
|
|
while True:
|
|
chunk = await field.read_chunk(8 * 1024 * 1024)
|
|
if not chunk:
|
|
break
|
|
f.write(chunk)
|
|
file_written += len(chunk)
|
|
except Exception:
|
|
delete_temp_file_if_exists(tmp_path)
|
|
raise UploadError(
|
|
500, "UPLOAD_IO_ERROR", "Failed to receive and store uploaded file."
|
|
)
|
|
|
|
elif fname == "tags":
|
|
tags_raw.append((await field.text()) or "")
|
|
elif fname == "name":
|
|
provided_name = (await field.text()) or None
|
|
elif fname == "user_metadata":
|
|
user_metadata_raw = (await field.text()) or None
|
|
elif fname == "id":
|
|
raise UploadError(
|
|
400,
|
|
"UNSUPPORTED_FIELD",
|
|
"Client-provided 'id' is not supported. Asset IDs are assigned by the server.",
|
|
)
|
|
elif fname == "mime_type":
|
|
provided_mime_type = ((await field.text()) or "").strip() or None
|
|
elif fname == "preview_id":
|
|
provided_preview_id = ((await field.text()) or "").strip() or None
|
|
if not file_present and not (provided_hash and provided_hash_exists):
|
|
raise UploadError(
|
|
400, "MISSING_FILE", "Form must include a 'file' part or a known 'hash'."
|
|
)
|
|
|
|
if (
|
|
file_present
|
|
and file_written == 0
|
|
and not (provided_hash and provided_hash_exists)
|
|
):
|
|
delete_temp_file_if_exists(tmp_path)
|
|
raise UploadError(400, "EMPTY_UPLOAD", "Uploaded file is empty.")
|
|
|
|
return ParsedUpload(
|
|
file_present=file_present,
|
|
file_written=file_written,
|
|
file_client_name=file_client_name,
|
|
tmp_path=tmp_path,
|
|
tags_raw=tags_raw,
|
|
provided_name=provided_name,
|
|
user_metadata_raw=user_metadata_raw,
|
|
provided_hash=provided_hash,
|
|
provided_hash_exists=provided_hash_exists,
|
|
provided_mime_type=provided_mime_type,
|
|
provided_preview_id=provided_preview_id,
|
|
)
|
|
|
|
|
|
def delete_temp_file_if_exists(tmp_path: str | None) -> None:
|
|
"""Safely remove a temp file and its parent directory if empty."""
|
|
if tmp_path:
|
|
try:
|
|
if os.path.exists(tmp_path):
|
|
os.remove(tmp_path)
|
|
except OSError as e:
|
|
logging.debug("Failed to delete temp file %s: %s", tmp_path, e)
|
|
try:
|
|
parent = os.path.dirname(tmp_path)
|
|
if parent and os.path.isdir(parent):
|
|
os.rmdir(parent) # only succeeds if empty
|
|
except OSError:
|
|
pass
|