fix: add upload images #50
@@ -23,7 +23,9 @@
|
||||
"esbenp.prettier-vscode",
|
||||
"PeterSchmalfeldt.explorer-exclude",
|
||||
"redhat.java",
|
||||
"ryanluker.vscode-coverage-gutters"
|
||||
"ryanluker.vscode-coverage-gutters",
|
||||
"ms-python.black-formatter",
|
||||
"ms-python.python"
|
||||
]
|
||||
}
|
||||
},
|
||||
|
||||
+17
-14
@@ -1,45 +1,48 @@
|
||||
import uuid
|
||||
import io
|
||||
import magic
|
||||
from fastapi import FastAPI, Request, HTTPException
|
||||
from fastapi.responses import FileResponse
|
||||
from pathlib import Path
|
||||
from PIL import Image
|
||||
import filetype
|
||||
|
||||
app = FastAPI()
|
||||
|
||||
STORAGE_DIR = Path("images")
|
||||
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="Body trống")
|
||||
raise HTTPException(status_code=400, detail="Dữ liệu trống")
|
||||
|
||||
mime = magic.Magic(mime=True)
|
||||
content_type = mime.from_buffer(body)
|
||||
|
||||
if not content_type.startswith("image/"):
|
||||
raise HTTPException(status_code=400, detail=f"Định dạng không hỗ trợ: {content_type}")
|
||||
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ợ"
|
||||
)
|
||||
|
||||
try:
|
||||
img_data = io.BytesIO(body)
|
||||
img = Image.open(img_data)
|
||||
img = Image.open(io.BytesIO(body))
|
||||
|
||||
random_filename = f"{uuid.uuid4()}.webp"
|
||||
file_path = STORAGE_DIR / random_filename
|
||||
|
||||
img.save(file_path, format="WEBP", quality=80)
|
||||
|
||||
return {
|
||||
"status": "success",
|
||||
"filename": random_filename,
|
||||
"original_type": kind.mime,
|
||||
}
|
||||
|
||||
except Exception as e:
|
||||
raise HTTPException(status_code=500, detail=f"Lỗi xử lý ảnh: {str(e)}")
|
||||
|
||||
return {
|
||||
"status": "success",
|
||||
"original_format": content_type,
|
||||
"filename": random_filename
|
||||
}
|
||||
|
||||
@app.get("/{name}")
|
||||
async def get_image(name: str):
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
fastapi
|
||||
uvicorn
|
||||
python-multipart
|
||||
python-magic
|
||||
Pillow
|
||||
filetype
|
||||
@@ -2,15 +2,14 @@ package com.drinkool.controller;
|
||||
|
||||
import com.drinkool.services.FileService;
|
||||
import io.smallrye.mutiny.Uni;
|
||||
import io.vertx.mutiny.core.buffer.Buffer;
|
||||
import jakarta.inject.Inject;
|
||||
import jakarta.ws.rs.GET;
|
||||
import jakarta.ws.rs.POST;
|
||||
import jakarta.ws.rs.Path;
|
||||
import jakarta.ws.rs.PathParam;
|
||||
import jakarta.ws.rs.core.Response;
|
||||
import java.io.File;
|
||||
import org.eclipse.microprofile.rest.client.inject.RestClient;
|
||||
import org.jboss.resteasy.reactive.RestForm;
|
||||
|
||||
@Path("/api/file")
|
||||
public class FileController {
|
||||
@@ -20,8 +19,8 @@ public class FileController {
|
||||
FileService fileService;
|
||||
|
||||
@POST
|
||||
public Uni<Response> upload(@RestForm File file) {
|
||||
return fileService.upload(file);
|
||||
public Uni<Response> upload(Buffer body) {
|
||||
return fileService.upload(body);
|
||||
}
|
||||
|
||||
@GET
|
||||
|
||||
@@ -1,20 +1,19 @@
|
||||
package com.drinkool.services;
|
||||
|
||||
import io.smallrye.mutiny.Uni;
|
||||
import io.vertx.mutiny.core.buffer.Buffer;
|
||||
import jakarta.ws.rs.GET;
|
||||
import jakarta.ws.rs.POST;
|
||||
import jakarta.ws.rs.Path;
|
||||
import jakarta.ws.rs.PathParam;
|
||||
import jakarta.ws.rs.core.Response;
|
||||
import java.io.File;
|
||||
import org.eclipse.microprofile.rest.client.inject.RegisterRestClient;
|
||||
import org.jboss.resteasy.reactive.RestForm;
|
||||
|
||||
@RegisterRestClient(configKey = "file")
|
||||
@Path("/")
|
||||
public interface FileService {
|
||||
@POST
|
||||
Uni<Response> upload(@RestForm File file);
|
||||
Uni<Response> upload(Buffer body);
|
||||
|
||||
@GET
|
||||
@Path("/{name}")
|
||||
|
||||
Reference in New Issue
Block a user