fix(platform): 持久化并完善斗鱼兑换结果图片
This commit is contained in:
@@ -0,0 +1,123 @@
|
||||
"""保存斗鱼兑换结果图片,供履约任务长期查看、复制和下载。"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from datetime import UTC, datetime
|
||||
from typing import Any
|
||||
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from ..models import DouyuTask, PlatformFulfillmentTask
|
||||
from .file_service import build_file_url, sniff_content_type, upload_file_asset
|
||||
|
||||
EXCHANGE_RESULT_TASK_TYPES = frozenset(
|
||||
{
|
||||
"exchange_goods",
|
||||
"pay_locked_order",
|
||||
"exchange_esports_goods",
|
||||
"exchange_xpd_goods",
|
||||
}
|
||||
)
|
||||
EXCHANGE_RESULT_SCENE = "exchange-result"
|
||||
|
||||
|
||||
class ExchangeResultImageError(ValueError):
|
||||
"""兑换结果图片不符合任务状态或文件契约。"""
|
||||
|
||||
|
||||
def _result_image(task: DouyuTask) -> dict[str, Any]:
|
||||
result = task.result if isinstance(task.result, dict) else {}
|
||||
image = result.get("exchange_result_image")
|
||||
return image if isinstance(image, dict) else {}
|
||||
|
||||
|
||||
def exchange_result_image_payload(
|
||||
task: DouyuTask,
|
||||
platform_task: PlatformFulfillmentTask | None = None,
|
||||
) -> dict[str, Any] | None:
|
||||
"""返回任务已保存图片的稳定代理地址;没有保存时返回 ``None``。"""
|
||||
image = _result_image(task)
|
||||
object_key = str(
|
||||
(platform_task.exchange_result_image_key if platform_task else "")
|
||||
or image.get("object_key")
|
||||
or ""
|
||||
).strip()
|
||||
if not object_key:
|
||||
return None
|
||||
saved_at = (
|
||||
platform_task.exchange_result_image_saved_at
|
||||
if platform_task
|
||||
else None
|
||||
)
|
||||
if saved_at is None:
|
||||
raw_saved_at = image.get("saved_at")
|
||||
if isinstance(raw_saved_at, str):
|
||||
try:
|
||||
saved_at = datetime.fromisoformat(raw_saved_at)
|
||||
except ValueError:
|
||||
saved_at = None
|
||||
return {
|
||||
"objectKey": object_key,
|
||||
"url": build_file_url(object_key),
|
||||
"savedAt": saved_at,
|
||||
}
|
||||
|
||||
|
||||
def save_exchange_result_image(
|
||||
db: Session,
|
||||
task: DouyuTask,
|
||||
*,
|
||||
data: bytes,
|
||||
filename: str = "",
|
||||
uploader_id: int,
|
||||
platform_task: PlatformFulfillmentTask | None = None,
|
||||
) -> dict[str, Any]:
|
||||
"""保存 PNG 并把对象 key 同时写回斗鱼任务和履约任务。
|
||||
|
||||
重复调用是幂等的:已有图片直接返回,不再产生第二份对象。
|
||||
"""
|
||||
if task.task_type not in EXCHANGE_RESULT_TASK_TYPES:
|
||||
raise ExchangeResultImageError("当前任务不是兑换任务")
|
||||
if task.status != "success":
|
||||
raise ExchangeResultImageError("只有兑换成功的任务可以保存结果图片")
|
||||
if sniff_content_type(data) != "image/png":
|
||||
raise ExchangeResultImageError("兑换结果图片必须是 PNG")
|
||||
|
||||
existing = exchange_result_image_payload(task, platform_task)
|
||||
if existing is not None:
|
||||
if platform_task and not platform_task.exchange_result_image_key:
|
||||
platform_task.exchange_result_image_key = str(existing["objectKey"])
|
||||
platform_task.exchange_result_image_saved_at = (
|
||||
existing["savedAt"] or datetime.now(UTC).replace(microsecond=0)
|
||||
)
|
||||
db.commit()
|
||||
return existing
|
||||
|
||||
uploaded = upload_file_asset(
|
||||
db,
|
||||
data=data,
|
||||
filename=filename or f"douyu-exchange-result-{task.id}.png",
|
||||
content_type="image/png",
|
||||
scene=EXCHANGE_RESULT_SCENE,
|
||||
uploader_type="user",
|
||||
uploader_id=str(uploader_id),
|
||||
)
|
||||
saved_at = datetime.now(UTC).replace(microsecond=0)
|
||||
result = dict(task.result or {})
|
||||
result["exchange_result_image"] = {
|
||||
"object_key": uploaded.object_key,
|
||||
"content_type": uploaded.content_type,
|
||||
"size": uploaded.size,
|
||||
"saved_at": saved_at.isoformat(),
|
||||
}
|
||||
task.result = result
|
||||
if platform_task is not None:
|
||||
platform_task.exchange_task_id = int(task.id)
|
||||
platform_task.exchange_result_image_key = uploaded.object_key
|
||||
platform_task.exchange_result_image_saved_at = saved_at
|
||||
db.commit()
|
||||
return {
|
||||
"objectKey": uploaded.object_key,
|
||||
"url": uploaded.url,
|
||||
"savedAt": saved_at,
|
||||
}
|
||||
Reference in New Issue
Block a user