feat: 更新上传文件接口
This commit is contained in:
@@ -1,10 +1,9 @@
|
|||||||
from fastapi import APIRouter, Query, UploadFile, File
|
from fastapi import APIRouter, UploadFile, File, Form
|
||||||
from service import file_service
|
from service import file_service
|
||||||
|
|
||||||
router = APIRouter(prefix="/file", tags=["文件"])
|
router = APIRouter(prefix="/file", tags=["文件"])
|
||||||
|
|
||||||
|
|
||||||
@router.post("/upload", summary="上传文件", response_model=str)
|
@router.post("/upload", summary="上传文件", response_model=str)
|
||||||
async def upload(md5: str = Query(..., description="文件MD5值"),
|
async def upload(file: UploadFile = File(..., description="要上传的文件")):
|
||||||
file: UploadFile = File(..., description="要上传的文件")):
|
return await file_service.upload_file(file)
|
||||||
return await file_service.upload_file(md5, file)
|
|
||||||
|
|||||||
@@ -1,3 +1,5 @@
|
|||||||
|
import uuid
|
||||||
|
|
||||||
from fastapi import UploadFile, File, HTTPException
|
from fastapi import UploadFile, File, HTTPException
|
||||||
|
|
||||||
from config.rustfs import s3
|
from config.rustfs import s3
|
||||||
@@ -23,7 +25,7 @@ BUCKET = 'family'
|
|||||||
NGINX_PROXY = 'rustfs'
|
NGINX_PROXY = 'rustfs'
|
||||||
|
|
||||||
|
|
||||||
async def upload_file(md5: str, file: UploadFile = File(...)) -> str:
|
async def upload_file(file: UploadFile = File(...)) -> str:
|
||||||
# 校验文件类型
|
# 校验文件类型
|
||||||
if file.content_type not in ALLOWED_IMAGE_TYPES:
|
if file.content_type not in ALLOWED_IMAGE_TYPES:
|
||||||
raise HTTPException(
|
raise HTTPException(
|
||||||
@@ -32,7 +34,7 @@ async def upload_file(md5: str, file: UploadFile = File(...)) -> str:
|
|||||||
)
|
)
|
||||||
|
|
||||||
file_ext = file.filename.split('.')[-1]
|
file_ext = file.filename.split('.')[-1]
|
||||||
unique_filename = f"{md5}.{file_ext}"
|
unique_filename = f"{uuid.uuid4().hex}.{file_ext}"
|
||||||
|
|
||||||
file_content = await file.read()
|
file_content = await file.read()
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user