增加图片上传接口

main
mula.liu 2025-08-06 15:07:02 +08:00
parent dec66770f3
commit 1589fccfd8
5 changed files with 57 additions and 1 deletions

View File

@ -2,7 +2,7 @@
from fastapi import APIRouter, HTTPException, UploadFile, File, Form from fastapi import APIRouter, HTTPException, UploadFile, File, Form
from app.models.models import Meeting, TranscriptSegment, CreateMeetingRequest, UpdateMeetingRequest from app.models.models import Meeting, TranscriptSegment, CreateMeetingRequest, UpdateMeetingRequest
from app.core.database import get_db_connection from app.core.database import get_db_connection
from app.core.config import UPLOAD_DIR, AUDIO_DIR, ALLOWED_EXTENSIONS, MAX_FILE_SIZE from app.core.config import UPLOAD_DIR, AUDIO_DIR, MARKDOWN_DIR, ALLOWED_EXTENSIONS, ALLOWED_IMAGE_EXTENSIONS, MAX_FILE_SIZE, MAX_IMAGE_SIZE
from typing import Optional from typing import Optional
import os import os
import uuid import uuid
@ -392,3 +392,55 @@ def get_audio_file(meeting_id: int):
"file_size": audio_file['file_size'], "file_size": audio_file['file_size'],
"upload_time": audio_file['upload_time'] "upload_time": audio_file['upload_time']
} }
@router.post("/meetings/{meeting_id}/upload-image")
async def upload_image(
meeting_id: int,
image_file: UploadFile = File(...)
):
# Validate file extension
file_extension = os.path.splitext(image_file.filename)[1].lower()
if file_extension not in ALLOWED_IMAGE_EXTENSIONS:
raise HTTPException(
status_code=400,
detail=f"Unsupported image type. Allowed types: {', '.join(ALLOWED_IMAGE_EXTENSIONS)}"
)
# Check file size
if image_file.size > MAX_IMAGE_SIZE:
raise HTTPException(
status_code=400,
detail="Image size exceeds 10MB limit"
)
# Check if meeting exists
with get_db_connection() as connection:
cursor = connection.cursor(dictionary=True)
cursor.execute("SELECT meeting_id FROM meetings WHERE meeting_id = %s", (meeting_id,))
if not cursor.fetchone():
raise HTTPException(status_code=404, detail="Meeting not found")
# Create meeting-specific directory
meeting_dir = MARKDOWN_DIR / str(meeting_id)
meeting_dir.mkdir(exist_ok=True)
# Generate unique filename
unique_filename = f"{uuid.uuid4()}{file_extension}"
file_path = meeting_dir / unique_filename
# Store relative path for URL access
relative_path = f"markdown/{meeting_id}/{unique_filename}"
# Save file
try:
with open(file_path, "wb") as buffer:
shutil.copyfileobj(image_file.file, buffer)
except Exception as e:
raise HTTPException(status_code=500, detail=f"Failed to save image: {str(e)}")
return {
"message": "Image uploaded successfully",
"file_name": image_file.filename,
"file_path": relative_path,
"url": f"/uploads/{relative_path}"
}

View File

@ -5,14 +5,18 @@ from pathlib import Path
BASE_DIR = Path(__file__).parent.parent.parent BASE_DIR = Path(__file__).parent.parent.parent
UPLOAD_DIR = BASE_DIR / "uploads" UPLOAD_DIR = BASE_DIR / "uploads"
AUDIO_DIR = UPLOAD_DIR / "audio" AUDIO_DIR = UPLOAD_DIR / "audio"
MARKDOWN_DIR = UPLOAD_DIR / "markdown"
# 文件上传配置 # 文件上传配置
ALLOWED_EXTENSIONS = {".mp3", ".wav", ".m4a", ".mpeg", ".mp4"} ALLOWED_EXTENSIONS = {".mp3", ".wav", ".m4a", ".mpeg", ".mp4"}
ALLOWED_IMAGE_EXTENSIONS = {".jpg", ".jpeg", ".png", ".gif", ".webp"}
MAX_FILE_SIZE = 100 * 1024 * 1024 # 100MB MAX_FILE_SIZE = 100 * 1024 * 1024 # 100MB
MAX_IMAGE_SIZE = 10 * 1024 * 1024 # 10MB
# 确保上传目录存在 # 确保上传目录存在
UPLOAD_DIR.mkdir(exist_ok=True) UPLOAD_DIR.mkdir(exist_ok=True)
AUDIO_DIR.mkdir(exist_ok=True) AUDIO_DIR.mkdir(exist_ok=True)
MARKDOWN_DIR.mkdir(exist_ok=True)
# 数据库配置 # 数据库配置
DATABASE_CONFIG = { DATABASE_CONFIG = {

BIN
uploads/.DS_Store vendored

Binary file not shown.

Binary file not shown.

After

Width:  |  Height:  |  Size: 9.7 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 251 KiB