音频文件上传 | AI创作模块兼容音频
This commit is contained in:
@@ -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"),
|
||||
|
||||
Reference in New Issue
Block a user