45 lines
1.2 KiB
Python
45 lines
1.2 KiB
Python
"""Persistent image fixtures used by repeatable assistant test cases."""
|
|||
|
|
|
||
|
|
from __future__ import annotations
|
||
|
|
|
||
|
|
from dataclasses import dataclass
|
||
|
|
|
||
|
|
from services.image_processing import normalize_image
|
||
|
|
from services.object_storage import delete_object, get_object, put_object
|
||
|
|
|
||
|
|
|
||
|
|
TEST_ASSET_PREFIX = "test-assets/"
|
||
|
|
|
||
|
|
|
||
|
|
@dataclass(frozen=True)
|
||
|
|
class StoredTestImage:
|
||
|
|
storage_key: str
|
||
|
|
width: int
|
||
|
|
height: int
|
||
|
|
size_bytes: int
|
||
|
|
mime_type: str = "image/jpeg"
|
||
|
|
|
||
|
|
|
||
|
|
def store_test_image(asset_id: str, data: bytes) -> StoredTestImage:
|
||
|
|
normalized, width, height = normalize_image(data)
|
||
|
|
key = f"{TEST_ASSET_PREFIX}{asset_id}.jpg"
|
||
|
|
put_object(key, normalized, "image/jpeg")
|
||
|
|
return StoredTestImage(
|
||
|
|
storage_key=key,
|
||
|
|
width=width,
|
||
|
|
height=height,
|
||
|
|
size_bytes=len(normalized),
|
||
|
|
)
|
||
|
|
|
||
|
|
|
||
|
|
def read_test_image(storage_key: str) -> bytes:
|
||
|
|
if not storage_key.startswith(TEST_ASSET_PREFIX):
|
||
|
|
raise ValueError("测试图片存储位置无效")
|
||
|
|
return get_object(storage_key)
|
||
|
|
|
||
|
|
|
||
|
|
def delete_test_image(storage_key: str) -> None:
|
||
|
|
if not storage_key.startswith(TEST_ASSET_PREFIX):
|
||
|
|
raise ValueError("测试图片存储位置无效")
|
||
|
|
delete_object(storage_key)
|