'commit'
This commit is contained in:
@@ -3,6 +3,8 @@ import json
|
||||
import time
|
||||
import datetime
|
||||
import requests
|
||||
import uuid # 新增:导入uuid模块
|
||||
import os # 新增:导入os模块
|
||||
from typing import Optional
|
||||
|
||||
import fastapi
|
||||
@@ -201,12 +203,53 @@ async def check_task_status(task_id: str = Query(..., description="音乐生成
|
||||
task_info["audio_url"] = audio_url
|
||||
logger.info("音乐生成已完成!")
|
||||
logger.info(f"音频URL: {audio_url}")
|
||||
|
||||
# 新增:下载音频文件并上传到OBS
|
||||
try:
|
||||
# 使用UUID生成唯一文件名
|
||||
unique_id = uuid.uuid4()
|
||||
object_key = f"HuangHai/JiMeng/{unique_id}.mp3"
|
||||
|
||||
# 临时文件保存路径
|
||||
temp_file_path = os.path.join(os.path.dirname(__file__), f"{unique_id}.mp3")
|
||||
|
||||
# 下载URL内容到临时文件
|
||||
logger.info(f"开始下载URL内容: {audio_url}")
|
||||
with requests.get(audio_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
|
||||
from Util.ObsUtil import ObsUploader # 导入ObsUploader
|
||||
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}")
|
||||
task_info["obs_url"] = obs_url
|
||||
task_info["object_key"] = object_key
|
||||
else:
|
||||
error_msg = f"上传失败: {str(response_info)}"
|
||||
logger.error(error_msg)
|
||||
finally:
|
||||
# 清理临时文件
|
||||
if os.path.exists(temp_file_path):
|
||||
os.remove(temp_file_path)
|
||||
logger.info(f"临时文件已删除: {temp_file_path}")
|
||||
# 新增结束
|
||||
|
||||
# 下载音频文件
|
||||
file_name = f"suno_music_{int(time.time())}.mp3"
|
||||
save_path = music_generator.base_path / file_name
|
||||
if music_generator.download_audio(audio_url, str(save_path)):
|
||||
task_info["local_path"] = str(save_path)
|
||||
else:
|
||||
logger.warning("音乐生成已完成,但未找到音频URL!")
|
||||
|
||||
@@ -225,7 +268,7 @@ async def check_task_status(task_id: str = Query(..., description="音乐生成
|
||||
"status": status,
|
||||
"title": title,
|
||||
"audio_url": audio_url if status == "complete" else None,
|
||||
"local_path": task_info.get("local_path") if status == "complete" else None,
|
||||
"obs_url": task_info.get("obs_url") if status == "complete" else None, # 新增:返回OBS URL
|
||||
"error_message": task_info.get("error_message") if status == "failed" else None
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user