Merge branch 'main' of gitee.com:wg123/video-gen into main
This commit is contained in:
@@ -1,21 +1,25 @@
|
||||
import os
|
||||
import json
|
||||
import uuid
|
||||
from typing import Any, Optional, Dict
|
||||
from fastapi import APIRouter, Depends, HTTPException, status
|
||||
from fastapi import APIRouter, Depends, HTTPException, status, Query
|
||||
from pydantic import BaseModel, Field
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
from sqlalchemy import select, func
|
||||
|
||||
from app.dependencies import get_current_user, get_db
|
||||
from app.models.user import User
|
||||
from app.models.pre_test_template import PreTestTemplate
|
||||
from app.models.upload_task import UploadTask
|
||||
from app.services.upload_material_service import upload_material_to_platform
|
||||
from sqlalchemy import select
|
||||
from app.services.upload_queue import upload_queue
|
||||
from app.utils.douyinApi import DouyinApi
|
||||
|
||||
|
||||
router = APIRouter(prefix="/upload-material", tags=["上传素材"])
|
||||
|
||||
|
||||
class UploadTask(BaseModel):
|
||||
class UploadTaskRequest(BaseModel):
|
||||
advertiser_ids: list[str] = Field(..., description="广告主id数组,支持多条")
|
||||
resource_ids: list[str] = Field(..., description="资源id数组(generated_resources表主键)")
|
||||
oauth_id: str = Field(..., description="授权表id")
|
||||
@@ -24,7 +28,7 @@ class UploadTask(BaseModel):
|
||||
|
||||
|
||||
class BatchUploadRequest(BaseModel):
|
||||
tasks: list[UploadTask] = Field(..., description="批量上传任务列表")
|
||||
tasks: list[UploadTaskRequest] = Field(..., description="批量上传任务列表")
|
||||
|
||||
|
||||
@router.post(
|
||||
@@ -37,22 +41,6 @@ async def batch_upload_material(
|
||||
current_user: User = Depends(get_current_user),
|
||||
db: AsyncSession = Depends(get_db),
|
||||
) -> Any | dict:
|
||||
# try:
|
||||
# params = {
|
||||
# "advertiser_id": 1836693172153543,
|
||||
# "video_ids": ["tos-cn-i-sd07hgqsbj/2db9a53ee3ff4b6f8a2763b3bb463047"],
|
||||
# "diagnose_config": {"platform": "AD", "external_action": "AD_APP_ACTIVATE"},
|
||||
# }
|
||||
# response = await DouyinApi().pre_test_material("0019ecab9b8bc57d964", params)
|
||||
# return response
|
||||
# except Exception as e:
|
||||
# import traceback
|
||||
# return {
|
||||
# "code": 500,
|
||||
# "message": "请求失败",
|
||||
# "error": str(e),
|
||||
# "traceback": traceback.format_exc()
|
||||
# }
|
||||
if not req.tasks:
|
||||
return {
|
||||
"code": 0,
|
||||
@@ -214,4 +202,174 @@ async def batch_upload_material(
|
||||
"total_requested": sum(len(t.resource_ids) * len(t.advertiser_ids) for t in req.tasks),
|
||||
},
|
||||
"details": all_results,
|
||||
}
|
||||
|
||||
|
||||
@router.post(
|
||||
"/async-batch-upload",
|
||||
summary="异步批量上传素材到平台",
|
||||
description="支持批量上传多个授权账户下的资源到素材库,提交后立即返回,后台异步处理",
|
||||
)
|
||||
async def async_batch_upload_material(
|
||||
req: BatchUploadRequest,
|
||||
current_user: User = Depends(get_current_user),
|
||||
db: AsyncSession = Depends(get_db),
|
||||
) -> Any | dict:
|
||||
if not req.tasks:
|
||||
return {
|
||||
"code": 0,
|
||||
"message": "上传任务列表不能为空",
|
||||
"task_ids": [],
|
||||
}
|
||||
|
||||
task_ids = []
|
||||
|
||||
for task in req.tasks:
|
||||
if not task.advertiser_ids or not task.resource_ids:
|
||||
continue
|
||||
|
||||
if task.is_pre_test == "1" and not task.pre_test_template:
|
||||
continue
|
||||
|
||||
for advertiser_id in task.advertiser_ids:
|
||||
for resource_id in task.resource_ids:
|
||||
task_id = str(uuid.uuid4()).replace("-", "")[:32]
|
||||
|
||||
upload_task = UploadTask(
|
||||
id=task_id,
|
||||
user_id=current_user.id,
|
||||
oauth_id=task.oauth_id,
|
||||
advertiser_id=advertiser_id,
|
||||
resource_id=resource_id,
|
||||
status=1,
|
||||
note=None,
|
||||
)
|
||||
|
||||
db.add(upload_task)
|
||||
await upload_queue.enqueue(task_id)
|
||||
task_ids.append(task_id)
|
||||
|
||||
await db.commit()
|
||||
|
||||
return {
|
||||
"code": 0,
|
||||
"message": "上传任务已提交,请通过查询接口查看进度",
|
||||
"task_ids": task_ids,
|
||||
}
|
||||
|
||||
|
||||
@router.get(
|
||||
"/upload-status",
|
||||
summary="查询上传任务状态",
|
||||
description="查询单个上传任务的执行状态和结果",
|
||||
)
|
||||
async def get_upload_status(
|
||||
task_id: str = Query(..., description="上传任务ID"),
|
||||
current_user: User = Depends(get_current_user),
|
||||
db: AsyncSession = Depends(get_db),
|
||||
) -> Any | dict:
|
||||
result = await db.execute(
|
||||
select(UploadTask).where(
|
||||
UploadTask.id == task_id,
|
||||
UploadTask.user_id == current_user.id,
|
||||
UploadTask.deleted_at.is_(None),
|
||||
)
|
||||
)
|
||||
task = result.scalar_one_or_none()
|
||||
|
||||
if not task:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_404_NOT_FOUND,
|
||||
detail="任务不存在或无权限查看",
|
||||
)
|
||||
|
||||
status_map = {
|
||||
1: "待上传",
|
||||
2: "上传中",
|
||||
3: "上传成功",
|
||||
4: "上传失败",
|
||||
}
|
||||
|
||||
return {
|
||||
"code": 0,
|
||||
"data": {
|
||||
"task_id": task.id,
|
||||
"status": task.status,
|
||||
"status_text": status_map.get(task.status, "未知"),
|
||||
"advertiser_id": task.advertiser_id,
|
||||
"resource_id": task.resource_id,
|
||||
"note": task.note,
|
||||
"created_at": task.created_at,
|
||||
"updated_at": task.updated_at,
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
@router.get(
|
||||
"/upload-history",
|
||||
summary="查询上传任务历史",
|
||||
description="查询当前用户的上传任务历史列表",
|
||||
)
|
||||
async def get_upload_history(
|
||||
page: int = Query(1, ge=1, description="页码"),
|
||||
page_size: int = Query(20, ge=1, le=100, description="每页数量"),
|
||||
status: Optional[int] = Query(None, description="上传状态筛选:1待上传,2上传中,3上传成功,4上传失败"),
|
||||
current_user: User = Depends(get_current_user),
|
||||
db: AsyncSession = Depends(get_db),
|
||||
) -> Any | dict:
|
||||
offset = (page - 1) * page_size
|
||||
|
||||
query = select(UploadTask).where(
|
||||
UploadTask.user_id == current_user.id,
|
||||
UploadTask.deleted_at.is_(None),
|
||||
)
|
||||
|
||||
if status is not None:
|
||||
query = query.where(UploadTask.status == status)
|
||||
|
||||
result = await db.execute(
|
||||
query.order_by(UploadTask.created_at.desc())
|
||||
.offset(offset)
|
||||
.limit(page_size)
|
||||
)
|
||||
tasks = result.scalars().all()
|
||||
|
||||
count_query = select(func.count(UploadTask.id)).where(
|
||||
UploadTask.user_id == current_user.id,
|
||||
UploadTask.deleted_at.is_(None),
|
||||
)
|
||||
|
||||
if status is not None:
|
||||
count_query = count_query.where(UploadTask.status == status)
|
||||
|
||||
total_result = await db.execute(count_query)
|
||||
total = total_result.scalar_one()
|
||||
|
||||
status_map = {
|
||||
1: "待上传",
|
||||
2: "上传中",
|
||||
3: "上传成功",
|
||||
4: "上传失败",
|
||||
}
|
||||
|
||||
return {
|
||||
"code": 0,
|
||||
"data": [
|
||||
{
|
||||
"task_id": task.id,
|
||||
"status": task.status,
|
||||
"status_text": status_map.get(task.status, "未知"),
|
||||
"advertiser_id": task.advertiser_id,
|
||||
"resource_id": task.resource_id,
|
||||
"note": task.note,
|
||||
"created_at": task.created_at,
|
||||
"updated_at": task.updated_at,
|
||||
}
|
||||
for task in tasks
|
||||
],
|
||||
"pagination": {
|
||||
"page": page,
|
||||
"page_size": page_size,
|
||||
"total": total,
|
||||
},
|
||||
}
|
||||
Reference in New Issue
Block a user