From 0a2f0f8216a971630150cf8c01121afcf399eb37 Mon Sep 17 00:00:00 2001 From: yeonjy Date: Sat, 31 May 2025 01:42:47 +0900 Subject: [PATCH] =?UTF-8?q?#134=20fix:=20=EC=9D=B4=EB=AF=B8=EC=A7=80=20?= =?UTF-8?q?=ED=99=95=EC=9E=A5=EC=9E=90=EA=B0=80=20uri=EC=9D=98=20=EB=A7=88?= =?UTF-8?q?=EC=A7=80=EB=A7=89=EC=9D=B4=20=EC=95=84=EB=8B=8C=20=EA=B2=BD?= =?UTF-8?q?=EC=9A=B0=20trim=20=EC=B2=98=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../webeye/backend/global/error/ErrorCode.java | 1 + .../infrastructure/OpenAiClient.java | 18 +++++++++++++++--- .../swagger/ImageAnalysisSwagger.java | 4 ++++ 3 files changed, 20 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/webeye/backend/global/error/ErrorCode.java b/src/main/java/com/webeye/backend/global/error/ErrorCode.java index 4a30a8e..49a5b64 100644 --- a/src/main/java/com/webeye/backend/global/error/ErrorCode.java +++ b/src/main/java/com/webeye/backend/global/error/ErrorCode.java @@ -13,6 +13,7 @@ public enum ErrorCode { FILE_EXTENSION_NOT_FOUND(HttpStatus.BAD_REQUEST, "URL에서 확장자를 찾을 수 없습니다."), UNSUPPORTED_IMAGE_TYPE(HttpStatus.BAD_REQUEST, "지원하지 않는 이미지 형식입니다."), INVALID_IMAGE_URL(HttpStatus.BAD_REQUEST, "잘못된 이미지 URL입니다."), + NO_IMAGE_FILE_EXTENSION_FOUND(HttpStatus.BAD_REQUEST, "이미지 확장자가 없습니다."), // image url extract IMAGE_URL_NOT_FOUND(HttpStatus.NOT_FOUND, "이미지의 URL이 추출되지 않았습니다."), diff --git a/src/main/java/com/webeye/backend/imageanalysis/infrastructure/OpenAiClient.java b/src/main/java/com/webeye/backend/imageanalysis/infrastructure/OpenAiClient.java index 3d2eb1c..4faf91a 100644 --- a/src/main/java/com/webeye/backend/imageanalysis/infrastructure/OpenAiClient.java +++ b/src/main/java/com/webeye/backend/imageanalysis/infrastructure/OpenAiClient.java @@ -27,8 +27,7 @@ import java.net.MalformedURLException; import java.util.List; -import static com.webeye.backend.global.error.ErrorCode.FILE_EXTENSION_NOT_FOUND; -import static com.webeye.backend.global.error.ErrorCode.INVALID_IMAGE_URL; +import static com.webeye.backend.global.error.ErrorCode.*; @Slf4j @Component @@ -202,7 +201,8 @@ public ImageAnalysisResponse explainImage(ImageAnalysisRequest request) { """; ImageAnalysisPrompt prompt = new ImageAnalysisPrompt(system, user); - return callWithStructuredOutput(List.of(request.url()), prompt, ImageAnalysisResponse.class); + + return callWithStructuredOutput(List.of(trimImageUrl(request.url())), prompt, ImageAnalysisResponse.class); } private T callWithStructuredOutput(List urls, ImageAnalysisPrompt prompt, Class clazz) { @@ -237,6 +237,18 @@ private String extractFileExtension(String url) { return fileName.substring(dotIndex + 1); } + private String trimImageUrl(String url) { + for (ImageMimeType type : ImageMimeType.values()) { + String extension = "." + type.name().toLowerCase(); + int index = url.toLowerCase().indexOf(extension); + if (index != -1) { + int endIndex = index + extension.length(); + return url.substring(0, endIndex); + } + } + throw new BusinessException(NO_IMAGE_FILE_EXTENSION_FOUND); + } + public RawMaterialAiResponse explainRawMaterial(FoodProductAnalysisRequest request) { Message systemMessage = new SystemMessage(""" diff --git a/src/main/java/com/webeye/backend/imageanalysis/presentation/swagger/ImageAnalysisSwagger.java b/src/main/java/com/webeye/backend/imageanalysis/presentation/swagger/ImageAnalysisSwagger.java index 0d48894..f9ea9c9 100644 --- a/src/main/java/com/webeye/backend/imageanalysis/presentation/swagger/ImageAnalysisSwagger.java +++ b/src/main/java/com/webeye/backend/imageanalysis/presentation/swagger/ImageAnalysisSwagger.java @@ -19,6 +19,10 @@ public interface ImageAnalysisSwagger { @ApiResponse( responseCode = "200", description = "이미지가 성공적으로 분석되었습니다." + ), + @ApiResponse( + responseCode = "400", + description = "이미지 확장자가 없습니다." ) }) SuccessResponse imageAnalysis(