diff --git a/src/main/java/com/hyundai/softeer/backend/domain/lottery/drawing/controller/PreviewController.java b/src/main/java/com/hyundai/softeer/backend/domain/lottery/drawing/controller/PreviewController.java index d389cff8..e8358d84 100644 --- a/src/main/java/com/hyundai/softeer/backend/domain/lottery/drawing/controller/PreviewController.java +++ b/src/main/java/com/hyundai/softeer/backend/domain/lottery/drawing/controller/PreviewController.java @@ -11,12 +11,11 @@ import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; -import org.springframework.validation.annotation.Validated; import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; @Controller -@RequestMapping("/api/v1/lottery/drawing") @RequiredArgsConstructor public class PreviewController { @@ -40,18 +39,18 @@ public class PreviewController { @ApiResponse(responseCode = "200", description = "html 렌더링 성공 시", useReturnTypeSchema = true), @ApiResponse(responseCode = "400", description = "이벤트를 참여하지 않은 sharedUrl이 들어올 경우", useReturnTypeSchema = true) }) - @GetMapping("/preview") + @GetMapping("/{sharedUrl}") public String preview( - @Validated PreviewRequest previewRequest, + @PathVariable String sharedUrl, Model model ) { - EventUser eventUser = previewService.preview(previewRequest, model); + EventUser eventUser = previewService.preview(sharedUrl, model); model.addAttribute("resultImgUrl", eventUser.getResultImgUrl()); model.addAttribute("name", eventUser.getUser().getName()); model.addAttribute("apiUrl", baseUrl + "/api/v1"); - model.addAttribute("ogUrl", baseUrl + "/api/v1/lottery/drawing/preview?sharedUrl=" + previewRequest.getSharedUrl()); - model.addAttribute("sharedUrl", previewRequest.getSharedUrl()); + model.addAttribute("ogUrl", baseUrl + "/" + sharedUrl); + model.addAttribute("sharedUrl", sharedUrl); return "preview"; } diff --git a/src/main/java/com/hyundai/softeer/backend/domain/lottery/drawing/service/DrawingLotteryService.java b/src/main/java/com/hyundai/softeer/backend/domain/lottery/drawing/service/DrawingLotteryService.java index f892b2ff..52800758 100644 --- a/src/main/java/com/hyundai/softeer/backend/domain/lottery/drawing/service/DrawingLotteryService.java +++ b/src/main/java/com/hyundai/softeer/backend/domain/lottery/drawing/service/DrawingLotteryService.java @@ -57,6 +57,7 @@ public class DrawingLotteryService implements LotteryService { private final Random random = new Random(); public static final int RANK_COUNT = 20; + public static final String BUCKET_DIR = "preview/"; @Value("${properties.event-id}") private Long eventId; @@ -237,18 +238,24 @@ public void saveDrawImage(MultipartFile file, Long eventId, User authenticatedUs EventUser eventUser = eventUserRepository.findByUserIdAndSubEventId(authenticatedUser.getId(), subEvent.getId()) .orElseThrow(() -> new EventUserNotFoundException()); + String resultImgUrl = eventUser.getResultImgUrl(); + + if(resultImgUrl != null) { + amazonS3Client.deleteObject(bucket, BUCKET_DIR + resultImgUrl); + } + String fileName = createFileName(file.getOriginalFilename()); ObjectMetadata metadata = new ObjectMetadata(); metadata.setContentLength(file.getSize()); metadata.setContentType(file.getContentType()); try { - amazonS3Client.putObject(bucket, fileName, file.getInputStream(), metadata); + amazonS3Client.putObject(bucket, BUCKET_DIR + fileName, file.getInputStream(), metadata); } catch (IOException e) { throw new RuntimeException(e); } - String fileUrl = amazonS3Client.getUrl(bucket, fileName).toString(); + String fileUrl = amazonS3Client.getUrl(bucket, BUCKET_DIR + fileName).toString(); eventUser.setResultImgUrl(fileUrl); } diff --git a/src/main/java/com/hyundai/softeer/backend/domain/lottery/drawing/service/PreviewService.java b/src/main/java/com/hyundai/softeer/backend/domain/lottery/drawing/service/PreviewService.java index ca543e7f..be5f9bd6 100644 --- a/src/main/java/com/hyundai/softeer/backend/domain/lottery/drawing/service/PreviewService.java +++ b/src/main/java/com/hyundai/softeer/backend/domain/lottery/drawing/service/PreviewService.java @@ -15,9 +15,7 @@ public class PreviewService { private final EventUserRepository eventUserRepository; - public EventUser preview(PreviewRequest previewRequest, Model model) { - String sharedUrl = previewRequest.getSharedUrl(); - + public EventUser preview(String sharedUrl, Model model) { EventUser eventUser = eventUserRepository.findBySharedUrl(sharedUrl) .orElseThrow(() -> new EventUserNotFoundException());