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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import asyncio
|
| 2 |
+
import functools
|
| 3 |
+
import json
|
| 4 |
+
import logging
|
| 5 |
+
import os
|
| 6 |
+
import urllib.parse
|
| 7 |
+
import uuid
|
| 8 |
+
from typing import Any
|
| 9 |
+
|
| 10 |
+
from aiohttp import web
|
| 11 |
+
from pydantic import ValidationError
|
| 12 |
+
|
| 13 |
+
import folder_paths
|
| 14 |
+
from app import user_manager
|
| 15 |
+
from app.assets.api import schemas_in, schemas_out
|
| 16 |
+
from app.assets.services import schemas
|
| 17 |
+
from app.assets.api.schemas_in import (
|
| 18 |
+
AssetValidationError,
|
| 19 |
+
UploadError,
|
| 20 |
+
)
|
| 21 |
+
from app.assets.helpers import normalize_tags, validate_blake3_hash
|
| 22 |
+
from app.assets.api.upload import (
|
| 23 |
+
delete_temp_file_if_exists,
|
| 24 |
+
parse_multipart_upload,
|
| 25 |
+
)
|
| 26 |
+
from app.assets.seeder import ScanInProgressError, asset_seeder
|
| 27 |
+
from app.assets.services import (
|
| 28 |
+
DependencyMissingError,
|
| 29 |
+
HashMismatchError,
|
| 30 |
+
apply_tags,
|
| 31 |
+
asset_exists,
|
| 32 |
+
create_from_hash,
|
| 33 |
+
delete_asset_reference,
|
| 34 |
+
get_asset_detail,
|
| 35 |
+
list_assets_page,
|
| 36 |
+
list_tags,
|
| 37 |
+
remove_tags,
|
| 38 |
+
resolve_asset_for_download,
|
| 39 |
+
update_asset_metadata,
|
| 40 |
+
upload_from_temp_path,
|
| 41 |
+
)
|
| 42 |
+
from app.assets.services.cursor import InvalidCursorError
|
| 43 |
+
from app.assets.services.path_utils import compute_display_name
|
| 44 |
+
from app.assets.services.tagging import list_tag_histogram
|
| 45 |
+
|
| 46 |
+
ROUTES = web.RouteTableDef()
|
| 47 |
+
USER_MANAGER: user_manager.UserManager | None = None
|
| 48 |
+
_ASSETS_ENABLED = False
|
| 49 |
+
|
| 50 |
+
|
| 51 |
+
def _require_assets_feature_enabled(handler):
|
| 52 |
+
@functools.wraps(handler)
|
| 53 |
+
async def wrapper(request: web.Request) -> web.Response:
|
| 54 |
+
if not _ASSETS_ENABLED:
|
| 55 |
+
return _build_error_response(
|
| 56 |
+
503,
|
| 57 |
+
"SERVICE_DISABLED",
|
| 58 |
+
"Assets system is disabled. Start the server with --enable-assets to use this feature.",
|
| 59 |
+
)
|
| 60 |
+
return await handler(request)
|
| 61 |
+
|
| 62 |
+
return wrapper
|
| 63 |
+
|
| 64 |
+
|
| 65 |
+
# UUID regex (canonical hyphenated form, case-insensitive)
|
| 66 |
+
UUID_RE = r"[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}"
|
| 67 |
+
|
| 68 |
+
|
| 69 |
+
def get_query_dict(request: web.Request) -> dict[str, Any]:
|
| 70 |
+
"""Gets a dictionary of query parameters from the request.
|
| 71 |
+
|
| 72 |
+
request.query is a MultiMapping[str], needs to be converted to a dict
|
| 73 |
+
to be validated by Pydantic.
|
| 74 |
+
"""
|
| 75 |
+
query_dict = {
|
| 76 |
+
key: request.query.getall(key)
|
| 77 |
+
if len(request.query.getall(key)) > 1
|
| 78 |
+
else request.query.get(key)
|
| 79 |
+
for key in request.query.keys()
|
| 80 |
+
}
|
| 81 |
+
return query_dict
|
| 82 |
+
|
| 83 |
+
|
| 84 |
+
# Note to any custom node developers reading this code:
|
| 85 |
+
# The assets system is not yet fully implemented,
|
| 86 |
+
# do not rely on the code in /app/assets remaining the same.
|
| 87 |
+
|
| 88 |
+
|
| 89 |
+
def register_assets_routes(
|
| 90 |
+
app: web.Application,
|
| 91 |
+
user_manager_instance: user_manager.UserManager | None = None,
|
| 92 |
+
) -> None:
|
| 93 |
+
global USER_MANAGER, _ASSETS_ENABLED
|
| 94 |
+
if user_manager_instance is not None:
|
| 95 |
+
USER_MANAGER = user_manager_instance
|
| 96 |
+
_ASSETS_ENABLED = True
|
| 97 |
+
app.add_routes(ROUTES)
|
| 98 |
+
|
| 99 |
+
|
| 100 |
+
def disable_assets_routes() -> None:
|
| 101 |
+
"""Disable asset routes at runtime (e.g. after DB init failure)."""
|
| 102 |
+
global _ASSETS_ENABLED
|
| 103 |
+
_ASSETS_ENABLED = False
|
| 104 |
+
|
| 105 |
+
|
| 106 |
+
def _build_error_response(
|
| 107 |
+
status: int, code: str, message: str, details: dict | None = None
|
| 108 |
+
) -> web.Response:
|
| 109 |
+
return web.json_response(
|
| 110 |
+
{"error": {"code": code, "message": message, "details": details or {}}},
|
| 111 |
+
status=status,
|
| 112 |
+
)
|
| 113 |
+
|
| 114 |
+
|
| 115 |
+
def _build_validation_error_response(code: str, ve: ValidationError) -> web.Response:
|
| 116 |
+
errors = json.loads(ve.json())
|
| 117 |
+
return _build_error_response(400, code, "Validation failed.", {"errors": errors})
|
| 118 |
+
|
| 119 |
+
|
| 120 |
+
class InvalidTagFilterError(Exception):
|
| 121 |
+
"""Invalid combination of tag-filter query parameters."""
|
| 122 |
+
|
| 123 |
+
def __init__(self, message: str, details: dict):
|
| 124 |
+
super().__init__(message)
|
| 125 |
+
self.details = details
|
| 126 |
+
|
| 127 |
+
|
| 128 |
+
# Caps the per-tag EXISTS fan-out; deliberately covers the legacy spellings too.
|
| 129 |
+
MAX_TAG_FILTER_TAGS = 100
|
| 130 |
+
|
| 131 |
+
|
| 132 |
+
def _resolve_tag_filters(
|
| 133 |
+
q: schemas_in.ListAssetsQuery | schemas_in.TagsRefineQuery,
|
| 134 |
+
) -> tuple[list[str], list[str], list[str]]:
|
| 135 |
+
"""Resolve legacy (include/exclude) and new (all/any/none) tag-filter
|
| 136 |
+
spellings into effective (all, any, none) lists.
|
| 137 |
+
|
| 138 |
+
Combination validation applies only when the request uses at least one
|
| 139 |
+
new-name parameter (non-empty after normalisation); requests using only
|
| 140 |
+
the legacy names keep their historical behaviour, including degenerate
|
| 141 |
+
combinations like include_tags=a&exclude_tags=a.
|
| 142 |
+
"""
|
| 143 |
+
# model_dump, not attribute access: deprecated fields warn on every attribute read.
|
| 144 |
+
legacy = q.model_dump(include={"include_tags", "exclude_tags"})
|
| 145 |
+
include_tags = normalize_tags(legacy["include_tags"])
|
| 146 |
+
exclude_tags = normalize_tags(legacy["exclude_tags"])
|
| 147 |
+
tags_all = normalize_tags(q.tags_all)
|
| 148 |
+
tags_any = normalize_tags(q.tags_any)
|
| 149 |
+
tags_none = normalize_tags(q.tags_none)
|
| 150 |
+
|
| 151 |
+
for param_name, values in (
|
| 152 |
+
("include_tags", include_tags),
|
| 153 |
+
("exclude_tags", exclude_tags),
|
| 154 |
+
("tags_all", tags_all),
|
| 155 |
+
("tags_any", tags_any),
|
| 156 |
+
("tags_none", tags_none),
|
| 157 |
+
):
|
| 158 |
+
if len(values) > MAX_TAG_FILTER_TAGS:
|
| 159 |
+
raise InvalidTagFilterError(
|
| 160 |
+
f"'{param_name}' lists {len(values)} tags; the maximum is "
|
| 161 |
+
f"{MAX_TAG_FILTER_TAGS}.",
|
| 162 |
+
{
|
| 163 |
+
"parameter": param_name,
|
| 164 |
+
"count": len(values),
|
| 165 |
+
"max": MAX_TAG_FILTER_TAGS,
|
| 166 |
+
},
|
| 167 |
+
)
|
| 168 |
+
|
| 169 |
+
if not (tags_all or tags_any or tags_none):
|
| 170 |
+
return include_tags, [], exclude_tags
|
| 171 |
+
|
| 172 |
+
if include_tags and tags_all:
|
| 173 |
+
raise InvalidTagFilterError(
|
| 174 |
+
"Cannot combine 'include_tags' with 'tags_all'; use 'tags_all'.",
|
| 175 |
+
{"parameters": ["include_tags", "tags_all"]},
|
| 176 |
+
)
|
| 177 |
+
if exclude_tags and tags_none:
|
| 178 |
+
raise InvalidTagFilterError(
|
| 179 |
+
"Cannot combine 'exclude_tags' with 'tags_none'; use 'tags_none'.",
|
| 180 |
+
{"parameters": ["exclude_tags", "tags_none"]},
|
| 181 |
+
)
|
| 182 |
+
|
| 183 |
+
all_param, all_list = (
|
| 184 |
+
("tags_all", tags_all) if tags_all else ("include_tags", include_tags)
|
| 185 |
+
)
|
| 186 |
+
none_param, none_list = (
|
| 187 |
+
("tags_none", tags_none) if tags_none else ("exclude_tags", exclude_tags)
|
| 188 |
+
)
|
| 189 |
+
|
| 190 |
+
conflicting = sorted(set(all_list) & set(none_list))
|
| 191 |
+
if conflicting:
|
| 192 |
+
raise InvalidTagFilterError(
|
| 193 |
+
f"Query can never match: {', '.join(repr(t) for t in conflicting)} "
|
| 194 |
+
f"required by '{all_param}' but rejected by '{none_param}'.",
|
| 195 |
+
{"conflicting_tags": conflicting, "parameters": [all_param, none_param]},
|
| 196 |
+
)
|
| 197 |
+
|
| 198 |
+
return all_list, tags_any, none_list
|
| 199 |
+
|
| 200 |
+
|
| 201 |
+
def _validate_sort_field(requested: str | None) -> str:
|
| 202 |
+
if not requested:
|
| 203 |
+
return "created_at"
|
| 204 |
+
v = requested.lower()
|
| 205 |
+
if v in {"name", "created_at", "updated_at", "size", "last_access_time"}:
|
| 206 |
+
return v
|
| 207 |
+
return "created_at"
|
| 208 |
+
|
| 209 |
+
|
| 210 |
+
def _build_preview_url_from_view(tags: list[str], user_metadata: dict[str, Any] | None) -> str | None:
|
| 211 |
+
"""Build a /api/view preview URL from asset tags and user_metadata filename."""
|
| 212 |
+
if not user_metadata:
|
| 213 |
+
return None
|
| 214 |
+
filename = user_metadata.get("filename")
|
| 215 |
+
if not filename:
|
| 216 |
+
return None
|
| 217 |
+
|
| 218 |
+
if "input" in tags:
|
| 219 |
+
view_type = "input"
|
| 220 |
+
elif "output" in tags:
|
| 221 |
+
view_type = "output"
|
| 222 |
+
else:
|
| 223 |
+
return None
|
| 224 |
+
|
| 225 |
+
subfolder = ""
|
| 226 |
+
if "/" in filename:
|
| 227 |
+
subfolder, filename = filename.rsplit("/", 1)
|
| 228 |
+
|
| 229 |
+
encoded_filename = urllib.parse.quote(filename, safe="")
|
| 230 |
+
url = f"/api/view?type={view_type}&filename={encoded_filename}"
|
| 231 |
+
if subfolder:
|
| 232 |
+
url += f"&subfolder={urllib.parse.quote(subfolder, safe='')}"
|
| 233 |
+
return url
|
| 234 |
+
|
| 235 |
+
|
| 236 |
+
def _build_asset_response(result: schemas.AssetDetailResult | schemas.UploadResult) -> schemas_out.Asset:
|
| 237 |
+
"""Build an Asset response from a service result."""
|
| 238 |
+
if result.ref.preview_id:
|
| 239 |
+
preview_detail = get_asset_detail(result.ref.preview_id)
|
| 240 |
+
if preview_detail:
|
| 241 |
+
preview_url = _build_preview_url_from_view(preview_detail.tags, preview_detail.ref.user_metadata)
|
| 242 |
+
else:
|
| 243 |
+
preview_url = None
|
| 244 |
+
else:
|
| 245 |
+
preview_url = _build_preview_url_from_view(result.tags, result.ref.user_metadata)
|
| 246 |
+
if result.ref.file_path:
|
| 247 |
+
display_name = compute_display_name(result.ref.file_path)
|
| 248 |
+
# In-root loader path (model category dropped): what model loaders consume.
|
| 249 |
+
loader_path = result.ref.loader_path
|
| 250 |
+
else:
|
| 251 |
+
display_name, loader_path = None, None
|
| 252 |
+
asset_content_hash = result.asset.hash if result.asset else None
|
| 253 |
+
return schemas_out.Asset(
|
| 254 |
+
id=result.ref.id,
|
| 255 |
+
name=result.ref.name,
|
| 256 |
+
hash=asset_content_hash,
|
| 257 |
+
loader_path=loader_path,
|
| 258 |
+
display_name=display_name,
|
| 259 |
+
asset_hash=asset_content_hash,
|
| 260 |
+
size=int(result.asset.size_bytes) if result.asset else None,
|
| 261 |
+
mime_type=result.asset.mime_type if result.asset else None,
|
| 262 |
+
tags=result.tags,
|
| 263 |
+
preview_url=preview_url,
|
| 264 |
+
preview_id=result.ref.preview_id,
|
| 265 |
+
user_metadata=result.ref.user_metadata or {},
|
| 266 |
+
metadata=result.ref.system_metadata,
|
| 267 |
+
job_id=result.ref.job_id,
|
| 268 |
+
prompt_id=result.ref.job_id, # deprecated alias of job_id, kept for compatibility
|
| 269 |
+
created_at=result.ref.created_at,
|
| 270 |
+
updated_at=result.ref.updated_at,
|
| 271 |
+
last_access_time=result.ref.last_access_time,
|
| 272 |
+
)
|
| 273 |
+
|
| 274 |
+
|
| 275 |
+
@ROUTES.head("/api/assets/hash/{hash}")
|
| 276 |
+
@_require_assets_feature_enabled
|
| 277 |
+
async def head_asset_by_hash(request: web.Request) -> web.Response:
|
| 278 |
+
hash_str = request.match_info.get("hash", "").strip().lower()
|
| 279 |
+
try:
|
| 280 |
+
hash_str = validate_blake3_hash(hash_str)
|
| 281 |
+
except ValueError:
|
| 282 |
+
return _build_error_response(
|
| 283 |
+
400, "INVALID_HASH", "hash must be like 'blake3:<hex>'"
|
| 284 |
+
)
|
| 285 |
+
exists = asset_exists(hash_str)
|
| 286 |
+
return web.Response(status=200 if exists else 404)
|
| 287 |
+
|
| 288 |
+
|
| 289 |
+
@ROUTES.get("/api/assets")
|
| 290 |
+
@_require_assets_feature_enabled
|
| 291 |
+
async def list_assets_route(request: web.Request) -> web.Response:
|
| 292 |
+
"""
|
| 293 |
+
GET request to list assets.
|
| 294 |
+
"""
|
| 295 |
+
query_dict = get_query_dict(request)
|
| 296 |
+
try:
|
| 297 |
+
q = schemas_in.ListAssetsQuery.model_validate(query_dict)
|
| 298 |
+
except ValidationError as ve:
|
| 299 |
+
return _build_validation_error_response("INVALID_QUERY", ve)
|
| 300 |
+
|
| 301 |
+
try:
|
| 302 |
+
tags_all, tags_any, tags_none = _resolve_tag_filters(q)
|
| 303 |
+
except InvalidTagFilterError as e:
|
| 304 |
+
return _build_error_response(400, "INVALID_TAG_FILTER", str(e), e.details)
|
| 305 |
+
|
| 306 |
+
sort = _validate_sort_field(q.sort)
|
| 307 |
+
order_candidate = (q.order or "desc").lower()
|
| 308 |
+
order = order_candidate if order_candidate in {"asc", "desc"} else "desc"
|
| 309 |
+
|
| 310 |
+
try:
|
| 311 |
+
result = list_assets_page(
|
| 312 |
+
owner_id=USER_MANAGER.get_request_user_id(request),
|
| 313 |
+
include_tags=tags_all,
|
| 314 |
+
exclude_tags=tags_none,
|
| 315 |
+
any_tags=tags_any,
|
| 316 |
+
name_contains=q.name_contains,
|
| 317 |
+
metadata_filter=q.metadata_filter,
|
| 318 |
+
limit=q.limit,
|
| 319 |
+
offset=q.offset,
|
| 320 |
+
sort=sort,
|
| 321 |
+
order=order,
|
| 322 |
+
after=q.after,
|
| 323 |
+
)
|
| 324 |
+
except InvalidCursorError as e:
|
| 325 |
+
return _build_error_response(400, "INVALID_CURSOR", str(e))
|
| 326 |
+
|
| 327 |
+
summaries = [_build_asset_response(item) for item in result.items]
|
| 328 |
+
|
| 329 |
+
# has_more semantics differ by mode:
|
| 330 |
+
# - cursor mode: a non-empty next_cursor means there are more results.
|
| 331 |
+
# - offset mode: derived from total - (offset + page size).
|
| 332 |
+
if q.after is not None:
|
| 333 |
+
has_more = result.next_cursor is not None
|
| 334 |
+
else:
|
| 335 |
+
has_more = (q.offset + len(summaries)) < result.total
|
| 336 |
+
|
| 337 |
+
payload = schemas_out.AssetsList(
|
| 338 |
+
assets=summaries,
|
| 339 |
+
total=result.total,
|
| 340 |
+
has_more=has_more,
|
| 341 |
+
next_cursor=result.next_cursor,
|
| 342 |
+
)
|
| 343 |
+
return web.json_response(payload.model_dump(mode="json", exclude_none=True))
|
| 344 |
+
|
| 345 |
+
|
| 346 |
+
@ROUTES.get(f"/api/assets/{{id:{UUID_RE}}}")
|
| 347 |
+
@_require_assets_feature_enabled
|
| 348 |
+
async def get_asset_route(request: web.Request) -> web.Response:
|
| 349 |
+
"""
|
| 350 |
+
GET request to get an asset's info as JSON.
|
| 351 |
+
"""
|
| 352 |
+
reference_id = str(uuid.UUID(request.match_info["id"]))
|
| 353 |
+
try:
|
| 354 |
+
result = get_asset_detail(
|
| 355 |
+
reference_id=reference_id,
|
| 356 |
+
owner_id=USER_MANAGER.get_request_user_id(request),
|
| 357 |
+
)
|
| 358 |
+
if not result:
|
| 359 |
+
return _build_error_response(
|
| 360 |
+
404,
|
| 361 |
+
"ASSET_NOT_FOUND",
|
| 362 |
+
f"AssetReference {reference_id} not found",
|
| 363 |
+
{"id": reference_id},
|
| 364 |
+
)
|
| 365 |
+
|
| 366 |
+
payload = _build_asset_response(result)
|
| 367 |
+
except ValueError as e:
|
| 368 |
+
return _build_error_response(
|
| 369 |
+
404, "ASSET_NOT_FOUND", str(e), {"id": reference_id}
|
| 370 |
+
)
|
| 371 |
+
except Exception:
|
| 372 |
+
logging.exception(
|
| 373 |
+
"get_asset failed for reference_id=%s, owner_id=%s",
|
| 374 |
+
reference_id,
|
| 375 |
+
USER_MANAGER.get_request_user_id(request),
|
| 376 |
+
)
|
| 377 |
+
return _build_error_response(500, "INTERNAL", "Unexpected server error.")
|
| 378 |
+
return web.json_response(payload.model_dump(mode="json", exclude_none=True), status=200)
|
| 379 |
+
|
| 380 |
+
|
| 381 |
+
@ROUTES.get(f"/api/assets/{{id:{UUID_RE}}}/content")
|
| 382 |
+
@_require_assets_feature_enabled
|
| 383 |
+
async def download_asset_content(request: web.Request) -> web.Response:
|
| 384 |
+
disposition = request.query.get("disposition", "attachment").lower().strip()
|
| 385 |
+
if disposition not in {"inline", "attachment"}:
|
| 386 |
+
disposition = "attachment"
|
| 387 |
+
|
| 388 |
+
try:
|
| 389 |
+
result = resolve_asset_for_download(
|
| 390 |
+
reference_id=str(uuid.UUID(request.match_info["id"])),
|
| 391 |
+
owner_id=USER_MANAGER.get_request_user_id(request),
|
| 392 |
+
)
|
| 393 |
+
abs_path = result.abs_path
|
| 394 |
+
content_type = result.content_type
|
| 395 |
+
filename = result.download_name
|
| 396 |
+
except ValueError as ve:
|
| 397 |
+
return _build_error_response(404, "ASSET_NOT_FOUND", str(ve))
|
| 398 |
+
except NotImplementedError as nie:
|
| 399 |
+
return _build_error_response(501, "BACKEND_UNSUPPORTED", str(nie))
|
| 400 |
+
except FileNotFoundError:
|
| 401 |
+
return _build_error_response(
|
| 402 |
+
404, "FILE_NOT_FOUND", "Underlying file not found on disk."
|
| 403 |
+
)
|
| 404 |
+
|
| 405 |
+
# User-controlled asset content must not render inline in the app origin
|
| 406 |
+
# (stored XSS via SVG/HTML/XML). Force dangerous types to download and
|
| 407 |
+
# override any requested inline disposition; SVG loaded into an <img> is
|
| 408 |
+
# exempt, see renders_safely_as_image. Centralised through folder_paths so
|
| 409 |
+
# this can't drift from /view and /userdata (the previous inline set here
|
| 410 |
+
# omitted image/svg+xml and missed the charset/casing/+xml-dialect bypasses).
|
| 411 |
+
extra_headers = {}
|
| 412 |
+
sec_fetch_dest = request.headers.get("Sec-Fetch-Dest")
|
| 413 |
+
if folder_paths.is_dangerous_content_type(content_type):
|
| 414 |
+
# This response now depends on a request header, so it must not be
|
| 415 |
+
# reused across destinations by a browser or intermediary cache: an
|
| 416 |
+
# inline SVG primed by an <img> fetch and replayed to a document
|
| 417 |
+
# navigation of the same URL would re-enable the stored XSS.
|
| 418 |
+
extra_headers["Vary"] = "Sec-Fetch-Dest"
|
| 419 |
+
extra_headers["Cache-Control"] = "no-store"
|
| 420 |
+
if not folder_paths.renders_safely_as_image(content_type, sec_fetch_dest):
|
| 421 |
+
content_type = "application/octet-stream"
|
| 422 |
+
disposition = "attachment"
|
| 423 |
+
|
| 424 |
+
# mime_type is uploader-supplied and unvalidated, so it can carry
|
| 425 |
+
# parameters. aiohttp rejects a charset in the content_type argument with
|
| 426 |
+
# ValueError, which would turn a valid inline SVG into a 500.
|
| 427 |
+
content_type = content_type.split(";", 1)[0].strip() or "application/octet-stream"
|
| 428 |
+
|
| 429 |
+
safe_name = (filename or "").replace("\r", "").replace("\n", "")
|
| 430 |
+
encoded = urllib.parse.quote(safe_name)
|
| 431 |
+
cd = f"{disposition}; filename*=UTF-8''{encoded}"
|
| 432 |
+
|
| 433 |
+
file_size = os.path.getsize(abs_path)
|
| 434 |
+
size_mb = file_size / (1024 * 1024)
|
| 435 |
+
logging.info(
|
| 436 |
+
"download_asset_content: path=%s, size=%d bytes (%.2f MB), type=%s, name=%s",
|
| 437 |
+
abs_path,
|
| 438 |
+
file_size,
|
| 439 |
+
size_mb,
|
| 440 |
+
content_type,
|
| 441 |
+
filename,
|
| 442 |
+
)
|
| 443 |
+
|
| 444 |
+
async def stream_file_chunks():
|
| 445 |
+
chunk_size = 64 * 1024
|
| 446 |
+
with open(abs_path, "rb") as f:
|
| 447 |
+
while True:
|
| 448 |
+
chunk = f.read(chunk_size)
|
| 449 |
+
if not chunk:
|
| 450 |
+
break
|
| 451 |
+
yield chunk
|
| 452 |
+
|
| 453 |
+
return web.Response(
|
| 454 |
+
body=stream_file_chunks(),
|
| 455 |
+
content_type=content_type,
|
| 456 |
+
headers={
|
| 457 |
+
"Content-Disposition": cd,
|
| 458 |
+
"Content-Length": str(file_size),
|
| 459 |
+
"X-Content-Type-Options": "nosniff",
|
| 460 |
+
**extra_headers,
|
| 461 |
+
},
|
| 462 |
+
)
|
| 463 |
+
|
| 464 |
+
|
| 465 |
+
@ROUTES.post("/api/assets/from-hash")
|
| 466 |
+
@_require_assets_feature_enabled
|
| 467 |
+
async def create_asset_from_hash_route(request: web.Request) -> web.Response:
|
| 468 |
+
try:
|
| 469 |
+
payload = await request.json()
|
| 470 |
+
body = schemas_in.CreateFromHashBody.model_validate(payload)
|
| 471 |
+
except ValidationError as ve:
|
| 472 |
+
return _build_validation_error_response("INVALID_BODY", ve)
|
| 473 |
+
except Exception:
|
| 474 |
+
return _build_error_response(
|
| 475 |
+
400, "INVALID_JSON", "Request body must be valid JSON."
|
| 476 |
+
)
|
| 477 |
+
|
| 478 |
+
# Derive name from hash if not provided
|
| 479 |
+
name = body.name
|
| 480 |
+
if name is None:
|
| 481 |
+
name = body.hash.split(":", 1)[1] if ":" in body.hash else body.hash
|
| 482 |
+
|
| 483 |
+
result = create_from_hash(
|
| 484 |
+
hash_str=body.hash,
|
| 485 |
+
name=name,
|
| 486 |
+
tags=body.tags,
|
| 487 |
+
user_metadata=body.user_metadata,
|
| 488 |
+
owner_id=USER_MANAGER.get_request_user_id(request),
|
| 489 |
+
mime_type=body.mime_type,
|
| 490 |
+
preview_id=body.preview_id,
|
| 491 |
+
)
|
| 492 |
+
if result is None:
|
| 493 |
+
return _build_error_response(
|
| 494 |
+
404, "ASSET_NOT_FOUND", f"Asset content {body.hash} does not exist"
|
| 495 |
+
)
|
| 496 |
+
|
| 497 |
+
asset = _build_asset_response(result)
|
| 498 |
+
payload_out = schemas_out.AssetCreated(
|
| 499 |
+
**asset.model_dump(),
|
| 500 |
+
created_new=result.created_new,
|
| 501 |
+
)
|
| 502 |
+
return web.json_response(payload_out.model_dump(mode="json", exclude_none=True), status=201)
|
| 503 |
+
|
| 504 |
+
|
| 505 |
+
@ROUTES.post("/api/assets")
|
| 506 |
+
@_require_assets_feature_enabled
|
| 507 |
+
async def upload_asset(request: web.Request) -> web.Response:
|
| 508 |
+
"""Multipart/form-data endpoint for Asset uploads."""
|
| 509 |
+
try:
|
| 510 |
+
parsed = await parse_multipart_upload(request, check_hash_exists=asset_exists)
|
| 511 |
+
except UploadError as e:
|
| 512 |
+
return _build_error_response(e.status, e.code, e.message)
|
| 513 |
+
|
| 514 |
+
owner_id = USER_MANAGER.get_request_user_id(request)
|
| 515 |
+
|
| 516 |
+
try:
|
| 517 |
+
spec = schemas_in.UploadAssetSpec.model_validate(
|
| 518 |
+
{
|
| 519 |
+
"tags": parsed.tags_raw,
|
| 520 |
+
"name": parsed.provided_name,
|
| 521 |
+
"user_metadata": parsed.user_metadata_raw,
|
| 522 |
+
"hash": parsed.provided_hash,
|
| 523 |
+
"mime_type": parsed.provided_mime_type,
|
| 524 |
+
"preview_id": parsed.provided_preview_id,
|
| 525 |
+
}
|
| 526 |
+
)
|
| 527 |
+
except ValidationError as ve:
|
| 528 |
+
delete_temp_file_if_exists(parsed.tmp_path)
|
| 529 |
+
return _build_error_response(
|
| 530 |
+
400, "INVALID_BODY", f"Validation failed: {ve.json()}"
|
| 531 |
+
)
|
| 532 |
+
|
| 533 |
+
try:
|
| 534 |
+
# Fast path: hash exists, create AssetReference without writing anything
|
| 535 |
+
if spec.hash and parsed.provided_hash_exists is True:
|
| 536 |
+
result = create_from_hash(
|
| 537 |
+
hash_str=spec.hash,
|
| 538 |
+
name=spec.name or (spec.hash.split(":", 1)[1]),
|
| 539 |
+
tags=spec.tags,
|
| 540 |
+
user_metadata=spec.user_metadata or {},
|
| 541 |
+
owner_id=owner_id,
|
| 542 |
+
mime_type=spec.mime_type,
|
| 543 |
+
preview_id=spec.preview_id,
|
| 544 |
+
)
|
| 545 |
+
if result is None:
|
| 546 |
+
delete_temp_file_if_exists(parsed.tmp_path)
|
| 547 |
+
return _build_error_response(
|
| 548 |
+
404, "ASSET_NOT_FOUND", f"Asset content {spec.hash} does not exist"
|
| 549 |
+
)
|
| 550 |
+
delete_temp_file_if_exists(parsed.tmp_path)
|
| 551 |
+
else:
|
| 552 |
+
# Otherwise, we must have a temp file path to ingest
|
| 553 |
+
if not parsed.tmp_path or not os.path.exists(parsed.tmp_path):
|
| 554 |
+
return _build_error_response(
|
| 555 |
+
400,
|
| 556 |
+
"MISSING_INPUT",
|
| 557 |
+
"Provided hash not found and no file uploaded.",
|
| 558 |
+
)
|
| 559 |
+
|
| 560 |
+
result = upload_from_temp_path(
|
| 561 |
+
temp_path=parsed.tmp_path,
|
| 562 |
+
name=spec.name,
|
| 563 |
+
tags=spec.tags,
|
| 564 |
+
user_metadata=spec.user_metadata or {},
|
| 565 |
+
client_filename=parsed.file_client_name,
|
| 566 |
+
owner_id=owner_id,
|
| 567 |
+
expected_hash=spec.hash,
|
| 568 |
+
mime_type=spec.mime_type,
|
| 569 |
+
preview_id=spec.preview_id,
|
| 570 |
+
)
|
| 571 |
+
except AssetValidationError as e:
|
| 572 |
+
delete_temp_file_if_exists(parsed.tmp_path)
|
| 573 |
+
return _build_error_response(400, e.code, str(e))
|
| 574 |
+
except ValueError as e:
|
| 575 |
+
delete_temp_file_if_exists(parsed.tmp_path)
|
| 576 |
+
return _build_error_response(400, "INVALID_BODY", str(e))
|
| 577 |
+
except HashMismatchError as e:
|
| 578 |
+
delete_temp_file_if_exists(parsed.tmp_path)
|
| 579 |
+
return _build_error_response(400, "HASH_MISMATCH", str(e))
|
| 580 |
+
except DependencyMissingError as e:
|
| 581 |
+
delete_temp_file_if_exists(parsed.tmp_path)
|
| 582 |
+
return _build_error_response(503, "DEPENDENCY_MISSING", e.message)
|
| 583 |
+
except Exception:
|
| 584 |
+
delete_temp_file_if_exists(parsed.tmp_path)
|
| 585 |
+
logging.exception("upload_asset failed for owner_id=%s", owner_id)
|
| 586 |
+
return _build_error_response(500, "INTERNAL", "Unexpected server error.")
|
| 587 |
+
|
| 588 |
+
asset = _build_asset_response(result)
|
| 589 |
+
payload_out = schemas_out.AssetCreated(
|
| 590 |
+
**asset.model_dump(),
|
| 591 |
+
created_new=result.created_new,
|
| 592 |
+
)
|
| 593 |
+
status = 201 if result.created_new else 200
|
| 594 |
+
return web.json_response(payload_out.model_dump(mode="json", exclude_none=True), status=status)
|
| 595 |
+
|
| 596 |
+
|
| 597 |
+
@ROUTES.put(f"/api/assets/{{id:{UUID_RE}}}")
|
| 598 |
+
@_require_assets_feature_enabled
|
| 599 |
+
async def update_asset_route(request: web.Request) -> web.Response:
|
| 600 |
+
reference_id = str(uuid.UUID(request.match_info["id"]))
|
| 601 |
+
try:
|
| 602 |
+
body = schemas_in.UpdateAssetBody.model_validate(await request.json())
|
| 603 |
+
except ValidationError as ve:
|
| 604 |
+
return _build_validation_error_response("INVALID_BODY", ve)
|
| 605 |
+
except Exception:
|
| 606 |
+
return _build_error_response(
|
| 607 |
+
400, "INVALID_JSON", "Request body must be valid JSON."
|
| 608 |
+
)
|
| 609 |
+
|
| 610 |
+
try:
|
| 611 |
+
result = update_asset_metadata(
|
| 612 |
+
reference_id=reference_id,
|
| 613 |
+
name=body.name,
|
| 614 |
+
user_metadata=body.user_metadata,
|
| 615 |
+
owner_id=USER_MANAGER.get_request_user_id(request),
|
| 616 |
+
preview_id=body.preview_id,
|
| 617 |
+
)
|
| 618 |
+
payload = _build_asset_response(result)
|
| 619 |
+
except PermissionError as pe:
|
| 620 |
+
return _build_error_response(403, "FORBIDDEN", str(pe), {"id": reference_id})
|
| 621 |
+
except ValueError as ve:
|
| 622 |
+
return _build_error_response(
|
| 623 |
+
404, "ASSET_NOT_FOUND", str(ve), {"id": reference_id}
|
| 624 |
+
)
|
| 625 |
+
except Exception:
|
| 626 |
+
logging.exception(
|
| 627 |
+
"update_asset failed for reference_id=%s, owner_id=%s",
|
| 628 |
+
reference_id,
|
| 629 |
+
USER_MANAGER.get_request_user_id(request),
|
| 630 |
+
)
|
| 631 |
+
return _build_error_response(500, "INTERNAL", "Unexpected server error.")
|
| 632 |
+
return web.json_response(payload.model_dump(mode="json", exclude_none=True), status=200)
|
| 633 |
+
|
| 634 |
+
|
| 635 |
+
@ROUTES.delete(f"/api/assets/{{id:{UUID_RE}}}")
|
| 636 |
+
@_require_assets_feature_enabled
|
| 637 |
+
async def delete_asset_route(request: web.Request) -> web.Response:
|
| 638 |
+
reference_id = str(uuid.UUID(request.match_info["id"]))
|
| 639 |
+
|
| 640 |
+
try:
|
| 641 |
+
# Deleting an asset is a soft delete of the reference; the underlying
|
| 642 |
+
# content is preserved (it may be shared with other references).
|
| 643 |
+
deleted = delete_asset_reference(
|
| 644 |
+
reference_id=reference_id,
|
| 645 |
+
owner_id=USER_MANAGER.get_request_user_id(request),
|
| 646 |
+
delete_content_if_orphan=False,
|
| 647 |
+
)
|
| 648 |
+
except Exception:
|
| 649 |
+
logging.exception(
|
| 650 |
+
"delete_asset_reference failed for reference_id=%s, owner_id=%s",
|
| 651 |
+
reference_id,
|
| 652 |
+
USER_MANAGER.get_request_user_id(request),
|
| 653 |
+
)
|
| 654 |
+
return _build_error_response(500, "INTERNAL", "Unexpected server error.")
|
| 655 |
+
|
| 656 |
+
if not deleted:
|
| 657 |
+
return _build_error_response(
|
| 658 |
+
404, "ASSET_NOT_FOUND", f"AssetReference {reference_id} not found."
|
| 659 |
+
)
|
| 660 |
+
return web.Response(status=204)
|
| 661 |
+
|
| 662 |
+
|
| 663 |
+
@ROUTES.get("/api/tags")
|
| 664 |
+
@_require_assets_feature_enabled
|
| 665 |
+
async def get_tags(request: web.Request) -> web.Response:
|
| 666 |
+
"""
|
| 667 |
+
GET request to list all tags based on query parameters.
|
| 668 |
+
"""
|
| 669 |
+
query_map = dict(request.rel_url.query)
|
| 670 |
+
|
| 671 |
+
try:
|
| 672 |
+
query = schemas_in.TagsListQuery.model_validate(query_map)
|
| 673 |
+
except ValidationError as e:
|
| 674 |
+
return _build_error_response(
|
| 675 |
+
400,
|
| 676 |
+
"INVALID_QUERY",
|
| 677 |
+
"Invalid query parameters",
|
| 678 |
+
{"errors": json.loads(e.json())},
|
| 679 |
+
)
|
| 680 |
+
|
| 681 |
+
rows, total = list_tags(
|
| 682 |
+
prefix=query.prefix,
|
| 683 |
+
limit=query.limit,
|
| 684 |
+
offset=query.offset,
|
| 685 |
+
order=query.order,
|
| 686 |
+
include_zero=query.include_zero,
|
| 687 |
+
owner_id=USER_MANAGER.get_request_user_id(request),
|
| 688 |
+
)
|
| 689 |
+
|
| 690 |
+
tags = [
|
| 691 |
+
schemas_out.TagUsage(name=name, count=count)
|
| 692 |
+
for (name, count) in rows
|
| 693 |
+
]
|
| 694 |
+
payload = schemas_out.TagsList(
|
| 695 |
+
tags=tags, total=total, has_more=(query.offset + len(tags)) < total
|
| 696 |
+
)
|
| 697 |
+
return web.json_response(payload.model_dump(mode="json", exclude_none=True))
|
| 698 |
+
|
| 699 |
+
|
| 700 |
+
@ROUTES.post(f"/api/assets/{{id:{UUID_RE}}}/tags")
|
| 701 |
+
@_require_assets_feature_enabled
|
| 702 |
+
async def add_asset_tags(request: web.Request) -> web.Response:
|
| 703 |
+
reference_id = str(uuid.UUID(request.match_info["id"]))
|
| 704 |
+
try:
|
| 705 |
+
json_payload = await request.json()
|
| 706 |
+
data = schemas_in.TagsAdd.model_validate(json_payload)
|
| 707 |
+
except ValidationError as ve:
|
| 708 |
+
return _build_error_response(
|
| 709 |
+
400,
|
| 710 |
+
"INVALID_BODY",
|
| 711 |
+
"Invalid JSON body for tags add.",
|
| 712 |
+
{"errors": ve.errors()},
|
| 713 |
+
)
|
| 714 |
+
except Exception:
|
| 715 |
+
return _build_error_response(
|
| 716 |
+
400, "INVALID_JSON", "Request body must be valid JSON."
|
| 717 |
+
)
|
| 718 |
+
|
| 719 |
+
try:
|
| 720 |
+
result = apply_tags(
|
| 721 |
+
reference_id=reference_id,
|
| 722 |
+
tags=data.tags,
|
| 723 |
+
origin="manual",
|
| 724 |
+
owner_id=USER_MANAGER.get_request_user_id(request),
|
| 725 |
+
)
|
| 726 |
+
payload = schemas_out.TagsAdd(
|
| 727 |
+
added=result.added,
|
| 728 |
+
already_present=result.already_present,
|
| 729 |
+
total_tags=result.total_tags,
|
| 730 |
+
)
|
| 731 |
+
except PermissionError as pe:
|
| 732 |
+
return _build_error_response(403, "FORBIDDEN", str(pe), {"id": reference_id})
|
| 733 |
+
except ValueError as ve:
|
| 734 |
+
return _build_error_response(
|
| 735 |
+
404, "ASSET_NOT_FOUND", str(ve), {"id": reference_id}
|
| 736 |
+
)
|
| 737 |
+
except Exception:
|
| 738 |
+
logging.exception(
|
| 739 |
+
"add_tags_to_asset failed for reference_id=%s, owner_id=%s",
|
| 740 |
+
reference_id,
|
| 741 |
+
USER_MANAGER.get_request_user_id(request),
|
| 742 |
+
)
|
| 743 |
+
return _build_error_response(500, "INTERNAL", "Unexpected server error.")
|
| 744 |
+
|
| 745 |
+
return web.json_response(payload.model_dump(mode="json", exclude_none=True), status=200)
|
| 746 |
+
|
| 747 |
+
|
| 748 |
+
@ROUTES.delete(f"/api/assets/{{id:{UUID_RE}}}/tags")
|
| 749 |
+
@_require_assets_feature_enabled
|
| 750 |
+
async def delete_asset_tags(request: web.Request) -> web.Response:
|
| 751 |
+
reference_id = str(uuid.UUID(request.match_info["id"]))
|
| 752 |
+
try:
|
| 753 |
+
json_payload = await request.json()
|
| 754 |
+
data = schemas_in.TagsRemove.model_validate(json_payload)
|
| 755 |
+
except ValidationError as ve:
|
| 756 |
+
return _build_error_response(
|
| 757 |
+
400,
|
| 758 |
+
"INVALID_BODY",
|
| 759 |
+
"Invalid JSON body for tags remove.",
|
| 760 |
+
{"errors": ve.errors()},
|
| 761 |
+
)
|
| 762 |
+
except Exception:
|
| 763 |
+
return _build_error_response(
|
| 764 |
+
400, "INVALID_JSON", "Request body must be valid JSON."
|
| 765 |
+
)
|
| 766 |
+
|
| 767 |
+
try:
|
| 768 |
+
result = remove_tags(
|
| 769 |
+
reference_id=reference_id,
|
| 770 |
+
tags=data.tags,
|
| 771 |
+
owner_id=USER_MANAGER.get_request_user_id(request),
|
| 772 |
+
)
|
| 773 |
+
payload = schemas_out.TagsRemove(
|
| 774 |
+
removed=result.removed,
|
| 775 |
+
not_present=result.not_present,
|
| 776 |
+
total_tags=result.total_tags,
|
| 777 |
+
)
|
| 778 |
+
except PermissionError as pe:
|
| 779 |
+
return _build_error_response(403, "FORBIDDEN", str(pe), {"id": reference_id})
|
| 780 |
+
except ValueError as ve:
|
| 781 |
+
return _build_error_response(
|
| 782 |
+
404, "ASSET_NOT_FOUND", str(ve), {"id": reference_id}
|
| 783 |
+
)
|
| 784 |
+
except Exception:
|
| 785 |
+
logging.exception(
|
| 786 |
+
"remove_tags_from_asset failed for reference_id=%s, owner_id=%s",
|
| 787 |
+
reference_id,
|
| 788 |
+
USER_MANAGER.get_request_user_id(request),
|
| 789 |
+
)
|
| 790 |
+
return _build_error_response(500, "INTERNAL", "Unexpected server error.")
|
| 791 |
+
|
| 792 |
+
return web.json_response(payload.model_dump(mode="json", exclude_none=True), status=200)
|
| 793 |
+
|
| 794 |
+
|
| 795 |
+
@ROUTES.get("/api/assets/tags/refine")
|
| 796 |
+
@_require_assets_feature_enabled
|
| 797 |
+
async def get_tags_refine(request: web.Request) -> web.Response:
|
| 798 |
+
"""GET request to get tag histogram for filtered assets."""
|
| 799 |
+
query_dict = get_query_dict(request)
|
| 800 |
+
try:
|
| 801 |
+
q = schemas_in.TagsRefineQuery.model_validate(query_dict)
|
| 802 |
+
except ValidationError as ve:
|
| 803 |
+
return _build_validation_error_response("INVALID_QUERY", ve)
|
| 804 |
+
|
| 805 |
+
try:
|
| 806 |
+
tags_all, tags_any, tags_none = _resolve_tag_filters(q)
|
| 807 |
+
except InvalidTagFilterError as e:
|
| 808 |
+
return _build_error_response(400, "INVALID_TAG_FILTER", str(e), e.details)
|
| 809 |
+
|
| 810 |
+
tag_counts = list_tag_histogram(
|
| 811 |
+
owner_id=USER_MANAGER.get_request_user_id(request),
|
| 812 |
+
include_tags=tags_all,
|
| 813 |
+
exclude_tags=tags_none,
|
| 814 |
+
any_tags=tags_any,
|
| 815 |
+
name_contains=q.name_contains,
|
| 816 |
+
metadata_filter=q.metadata_filter,
|
| 817 |
+
limit=q.limit,
|
| 818 |
+
)
|
| 819 |
+
payload = schemas_out.TagHistogram(tag_counts=tag_counts)
|
| 820 |
+
return web.json_response(payload.model_dump(mode="json", exclude_none=True), status=200)
|
| 821 |
+
|
| 822 |
+
|
| 823 |
+
@ROUTES.post("/api/assets/seed")
|
| 824 |
+
@_require_assets_feature_enabled
|
| 825 |
+
async def seed_assets(request: web.Request) -> web.Response:
|
| 826 |
+
"""Trigger asset seeding for specified roots (models, input, output).
|
| 827 |
+
|
| 828 |
+
Query params:
|
| 829 |
+
wait: If "true", block until scan completes (synchronous behavior for tests)
|
| 830 |
+
|
| 831 |
+
Returns:
|
| 832 |
+
202 Accepted if scan started
|
| 833 |
+
409 Conflict if scan already running
|
| 834 |
+
200 OK with final stats if wait=true
|
| 835 |
+
"""
|
| 836 |
+
try:
|
| 837 |
+
payload = await request.json()
|
| 838 |
+
roots = payload.get("roots", ["models", "input", "output"])
|
| 839 |
+
except Exception:
|
| 840 |
+
roots = ["models", "input", "output"]
|
| 841 |
+
|
| 842 |
+
valid_roots = tuple(r for r in roots if r in ("models", "input", "output"))
|
| 843 |
+
if not valid_roots:
|
| 844 |
+
return _build_error_response(400, "INVALID_BODY", "No valid roots specified")
|
| 845 |
+
|
| 846 |
+
wait_param = request.query.get("wait", "").lower()
|
| 847 |
+
should_wait = wait_param in ("true", "1", "yes")
|
| 848 |
+
|
| 849 |
+
started = asset_seeder.start(roots=valid_roots)
|
| 850 |
+
if not started:
|
| 851 |
+
return web.json_response({"status": "already_running"}, status=409)
|
| 852 |
+
|
| 853 |
+
if should_wait:
|
| 854 |
+
await asyncio.to_thread(asset_seeder.wait)
|
| 855 |
+
status = asset_seeder.get_status()
|
| 856 |
+
return web.json_response(
|
| 857 |
+
{
|
| 858 |
+
"status": "completed",
|
| 859 |
+
"progress": {
|
| 860 |
+
"scanned": status.progress.scanned if status.progress else 0,
|
| 861 |
+
"total": status.progress.total if status.progress else 0,
|
| 862 |
+
"created": status.progress.created if status.progress else 0,
|
| 863 |
+
"skipped": status.progress.skipped if status.progress else 0,
|
| 864 |
+
},
|
| 865 |
+
"errors": status.errors,
|
| 866 |
+
},
|
| 867 |
+
status=200,
|
| 868 |
+
)
|
| 869 |
+
|
| 870 |
+
return web.json_response({"status": "started"}, status=202)
|
| 871 |
+
|
| 872 |
+
|
| 873 |
+
@ROUTES.get("/api/assets/seed/status")
|
| 874 |
+
@_require_assets_feature_enabled
|
| 875 |
+
async def get_seed_status(request: web.Request) -> web.Response:
|
| 876 |
+
"""Get current scan status and progress."""
|
| 877 |
+
status = asset_seeder.get_status()
|
| 878 |
+
return web.json_response(
|
| 879 |
+
{
|
| 880 |
+
"state": status.state.value,
|
| 881 |
+
"progress": {
|
| 882 |
+
"scanned": status.progress.scanned,
|
| 883 |
+
"total": status.progress.total,
|
| 884 |
+
"created": status.progress.created,
|
| 885 |
+
"skipped": status.progress.skipped,
|
| 886 |
+
}
|
| 887 |
+
if status.progress
|
| 888 |
+
else None,
|
| 889 |
+
"errors": status.errors,
|
| 890 |
+
},
|
| 891 |
+
status=200,
|
| 892 |
+
)
|
| 893 |
+
|
| 894 |
+
|
| 895 |
+
@ROUTES.post("/api/assets/seed/cancel")
|
| 896 |
+
@_require_assets_feature_enabled
|
| 897 |
+
async def cancel_seed(request: web.Request) -> web.Response:
|
| 898 |
+
"""Request cancellation of in-progress scan."""
|
| 899 |
+
cancelled = asset_seeder.cancel()
|
| 900 |
+
if cancelled:
|
| 901 |
+
return web.json_response({"status": "cancelling"}, status=200)
|
| 902 |
+
return web.json_response({"status": "idle"}, status=200)
|
| 903 |
+
|
| 904 |
+
|
| 905 |
+
@ROUTES.post("/api/assets/prune")
|
| 906 |
+
@_require_assets_feature_enabled
|
| 907 |
+
async def mark_missing_assets(request: web.Request) -> web.Response:
|
| 908 |
+
"""Mark assets as missing when outside all known root prefixes.
|
| 909 |
+
|
| 910 |
+
This is a non-destructive soft-delete operation. Assets and metadata
|
| 911 |
+
are preserved, but references are flagged as missing. They can be
|
| 912 |
+
restored if the file reappears in a future scan.
|
| 913 |
+
|
| 914 |
+
Returns:
|
| 915 |
+
200 OK with count of marked assets
|
| 916 |
+
409 Conflict if a scan is currently running
|
| 917 |
+
"""
|
| 918 |
+
try:
|
| 919 |
+
marked = asset_seeder.mark_missing_outside_prefixes()
|
| 920 |
+
except ScanInProgressError:
|
| 921 |
+
return web.json_response(
|
| 922 |
+
{"status": "scan_running", "marked": 0},
|
| 923 |
+
status=409,
|
| 924 |
+
)
|
| 925 |
+
return web.json_response({"status": "completed", "marked": marked}, status=200)
|