音频文件上传 | AI创作模块兼容音频

This commit is contained in:
2026-07-03 11:44:32 +08:00
parent 899732aa54
commit 28b1d24173
14 changed files with 209 additions and 8 deletions
+43
View File
@@ -47,6 +47,11 @@ from app.services.generation_refund_service import mark_generation_record_failed
from app.services.media_token_usage_snapshot_service import sync_generation_record_media_token_snapshot
from app.services.credit_record_meta_service import build_generation_record_prompt_meta
from app.services.video_cover_service import async_create_video_cover_for_local_video
from app.enums.audio_reference import (
AUDIO_ALLOWED_EXTENSIONS,
AUDIO_ALLOWED_MIME_TYPES,
AUDIO_MAX_FILE_SIZE_MB,
)
from app.utils.id_gen import generate_id
from app.utils.exceptions import InsufficientCreditsError, RecordNotFoundError, InvalidStatusError
@@ -832,6 +837,44 @@ async def upload_video(
return {"url": url, "filename": file.filename or safe_name, "type": "video"}
@router.post("/upload-audio")
async def upload_audio(
file: UploadFile = File(...),
current_user: User = Depends(get_current_user),
):
"""Upload an audio file for AI creation reference."""
import os
import uuid
from app.config import settings
from datetime import datetime
ext = os.path.splitext(file.filename or "")[1].lower().lstrip(".")
if ext not in AUDIO_ALLOWED_EXTENSIONS:
raise HTTPException(status_code=400, detail="仅支持 mp3、wav 音频文件")
expected_mime = AUDIO_ALLOWED_MIME_TYPES.get(ext)
if not file.content_type or file.content_type != expected_mime:
raise HTTPException(status_code=400, detail=f"音频 MIME 类型错误,{ext} 必须为 {expected_mime}")
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
safe_name = f"audio_ref_{current_user.id}_{timestamp}_{uuid.uuid4().hex[:8]}.{ext}"
date_dir = datetime.now().strftime("%Y/%m/%d")
dir_path = os.path.join(settings.UPLOAD_LOCAL_PATH, "audios", date_dir)
os.makedirs(dir_path, exist_ok=True)
file_path = os.path.join(dir_path, safe_name)
content = await file.read()
max_bytes = AUDIO_MAX_FILE_SIZE_MB * 1024 * 1024
if len(content) > max_bytes:
raise HTTPException(status_code=400, detail=f"音频大小不能超过{AUDIO_MAX_FILE_SIZE_MB}MB")
with open(file_path, "wb") as f:
f.write(content)
url = f"/uploads/audios/{date_dir}/{safe_name}"
return {"url": url, "filename": file.filename or safe_name, "type": "audio"}
@router.post("/delete-file")
async def delete_upload(
url: str = Query(..., description="文件URL,如 /uploads/images/2024/01/01/video_img_xxx.png"),
@@ -51,6 +51,7 @@ async def list_active_engines(
"supported_durations": durations,
"max_image_count": e.max_image_count,
"max_video_count": e.max_video_count,
"max_audio_count": e.max_audio_count,
"supports_first_last_frame": e.supports_first_last_frame,
"supports_universal_reference": e.supports_universal_reference,
})