Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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이 추출되지 않았습니다."),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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> T callWithStructuredOutput(List<String> urls, ImageAnalysisPrompt prompt, Class<T> clazz) {
Expand Down Expand Up @@ -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);
}

Comment on lines +240 to +251
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

trimImageUrl 메서드 구현을 개선할 수 있습니다.

현재 구현은 기본적으로 작동하지만 몇 가지 개선사항이 있습니다:

  1. 성능 최적화: 모든 ImageMimeType을 순회하는 대신 더 효율적인 방법 사용
  2. 정확성 향상: 현재는 URL 어디서든 확장자를 찾지만, URL의 path 부분에서만 찾는 것이 더 정확
  3. 코드 간소화: 로직을 더 간결하게 작성 가능

다음과 같이 개선을 제안합니다:

 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);
+    try {
+        // URL에서 path 부분만 추출 (쿼리 파라미터 제거)
+        String urlPath = url.split("\\?")[0];
+        
+        // path에서 파일명 추출
+        String fileName = urlPath.substring(urlPath.lastIndexOf('/') + 1);
+        
+        // 확장자 추출
+        int dotIndex = fileName.lastIndexOf('.');
+        if (dotIndex == -1) {
+            throw new BusinessException(NO_IMAGE_FILE_EXTENSION_FOUND);
+        }
+        
+        String extension = fileName.substring(dotIndex + 1).toLowerCase();
+        
+        // ImageMimeType에서 지원하는 확장자인지 확인
+        boolean isValidExtension = Arrays.stream(ImageMimeType.values())
+            .anyMatch(type -> type.name().toLowerCase().equals(extension));
+            
+        if (!isValidExtension) {
+            throw new BusinessException(UNSUPPORTED_IMAGE_TYPE);
+        }
+        
+        // 확장자까지만 포함하는 URL 반환
+        int extensionEndIndex = urlPath.lastIndexOf('.') + extension.length() + 1;
+        return url.substring(0, extensionEndIndex);
+        
+    } catch (Exception e) {
+        if (e instanceof BusinessException) {
+            throw e;
+        }
+        throw new BusinessException(NO_IMAGE_FILE_EXTENSION_FOUND);
+    }
 }

이렇게 하면:

  • URL의 path 부분에서만 확장자를 찾아 더 정확함
  • 쿼리 파라미터가 있어도 올바르게 처리됨
  • 지원하지 않는 확장자에 대해서도 적절한 에러 처리
  • 성능 개선 (순회 대신 직접 확장자 추출)

Committable suggestion skipped: line range outside the PR's diff.

🤖 Prompt for AI Agents
In
src/main/java/com/webeye/backend/imageanalysis/infrastructure/OpenAiClient.java
around lines 240 to 251, improve the trimImageUrl method by extracting the file
extension only from the URL's path segment instead of searching the entire URL
string. Parse the URL to isolate the path, then check if it ends with any
supported ImageMimeType extension. Return the substring up to the extension's
end if found, otherwise throw the BusinessException. This approach enhances
accuracy by ignoring query parameters, improves performance by avoiding full
iteration over all types, and simplifies the code logic.


public RawMaterialAiResponse explainRawMaterial(FoodProductAnalysisRequest request) {
Message systemMessage = new SystemMessage("""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ public interface ImageAnalysisSwagger {
@ApiResponse(
responseCode = "200",
description = "이미지가 성공적으로 분석되었습니다."
),
@ApiResponse(
responseCode = "400",
description = "이미지 확장자가 없습니다."
)
})
SuccessResponse<ImageAnalysisResponse> imageAnalysis(
Expand Down