diff --git a/.devcontainer/devcontainer.json b/.devcontainer/devcontainer.json index e44a899..b76a75a 100644 --- a/.devcontainer/devcontainer.json +++ b/.devcontainer/devcontainer.json @@ -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" ] } }, diff --git a/file-service/python/main.py b/file-service/python/main.py index a7e6abc..b247c75 100644 --- a/file-service/python/main.py +++ b/file-service/python/main.py @@ -1,49 +1,52 @@ 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") - 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}") + if not body: + raise HTTPException(status_code=400, detail="Dữ liệu trống") + + 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): file_path = STORAGE_DIR / name if not file_path.exists(): raise HTTPException(status_code=404, detail="NotFoundImage") - return FileResponse(file_path) \ No newline at end of file + return FileResponse(file_path) diff --git a/file-service/python/requirements.txt b/file-service/python/requirements.txt index 239d8f6..357be8c 100644 --- a/file-service/python/requirements.txt +++ b/file-service/python/requirements.txt @@ -1,5 +1,5 @@ fastapi uvicorn python-multipart -python-magic -Pillow \ No newline at end of file +Pillow +filetype \ No newline at end of file diff --git a/gateway-service/src/main/java/com/drinkool/controller/FileController.java b/gateway-service/src/main/java/com/drinkool/controller/FileController.java index d87ef15..7e61b1c 100644 --- a/gateway-service/src/main/java/com/drinkool/controller/FileController.java +++ b/gateway-service/src/main/java/com/drinkool/controller/FileController.java @@ -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 upload(@RestForm File file) { - return fileService.upload(file); + public Uni upload(Buffer body) { + return fileService.upload(body); } @GET diff --git a/gateway-service/src/main/java/com/drinkool/services/FileService.java b/gateway-service/src/main/java/com/drinkool/services/FileService.java index 6b2f9d5..d32d8a5 100644 --- a/gateway-service/src/main/java/com/drinkool/services/FileService.java +++ b/gateway-service/src/main/java/com/drinkool/services/FileService.java @@ -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 upload(@RestForm File file); + Uni upload(Buffer body); @GET @Path("/{name}")