Add signed image URLs to image routes
这个提交存在于:
父节点
be58d4a2ef
当前提交
24f7512fe9
共有 2 个文件被更改,包括 43 次插入 和 7 次删除
|
|
@ -4,8 +4,11 @@ from __future__ import annotations
|
|||
|
||||
from typing import Any
|
||||
|
||||
from backend.dto.common import ApiError
|
||||
from backend.dto.image_dto import GenerationImage
|
||||
from backend.routes.common import handle_request
|
||||
from backend.services.image_service import ImageService
|
||||
from backend.services.media_service import MediaService
|
||||
from core.audit_service import AuditService
|
||||
from core.identity import RequestContext
|
||||
from core.security import require_authenticated, require_write
|
||||
|
|
@ -19,14 +22,16 @@ def list_node_images(
|
|||
node_id: str,
|
||||
query: dict[str, Any] | None = None,
|
||||
service: ImageService | None = None,
|
||||
media_service: MediaService | None = None,
|
||||
) -> dict:
|
||||
image_service = service or _create_image_service()
|
||||
signer = media_service or MediaService()
|
||||
params = query or {}
|
||||
|
||||
def action() -> list[dict[str, Any]]:
|
||||
require_authenticated(ctx)
|
||||
include_archived = _as_bool(params.get("include_archived"))
|
||||
return [image.to_dict() for image in image_service.list_by_node(node_id, include_archived)]
|
||||
return [_serialize_image(image, signer) for image in image_service.list_by_node(node_id, include_archived)]
|
||||
|
||||
return handle_request(ctx, action)
|
||||
|
||||
|
|
@ -35,12 +40,14 @@ def get_image(
|
|||
ctx: RequestContext,
|
||||
image_id: str,
|
||||
service: ImageService | None = None,
|
||||
media_service: MediaService | None = None,
|
||||
) -> dict:
|
||||
image_service = service or _create_image_service()
|
||||
signer = media_service or MediaService()
|
||||
|
||||
def action() -> dict[str, Any]:
|
||||
require_authenticated(ctx)
|
||||
return image_service.get_image(image_id).to_dict()
|
||||
return _serialize_image(image_service.get_image(image_id), signer)
|
||||
|
||||
return handle_request(ctx, action)
|
||||
|
||||
|
|
@ -49,12 +56,14 @@ def select_image(
|
|||
ctx: RequestContext,
|
||||
image_id: str,
|
||||
service: ImageService | None = None,
|
||||
media_service: MediaService | None = None,
|
||||
) -> dict:
|
||||
image_service = service or _create_image_service()
|
||||
signer = media_service or MediaService()
|
||||
|
||||
def action() -> dict[str, Any]:
|
||||
require_write(ctx)
|
||||
return image_service.select_image(image_id, ctx).to_dict()
|
||||
return _serialize_image(image_service.select_image(image_id, ctx), signer)
|
||||
|
||||
return handle_request(ctx, action)
|
||||
|
||||
|
|
@ -63,12 +72,14 @@ def unselect_image(
|
|||
ctx: RequestContext,
|
||||
image_id: str,
|
||||
service: ImageService | None = None,
|
||||
media_service: MediaService | None = None,
|
||||
) -> dict:
|
||||
image_service = service or _create_image_service()
|
||||
signer = media_service or MediaService()
|
||||
|
||||
def action() -> dict[str, Any]:
|
||||
require_write(ctx)
|
||||
return image_service.unselect_image(image_id, ctx).to_dict()
|
||||
return _serialize_image(image_service.unselect_image(image_id, ctx), signer)
|
||||
|
||||
return handle_request(ctx, action)
|
||||
|
||||
|
|
@ -77,12 +88,14 @@ def archive_image(
|
|||
ctx: RequestContext,
|
||||
image_id: str,
|
||||
service: ImageService | None = None,
|
||||
media_service: MediaService | None = None,
|
||||
) -> dict:
|
||||
image_service = service or _create_image_service()
|
||||
signer = media_service or MediaService()
|
||||
|
||||
def action() -> dict[str, Any]:
|
||||
require_write(ctx)
|
||||
return image_service.archive_image(image_id, ctx).to_dict()
|
||||
return _serialize_image(image_service.archive_image(image_id, ctx), signer)
|
||||
|
||||
return handle_request(ctx, action)
|
||||
|
||||
|
|
@ -91,12 +104,14 @@ def delete_image(
|
|||
ctx: RequestContext,
|
||||
image_id: str,
|
||||
service: ImageService | None = None,
|
||||
media_service: MediaService | None = None,
|
||||
) -> dict:
|
||||
image_service = service or _create_image_service()
|
||||
signer = media_service or MediaService()
|
||||
|
||||
def action() -> dict[str, Any]:
|
||||
require_write(ctx)
|
||||
return image_service.delete_image_metadata(image_id, ctx).to_dict()
|
||||
return _serialize_image(image_service.delete_image_metadata(image_id, ctx), signer)
|
||||
|
||||
return handle_request(ctx, action)
|
||||
|
||||
|
|
@ -116,3 +131,19 @@ def _as_bool(value: Any) -> bool:
|
|||
if isinstance(value, str):
|
||||
return value.strip().lower() in {"1", "true", "yes", "on"}
|
||||
return bool(value)
|
||||
|
||||
|
||||
def _serialize_image(image: GenerationImage, media_service: MediaService) -> dict[str, Any]:
|
||||
payload = image.to_dict()
|
||||
payload["image_url"] = _signed_url(media_service, image.image_uri)
|
||||
payload["thumbnail_url"] = _signed_url(media_service, image.thumbnail_uri)
|
||||
return payload
|
||||
|
||||
|
||||
def _signed_url(media_service: MediaService, uri: str | None) -> str | None:
|
||||
if not uri:
|
||||
return None
|
||||
try:
|
||||
return media_service.sign_media(uri)["url"]
|
||||
except ApiError:
|
||||
return None
|
||||
|
|
|
|||
|
|
@ -827,6 +827,7 @@ database:
|
|||
self.assertEqual(queued_node["data"]["status"], "queued")
|
||||
self.assertTrue(queued_node["data"]["metadata"]["queue_placeholder"])
|
||||
|
||||
(self.root / "images" / "first.png").write_bytes(b"first")
|
||||
first_image = self.image_service.create_image(
|
||||
{"node_id": node_id, "image_uri": "storage://images/first.png"},
|
||||
self.ctx,
|
||||
|
|
@ -855,12 +856,16 @@ database:
|
|||
second_image.image_id,
|
||||
)
|
||||
|
||||
visible_images = list_node_images(self.ctx, node_id, {}, self.image_service)
|
||||
visible_images = list_node_images(self.ctx, node_id, {}, self.image_service, self.media_service)
|
||||
self.assertTrue(visible_images["ok"])
|
||||
self.assertEqual(
|
||||
{image["image_id"] for image in visible_images["data"]},
|
||||
{first_image.image_id, second_image.image_id},
|
||||
)
|
||||
signed_first = next(image for image in visible_images["data"] if image["image_id"] == first_image.image_id)
|
||||
self.assertTrue(signed_first["image_url"].startswith("/api/v1/plugins/extensions/"))
|
||||
self.assertIsNone(signed_first["thumbnail_url"])
|
||||
self.assertNotIn(str(self.root), str(signed_first))
|
||||
|
||||
archived = archive_image(self.ctx, second_image.image_id, self.image_service)
|
||||
self.assertTrue(archived["ok"])
|
||||
|
|
|
|||
正在加载…
添加表格
在新工单中引用