Skip to content

Commit 08f1926

Browse files
authored
이미지 수정 반영 안됨 해결 (#207)
1 parent ce9d1fe commit 08f1926

File tree

2 files changed

+26
-5
lines changed

2 files changed

+26
-5
lines changed

src/main/java/org/ezcode/codetest/application/problem/dto/response/ProblemDetailResponse.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,14 @@ public record ProblemDetailResponse(
5353
@Schema(description = "문제 이미지 URL", example = "https://bucket.s3.ap-northeast-2.amazonaws.com/problem/example.jpg")
5454
String imageUrl,
5555

56+
@Schema(description = "문제 이미지 파일명", example = "example.jpg")
57+
String imageName,
58+
5659
List<TestcaseResponse> testcases
5760
) {
5861

5962
public static ProblemDetailResponse from(Problem problem, List<Category> categories) {
63+
String imageUrl = problem.getImageUrl().isEmpty() ? null : problem.getImageUrl().get(0);
6064

6165
return ProblemDetailResponse.builder()
6266
.id(problem.getId())
@@ -71,8 +75,17 @@ public static ProblemDetailResponse from(Problem problem, List<Category> categor
7175
.reference(problem.getReference())
7276
.createdAt(problem.getCreatedAt())
7377
.modifiedAt(problem.getModifiedAt())
74-
.imageUrl(problem.getImageUrl().isEmpty() ? null : problem.getImageUrl().get(0))
78+
.imageUrl(imageUrl)
79+
.imageName(extractFileName(imageUrl))
7580
.testcases(problem.top2Testcases().stream().map(TestcaseResponse::from).toList())
7681
.build();
7782
}
83+
84+
private static String extractFileName(String url) {
85+
if (url == null || url.isBlank()) {
86+
return null;
87+
}
88+
int lastSlashIndex = url.lastIndexOf('/');
89+
return lastSlashIndex >= 0 ? url.substring(lastSlashIndex + 1) : url;
90+
}
7891
}

src/main/java/org/ezcode/codetest/application/problem/service/ProblemService.java

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -196,13 +196,21 @@ public void addImageToExistingProblem(Long problemId, MultipartFile imageFile) {
196196

197197
Problem problem = problemDomainService.getProblem(problemId);
198198

199-
// 1. S3 업로드
199+
// 1. 기존 이미지가 있으면 S3에서 삭제
200+
if (!problem.getImageUrl().isEmpty()) {
201+
for (String fileUrl : problem.getImageUrl()) {
202+
s3Uploader.delete(fileUrl, "problem");
203+
}
204+
problem.clearImages();
205+
}
206+
207+
// 2. S3 업로드
200208
String key = s3Uploader.upload(imageFile, "problem");
201209

202-
// 2. 문제에 이미지 연결
203-
problem.addImage(key); // 또는 setImageUrl(List.of(key))
210+
// 3. 문제에 이미지 연결
211+
problem.addImage(key);
204212

205-
// 3. 저장
213+
// 4. 저장
206214
problemDomainService.saveProblem(problem);
207215
}
208216

0 commit comments

Comments
 (0)