diff --git a/src/main/java/org/ezcode/codetest/application/problem/dto/response/ProblemDetailResponse.java b/src/main/java/org/ezcode/codetest/application/problem/dto/response/ProblemDetailResponse.java index a51e298d..1590f6e7 100644 --- a/src/main/java/org/ezcode/codetest/application/problem/dto/response/ProblemDetailResponse.java +++ b/src/main/java/org/ezcode/codetest/application/problem/dto/response/ProblemDetailResponse.java @@ -53,10 +53,14 @@ public record ProblemDetailResponse( @Schema(description = "문제 이미지 URL", example = "https://bucket.s3.ap-northeast-2.amazonaws.com/problem/example.jpg") String imageUrl, + @Schema(description = "문제 이미지 파일명", example = "example.jpg") + String imageName, + List testcases ) { public static ProblemDetailResponse from(Problem problem, List categories) { + String imageUrl = problem.getImageUrl().isEmpty() ? null : problem.getImageUrl().get(0); return ProblemDetailResponse.builder() .id(problem.getId()) @@ -71,8 +75,17 @@ public static ProblemDetailResponse from(Problem problem, List categor .reference(problem.getReference()) .createdAt(problem.getCreatedAt()) .modifiedAt(problem.getModifiedAt()) - .imageUrl(problem.getImageUrl().isEmpty() ? null : problem.getImageUrl().get(0)) + .imageUrl(imageUrl) + .imageName(extractFileName(imageUrl)) .testcases(problem.top2Testcases().stream().map(TestcaseResponse::from).toList()) .build(); } + + private static String extractFileName(String url) { + if (url == null || url.isBlank()) { + return null; + } + int lastSlashIndex = url.lastIndexOf('/'); + return lastSlashIndex >= 0 ? url.substring(lastSlashIndex + 1) : url; + } } diff --git a/src/main/java/org/ezcode/codetest/application/problem/service/ProblemService.java b/src/main/java/org/ezcode/codetest/application/problem/service/ProblemService.java index 008872a9..23d0e84f 100644 --- a/src/main/java/org/ezcode/codetest/application/problem/service/ProblemService.java +++ b/src/main/java/org/ezcode/codetest/application/problem/service/ProblemService.java @@ -196,13 +196,21 @@ public void addImageToExistingProblem(Long problemId, MultipartFile imageFile) { Problem problem = problemDomainService.getProblem(problemId); - // 1. S3 업로드 + // 1. 기존 이미지가 있으면 S3에서 삭제 + if (!problem.getImageUrl().isEmpty()) { + for (String fileUrl : problem.getImageUrl()) { + s3Uploader.delete(fileUrl, "problem"); + } + problem.clearImages(); + } + + // 2. S3 업로드 String key = s3Uploader.upload(imageFile, "problem"); - // 2. 문제에 이미지 연결 - problem.addImage(key); // 또는 setImageUrl(List.of(key)) + // 3. 문제에 이미지 연결 + problem.addImage(key); - // 3. 저장 + // 4. 저장 problemDomainService.saveProblem(problem); }