'commit'
This commit is contained in:
@@ -7,6 +7,10 @@ from fastapi import HTTPException
|
||||
from JiMeng.Kit.FenJingTouGenerator import FenJingTouGenerator # 导入分镜头生成器
|
||||
from JiMeng.Kit.JmImg2VideoUtil import JmImg2Video # 导入视频生成工具
|
||||
from JiMeng.Kit.JmTxt2ImgUtil import JmTxt2Img
|
||||
import os
|
||||
import uuid
|
||||
import requests
|
||||
from Util.ObsUtil import ObsUploader
|
||||
|
||||
# 创建路由路由器
|
||||
router = APIRouter(prefix="/api/jimeng", tags=["即梦"])
|
||||
@@ -145,3 +149,73 @@ async def query_video_task(request: fastapi.Request):
|
||||
logger.error(f"视频任务查询失败: {str(e)}")
|
||||
raise HTTPException(status_code=500, detail=f"视频任务查询失败: {str(e)}")
|
||||
|
||||
|
||||
@router.post("/upload_url_to_obs")
|
||||
async def upload_url_to_obs(request: fastapi.Request):
|
||||
try:
|
||||
data = await request.json()
|
||||
url = data.get("url")
|
||||
|
||||
if not url:
|
||||
raise HTTPException(status_code=400, detail="缺少URL参数")
|
||||
|
||||
logger.info(f"收到URL上传请求,URL: {url}")
|
||||
|
||||
# 使用UUID生成唯一文件名
|
||||
unique_id = uuid.uuid4()
|
||||
object_key = f"HuangHai/JiMeng/{unique_id}.mp4"
|
||||
|
||||
# 临时文件保存路径
|
||||
temp_file_path = os.path.join(os.path.dirname(__file__), f"{unique_id}.mp4")
|
||||
|
||||
try:
|
||||
# 下载URL内容到临时文件
|
||||
logger.info(f"开始下载URL内容: {url}")
|
||||
with requests.get(url, stream=True, timeout=3600) as r:
|
||||
r.raise_for_status()
|
||||
with open(temp_file_path, 'wb') as f:
|
||||
for chunk in r.iter_content(chunk_size=8192):
|
||||
f.write(chunk)
|
||||
logger.info(f"URL内容下载完成,保存至: {temp_file_path}")
|
||||
|
||||
# 上传文件到OBS
|
||||
obs_uploader = ObsUploader()
|
||||
logger.info(f"开始上传文件到OBS: {temp_file_path}\n对象键: {object_key}")
|
||||
|
||||
# 正确处理元组返回值
|
||||
success, response_info = obs_uploader.upload_file(
|
||||
file_path=temp_file_path,
|
||||
object_key=object_key
|
||||
)
|
||||
|
||||
if success:
|
||||
# 构造OBS URL
|
||||
from Config.Config import OBS_SERVER, OBS_BUCKET
|
||||
obs_url = f"https://{OBS_BUCKET}.{OBS_SERVER}/{object_key}"
|
||||
logger.info(f"文件上传成功,OBS URL: {obs_url}")
|
||||
return {
|
||||
"code": 200,
|
||||
"message": "成功",
|
||||
"data": {
|
||||
"obs_url": obs_url,
|
||||
"object_key": object_key
|
||||
}
|
||||
}
|
||||
else:
|
||||
error_msg = f"上传失败: {str(response_info)}"
|
||||
logger.error(error_msg)
|
||||
raise HTTPException(status_code=500, detail=error_msg)
|
||||
|
||||
finally:
|
||||
# 清理临时文件
|
||||
if os.path.exists(temp_file_path):
|
||||
os.remove(temp_file_path)
|
||||
logger.info(f"临时文件已删除: {temp_file_path}")
|
||||
|
||||
except HTTPException as e:
|
||||
logger.error(f"请求参数错误: {str(e.detail)}")
|
||||
raise e
|
||||
except Exception as e:
|
||||
logger.error(f"URL上传处理失败: {str(e)}")
|
||||
raise HTTPException(status_code=500, detail=f"URL上传处理失败: {str(e)}")
|
||||
|
||||
|
Binary file not shown.
Reference in New Issue
Block a user