Fix memoir image delivery and Android rendering
This commit is contained in:
@@ -1,6 +1,8 @@
|
||||
"""
|
||||
章节相关 API 路由
|
||||
"""
|
||||
import logging
|
||||
import os
|
||||
from typing import List, Optional
|
||||
|
||||
from fastapi import APIRouter, Depends, HTTPException, Query
|
||||
@@ -12,11 +14,48 @@ from database.models import Chapter as ChapterModel
|
||||
from database.models import User as UserModel
|
||||
from middleware.auth import get_current_user
|
||||
from agents.prompts.memory_prompts import CHAPTER_CATEGORIES, CHAPTER_ORDER, STAGE_TO_ORDER
|
||||
from services.memoir_images.storage import (
|
||||
TencentCosStorageService,
|
||||
normalize_cos_url,
|
||||
resolve_image_storage_key,
|
||||
)
|
||||
|
||||
router = APIRouter(prefix="/api/chapters", tags=["chapters"])
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
def _normalize_image_assets(images: list[dict] | None) -> list[dict]:
|
||||
bucket = os.getenv("TENCENT_COS_BUCKET", "")
|
||||
region = os.getenv("TENCENT_COS_REGION", "")
|
||||
base_url = os.getenv("TENCENT_COS_BASE_URL", "")
|
||||
storage = TencentCosStorageService.from_env()
|
||||
normalized_assets: list[dict] = []
|
||||
|
||||
for item in (images or []):
|
||||
asset = dict(item)
|
||||
normalized_url = normalize_cos_url(
|
||||
asset.get("url"),
|
||||
bucket=bucket,
|
||||
region=region,
|
||||
base_url=base_url,
|
||||
)
|
||||
storage_key = resolve_image_storage_key(asset)
|
||||
if asset.get("status") == "completed" and storage_key:
|
||||
try:
|
||||
asset["url"] = storage.get_download_url(storage_key)
|
||||
except Exception as exc:
|
||||
logger.warning("章节图片签名失败: key=%s, error=%s", storage_key, exc)
|
||||
asset["url"] = normalized_url
|
||||
else:
|
||||
asset["url"] = normalized_url
|
||||
asset.pop("storage_key", None)
|
||||
normalized_assets.append(asset)
|
||||
|
||||
return normalized_assets
|
||||
|
||||
|
||||
def _chapter_to_dict(ch: ChapterModel) -> dict:
|
||||
normalized_images = _normalize_image_assets(ch.images)
|
||||
return {
|
||||
"id": ch.id,
|
||||
"title": ch.title,
|
||||
@@ -24,7 +63,7 @@ def _chapter_to_dict(ch: ChapterModel) -> dict:
|
||||
"order_index": ch.order_index,
|
||||
"status": ch.status,
|
||||
"category": ch.category,
|
||||
"images": ch.images or [],
|
||||
"images": normalized_images,
|
||||
"updated_at": ch.updated_at.isoformat() if ch.updated_at else None,
|
||||
"is_new": ch.is_new,
|
||||
"source_segments": ch.source_segments or [],
|
||||
@@ -105,7 +144,7 @@ async def get_chapter(
|
||||
"order_index": chapter.order_index,
|
||||
"status": chapter.status,
|
||||
"category": chapter.category,
|
||||
"images": chapter.images or [],
|
||||
"images": _normalize_image_assets(chapter.images),
|
||||
"updated_at": chapter.updated_at.isoformat() if chapter.updated_at else None,
|
||||
"is_new": chapter.is_new,
|
||||
"source_segments": chapter.source_segments or [],
|
||||
@@ -151,4 +190,3 @@ async def regenerate_chapter(
|
||||
|
||||
# TODO: 实现重新整理逻辑
|
||||
return {"status": "ok", "message": "Chapter regeneration triggered"}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user