Co-authored-by: TakahashiNg <83152264+TakahashiNguyen@users.noreply.github.com> Reviewed-on: #49
This commit was merged in pull request #49.
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
import uuid
|
||||
import io
|
||||
from fastapi import FastAPI, Request, HTTPException
|
||||
from fastapi import FastAPI, File, UploadFile, HTTPException
|
||||
from fastapi.responses import FileResponse
|
||||
from pathlib import Path
|
||||
from PIL import Image
|
||||
@@ -13,18 +13,12 @@ STORAGE_DIR.mkdir(exist_ok=True)
|
||||
|
||||
|
||||
@app.post("/")
|
||||
async def upload_image(request: Request):
|
||||
body = await request.body()
|
||||
|
||||
if not body:
|
||||
raise HTTPException(status_code=400, detail="Dữ liệu trống")
|
||||
async def upload_image(file: UploadFile = File(...)):
|
||||
body = await file.read()
|
||||
|
||||
kind = filetype.guess(body)
|
||||
if kind is None or not kind.mime.startswith("image/"):
|
||||
mime_type = kind.mime if kind else "không xác định"
|
||||
raise HTTPException(
|
||||
status_code=400, detail=f"Định dạng {mime_type} không được hỗ trợ"
|
||||
)
|
||||
if not kind or not kind.mime.startswith("image/"):
|
||||
raise HTTPException(status_code=400, detail="Chỉ chấp nhận file ảnh.")
|
||||
|
||||
try:
|
||||
img = Image.open(io.BytesIO(body))
|
||||
@@ -35,9 +29,7 @@ async def upload_image(request: Request):
|
||||
img.save(file_path, format="WEBP", quality=80)
|
||||
|
||||
return {
|
||||
"status": "success",
|
||||
"filename": random_filename,
|
||||
"original_type": kind.mime,
|
||||
}
|
||||
|
||||
except Exception as e:
|
||||
|
||||
Reference in New Issue
Block a user