feat: expand AI workflow support and refresh docs
This commit is contained in:
@@ -5,18 +5,65 @@
|
||||
|
||||
from typing import List, Optional
|
||||
from datetime import datetime, timedelta
|
||||
from urllib.parse import urlparse
|
||||
from fastapi import APIRouter, Depends, HTTPException, Query
|
||||
from pydantic import BaseModel
|
||||
from sqlalchemy.orm import Session, joinedload
|
||||
from sqlalchemy import desc
|
||||
|
||||
from app.db import get_db
|
||||
from app.core.config import settings
|
||||
from app.core.security import get_current_user
|
||||
from app.models.logs import PltProcessRecord, UserActionLog, PltPiece
|
||||
from app.models.user import User
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
CURRENT_QINIU_DOMAIN = settings.QINIU_DOMAIN.rstrip("/")
|
||||
CURRENT_QINIU_HOST = urlparse(CURRENT_QINIU_DOMAIN).netloc.lower() if CURRENT_QINIU_DOMAIN else ""
|
||||
KNOWN_QINIU_HOST_MARKERS = ("qiniuio.com", "clouddn.com", "qnssl.com")
|
||||
LEGACY_QINIU_HOSTS = {
|
||||
"iovip-z2.qiniuio.com",
|
||||
"t9zfnhhrn.hn-bkt.clouddn.com",
|
||||
}
|
||||
|
||||
|
||||
def normalize_qiniu_url(url: Optional[str]) -> Optional[str]:
|
||||
"""将历史记录中的旧七牛域名切换到当前绑定域名。"""
|
||||
if not url or not CURRENT_QINIU_DOMAIN:
|
||||
return url
|
||||
|
||||
parsed = urlparse(url)
|
||||
host = parsed.netloc.lower()
|
||||
|
||||
if not parsed.scheme or not host or not parsed.path:
|
||||
return url
|
||||
|
||||
if host == CURRENT_QINIU_HOST:
|
||||
return url
|
||||
|
||||
is_qiniu_host = host in LEGACY_QINIU_HOSTS or any(
|
||||
host.endswith(marker) for marker in KNOWN_QINIU_HOST_MARKERS
|
||||
)
|
||||
if not is_qiniu_host:
|
||||
return url
|
||||
|
||||
suffix = parsed.path
|
||||
if parsed.query:
|
||||
suffix = f"{suffix}?{parsed.query}"
|
||||
return f"{CURRENT_QINIU_DOMAIN}{suffix}"
|
||||
|
||||
|
||||
def normalize_record_piece_urls(record: Optional[PltProcessRecord]) -> Optional[PltProcessRecord]:
|
||||
"""在返回详情前修正裁片图片地址,兼容旧域名历史记录。"""
|
||||
if not record or not record.pieces:
|
||||
return record
|
||||
|
||||
for piece in record.pieces:
|
||||
piece.image_url = normalize_qiniu_url(piece.image_url)
|
||||
|
||||
return record
|
||||
|
||||
|
||||
# ==================== 数据模型 ====================
|
||||
|
||||
@@ -197,7 +244,7 @@ async def create_plt_record(
|
||||
record_id=record.id,
|
||||
group_id=piece_data.group_id,
|
||||
size=piece_data.size,
|
||||
image_url=piece_data.image_url,
|
||||
image_url=normalize_qiniu_url(piece_data.image_url),
|
||||
width_px=piece_data.width_px,
|
||||
height_px=piece_data.height_px,
|
||||
width_cm=piece_data.width_cm,
|
||||
@@ -235,28 +282,29 @@ async def get_plt_record_detail(
|
||||
if not record:
|
||||
raise HTTPException(status_code=404, detail="记录不存在")
|
||||
|
||||
return record
|
||||
return normalize_record_piece_urls(record)
|
||||
|
||||
|
||||
@router.get("/plt/history", response_model=List[PltRecordResponse])
|
||||
async def get_plt_history(
|
||||
limit: int = Query(20, ge=1, le=100),
|
||||
days: int = Query(7, ge=1, le=30), # 保留天数,默认7天
|
||||
days: int = Query(0, ge=0, le=3650), # 0 表示不限制时间
|
||||
current_username: str = Depends(get_current_user),
|
||||
db: Session = Depends(get_db)
|
||||
):
|
||||
"""获取用户的PLT处理历史(默认保留7天)"""
|
||||
"""获取用户的 PLT 处理历史,days=0 表示全部记录"""
|
||||
user = db.query(User).filter(User.username == current_username).first()
|
||||
if not user:
|
||||
raise HTTPException(status_code=404, detail="用户不存在")
|
||||
|
||||
# 只返回指定天数内的记录
|
||||
since = datetime.now() - timedelta(days=days)
|
||||
|
||||
records = db.query(PltProcessRecord)\
|
||||
.filter(PltProcessRecord.user_id == user.id)\
|
||||
.filter(PltProcessRecord.created_at >= since)\
|
||||
.order_by(desc(PltProcessRecord.created_at))\
|
||||
|
||||
query = db.query(PltProcessRecord)\
|
||||
.filter(PltProcessRecord.user_id == user.id)
|
||||
|
||||
if days > 0:
|
||||
since = datetime.now() - timedelta(days=days)
|
||||
query = query.filter(PltProcessRecord.created_at >= since)
|
||||
|
||||
records = query.order_by(desc(PltProcessRecord.created_at))\
|
||||
.limit(limit)\
|
||||
.all()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user