mirror of
https://github.com/comfyanonymous/ComfyUI.git
synced 2026-03-06 09:47:35 +08:00
Consolidate duplicate delete_temp_file_if_exists function
- Remove duplicate from routes.py - Import from upload.py instead - Rename to public API (remove leading underscore) Amp-Thread-ID: https://ampcode.com/threads/T-019c3549-c245-7628-950c-dd6826185394 Co-authored-by: Amp <amp@ampcode.com>
This commit is contained in:
parent
7c41cdd75a
commit
5f0eb0432c
@ -14,7 +14,10 @@ from app.assets.api.schemas_in import (
|
|||||||
AssetValidationError,
|
AssetValidationError,
|
||||||
UploadError,
|
UploadError,
|
||||||
)
|
)
|
||||||
from app.assets.api.upload import parse_multipart_upload
|
from app.assets.api.upload import (
|
||||||
|
delete_temp_file_if_exists,
|
||||||
|
parse_multipart_upload,
|
||||||
|
)
|
||||||
from app.assets.seeder import asset_seeder
|
from app.assets.seeder import asset_seeder
|
||||||
from app.assets.services import (
|
from app.assets.services import (
|
||||||
DependencyMissingError,
|
DependencyMissingError,
|
||||||
@ -305,14 +308,6 @@ async def create_asset_from_hash_route(request: web.Request) -> web.Response:
|
|||||||
return web.json_response(payload_out.model_dump(mode="json"), status=201)
|
return web.json_response(payload_out.model_dump(mode="json"), status=201)
|
||||||
|
|
||||||
|
|
||||||
def _delete_temp_file_if_exists(path: str | None) -> None:
|
|
||||||
if path and os.path.exists(path):
|
|
||||||
try:
|
|
||||||
os.remove(path)
|
|
||||||
except Exception:
|
|
||||||
pass
|
|
||||||
|
|
||||||
|
|
||||||
@ROUTES.post("/api/assets")
|
@ROUTES.post("/api/assets")
|
||||||
async def upload_asset(request: web.Request) -> web.Response:
|
async def upload_asset(request: web.Request) -> web.Response:
|
||||||
"""Multipart/form-data endpoint for Asset uploads."""
|
"""Multipart/form-data endpoint for Asset uploads."""
|
||||||
@ -333,7 +328,7 @@ async def upload_asset(request: web.Request) -> web.Response:
|
|||||||
}
|
}
|
||||||
)
|
)
|
||||||
except ValidationError as ve:
|
except ValidationError as ve:
|
||||||
_delete_temp_file_if_exists(parsed.tmp_path)
|
delete_temp_file_if_exists(parsed.tmp_path)
|
||||||
return _build_error_response(
|
return _build_error_response(
|
||||||
400, "INVALID_BODY", f"Validation failed: {ve.json()}"
|
400, "INVALID_BODY", f"Validation failed: {ve.json()}"
|
||||||
)
|
)
|
||||||
@ -343,7 +338,7 @@ async def upload_asset(request: web.Request) -> web.Response:
|
|||||||
len(spec.tags) < 2
|
len(spec.tags) < 2
|
||||||
or spec.tags[1] not in folder_paths.folder_names_and_paths
|
or spec.tags[1] not in folder_paths.folder_names_and_paths
|
||||||
):
|
):
|
||||||
_delete_temp_file_if_exists(parsed.tmp_path)
|
delete_temp_file_if_exists(parsed.tmp_path)
|
||||||
category = spec.tags[1] if len(spec.tags) >= 2 else ""
|
category = spec.tags[1] if len(spec.tags) >= 2 else ""
|
||||||
return _build_error_response(
|
return _build_error_response(
|
||||||
400, "INVALID_BODY", f"unknown models category '{category}'"
|
400, "INVALID_BODY", f"unknown models category '{category}'"
|
||||||
@ -360,11 +355,11 @@ async def upload_asset(request: web.Request) -> web.Response:
|
|||||||
owner_id=owner_id,
|
owner_id=owner_id,
|
||||||
)
|
)
|
||||||
if result is None:
|
if result is None:
|
||||||
_delete_temp_file_if_exists(parsed.tmp_path)
|
delete_temp_file_if_exists(parsed.tmp_path)
|
||||||
return _build_error_response(
|
return _build_error_response(
|
||||||
404, "ASSET_NOT_FOUND", f"Asset content {spec.hash} does not exist"
|
404, "ASSET_NOT_FOUND", f"Asset content {spec.hash} does not exist"
|
||||||
)
|
)
|
||||||
_delete_temp_file_if_exists(parsed.tmp_path)
|
delete_temp_file_if_exists(parsed.tmp_path)
|
||||||
else:
|
else:
|
||||||
# Otherwise, we must have a temp file path to ingest
|
# Otherwise, we must have a temp file path to ingest
|
||||||
if not parsed.tmp_path or not os.path.exists(parsed.tmp_path):
|
if not parsed.tmp_path or not os.path.exists(parsed.tmp_path):
|
||||||
@ -384,19 +379,19 @@ async def upload_asset(request: web.Request) -> web.Response:
|
|||||||
expected_hash=spec.hash,
|
expected_hash=spec.hash,
|
||||||
)
|
)
|
||||||
except AssetValidationError as e:
|
except AssetValidationError as e:
|
||||||
_delete_temp_file_if_exists(parsed.tmp_path)
|
delete_temp_file_if_exists(parsed.tmp_path)
|
||||||
return _build_error_response(400, e.code, str(e))
|
return _build_error_response(400, e.code, str(e))
|
||||||
except ValueError as e:
|
except ValueError as e:
|
||||||
_delete_temp_file_if_exists(parsed.tmp_path)
|
delete_temp_file_if_exists(parsed.tmp_path)
|
||||||
return _build_error_response(400, "BAD_REQUEST", str(e))
|
return _build_error_response(400, "BAD_REQUEST", str(e))
|
||||||
except HashMismatchError as e:
|
except HashMismatchError as e:
|
||||||
_delete_temp_file_if_exists(parsed.tmp_path)
|
delete_temp_file_if_exists(parsed.tmp_path)
|
||||||
return _build_error_response(400, "HASH_MISMATCH", str(e))
|
return _build_error_response(400, "HASH_MISMATCH", str(e))
|
||||||
except DependencyMissingError as e:
|
except DependencyMissingError as e:
|
||||||
_delete_temp_file_if_exists(parsed.tmp_path)
|
delete_temp_file_if_exists(parsed.tmp_path)
|
||||||
return _build_error_response(503, "DEPENDENCY_MISSING", e.message)
|
return _build_error_response(503, "DEPENDENCY_MISSING", e.message)
|
||||||
except Exception:
|
except Exception:
|
||||||
_delete_temp_file_if_exists(parsed.tmp_path)
|
delete_temp_file_if_exists(parsed.tmp_path)
|
||||||
logging.exception("upload_asset failed for owner_id=%s", owner_id)
|
logging.exception("upload_asset failed for owner_id=%s", owner_id)
|
||||||
return _build_error_response(500, "INTERNAL", "Unexpected server error.")
|
return _build_error_response(500, "INTERNAL", "Unexpected server error.")
|
||||||
|
|
||||||
|
|||||||
@ -117,7 +117,7 @@ async def parse_multipart_upload(
|
|||||||
f.write(chunk)
|
f.write(chunk)
|
||||||
file_written += len(chunk)
|
file_written += len(chunk)
|
||||||
except Exception:
|
except Exception:
|
||||||
_delete_temp_file_if_exists(tmp_path)
|
delete_temp_file_if_exists(tmp_path)
|
||||||
raise UploadError(
|
raise UploadError(
|
||||||
500, "UPLOAD_IO_ERROR", "Failed to receive and store uploaded file."
|
500, "UPLOAD_IO_ERROR", "Failed to receive and store uploaded file."
|
||||||
)
|
)
|
||||||
@ -139,7 +139,7 @@ async def parse_multipart_upload(
|
|||||||
and file_written == 0
|
and file_written == 0
|
||||||
and not (provided_hash and provided_hash_exists)
|
and not (provided_hash and provided_hash_exists)
|
||||||
):
|
):
|
||||||
_delete_temp_file_if_exists(tmp_path)
|
delete_temp_file_if_exists(tmp_path)
|
||||||
raise UploadError(400, "EMPTY_UPLOAD", "Uploaded file is empty.")
|
raise UploadError(400, "EMPTY_UPLOAD", "Uploaded file is empty.")
|
||||||
|
|
||||||
return ParsedUpload(
|
return ParsedUpload(
|
||||||
@ -155,7 +155,7 @@ async def parse_multipart_upload(
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
def _delete_temp_file_if_exists(tmp_path: str | None) -> None:
|
def delete_temp_file_if_exists(tmp_path: str | None) -> None:
|
||||||
"""Safely remove a temp file if it exists."""
|
"""Safely remove a temp file if it exists."""
|
||||||
if tmp_path:
|
if tmp_path:
|
||||||
try:
|
try:
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user