From 13dacc36e2cd7166c24f015a000b946981849e10 Mon Sep 17 00:00:00 2001 From: cabbage16 Date: Tue, 6 Aug 2024 00:41:28 +0900 Subject: [PATCH] =?UTF-8?q?perf(#111):=20=EA=B3=B5=EC=A7=80=EC=82=AC?= =?UTF-8?q?=ED=95=AD=20=ED=8C=8C=EC=9D=BC=20=EC=97=85=EB=A1=9C=EB=93=9C=20?= =?UTF-8?q?presigned=20url=20=EC=A0=81=EC=9A=A9=20-=20=EA=B3=B5=EC=A7=80?= =?UTF-8?q?=EC=82=AC=ED=95=AD=20=ED=8C=8C=EC=9D=BC=20=EC=97=85=EB=A1=9C?= =?UTF-8?q?=EB=93=9C=20=EA=B8=B0=EB=8A=A5=EC=97=90=20presigned=20url?= =?UTF-8?q?=EC=9D=84=20=EC=A0=81=EC=9A=A9=ED=96=88=EC=8A=B5=EB=8B=88?= =?UTF-8?q?=EB=8B=A4.=20-=20presigned=20url=EC=9D=84=20=EC=9D=B4=EC=9A=A9?= =?UTF-8?q?=ED=95=98=EB=A9=B4=20=EA=B3=B5=EC=A7=80=EC=82=AC=ED=95=AD?= =?UTF-8?q?=EC=9D=84=20=EC=A1=B0=ED=9A=8C=ED=95=A0=20=EB=95=8C=20=EB=A7=88?= =?UTF-8?q?=EB=8B=A4=20=EC=83=88=EB=A1=9C=EC=9A=B4=20presigned=20url?= =?UTF-8?q?=EC=9D=84=20=EC=9A=94=EC=B2=AD=ED=95=B4=EC=95=BC=ED=95=98?= =?UTF-8?q?=EB=AF=80=EB=A1=9C=20Notice=EC=97=90=EC=84=9C=20fileUrl=20?= =?UTF-8?q?=EC=BB=AC=EB=9F=BC=EC=9D=84=20fileUuid=20=EB=A1=9C=20=EB=8C=80?= =?UTF-8?q?=EC=B2=B4=ED=96=88=EC=8A=B5=EB=8B=88=EB=8B=A4.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../notice/CreateNoticeUseCase.java | 4 ++- .../notice/QueryNoticeUseCase.java | 11 +++++--- .../notice/UpdateNoticeUseCase.java | 4 ++- .../application/notice/UploadFileUseCase.java | 26 +++++++------------ .../maru/domain/notice/domain/Notice.java | 14 +++++----- .../presentation/notice/NoticeController.java | 13 +++++----- .../notice/dto/request/NoticeRequest.java | 6 ++--- .../notice/dto/response/NoticeResponse.java | 4 +-- .../dto/response/UploadFileResponse.java | 17 ++++++++++++ 9 files changed, 60 insertions(+), 39 deletions(-) create mode 100644 src/main/java/com/bamdoliro/maru/presentation/notice/dto/response/UploadFileResponse.java diff --git a/src/main/java/com/bamdoliro/maru/application/notice/CreateNoticeUseCase.java b/src/main/java/com/bamdoliro/maru/application/notice/CreateNoticeUseCase.java index 1945fcc3..7bbb30e1 100644 --- a/src/main/java/com/bamdoliro/maru/application/notice/CreateNoticeUseCase.java +++ b/src/main/java/com/bamdoliro/maru/application/notice/CreateNoticeUseCase.java @@ -7,6 +7,8 @@ import com.bamdoliro.maru.shared.response.IdResponse; import lombok.RequiredArgsConstructor; +import java.util.UUID; + @RequiredArgsConstructor @UseCase public class CreateNoticeUseCase { @@ -15,7 +17,7 @@ public class CreateNoticeUseCase { public IdResponse execute(NoticeRequest request) { Notice notice = noticeRepository.save( - new Notice(request.getTitle(), request.getContent(), request.getFileUrl()) + new Notice(request.getTitle(), request.getContent(), UUID.fromString(request.getFileUuid())) ); return new IdResponse(notice); diff --git a/src/main/java/com/bamdoliro/maru/application/notice/QueryNoticeUseCase.java b/src/main/java/com/bamdoliro/maru/application/notice/QueryNoticeUseCase.java index 7743fff2..d9a38620 100644 --- a/src/main/java/com/bamdoliro/maru/application/notice/QueryNoticeUseCase.java +++ b/src/main/java/com/bamdoliro/maru/application/notice/QueryNoticeUseCase.java @@ -1,5 +1,8 @@ package com.bamdoliro.maru.application.notice; +import com.bamdoliro.maru.domain.notice.domain.Notice; +import com.bamdoliro.maru.infrastructure.s3.FileService; +import com.bamdoliro.maru.infrastructure.s3.constants.FolderConstant; import com.bamdoliro.maru.presentation.notice.dto.response.NoticeResponse; import com.bamdoliro.maru.shared.annotation.UseCase; import lombok.RequiredArgsConstructor; @@ -9,10 +12,12 @@ public class QueryNoticeUseCase { private final NoticeFacade noticeFacade; + private final FileService fileService; public NoticeResponse execute(Long id) { - return new NoticeResponse( - noticeFacade.getNotice(id) - ); + Notice notice = noticeFacade.getNotice(id); + String fileUrl = fileService.getPresignedUrl(FolderConstant.NOTICE_FILE, notice.getFileUuid().toString()).getDownloadUrl(); + + return new NoticeResponse(notice, fileUrl); } } diff --git a/src/main/java/com/bamdoliro/maru/application/notice/UpdateNoticeUseCase.java b/src/main/java/com/bamdoliro/maru/application/notice/UpdateNoticeUseCase.java index d59cc399..1dab47a2 100644 --- a/src/main/java/com/bamdoliro/maru/application/notice/UpdateNoticeUseCase.java +++ b/src/main/java/com/bamdoliro/maru/application/notice/UpdateNoticeUseCase.java @@ -6,6 +6,8 @@ import lombok.RequiredArgsConstructor; import org.springframework.transaction.annotation.Transactional; +import java.util.UUID; + @RequiredArgsConstructor @UseCase public class UpdateNoticeUseCase { @@ -15,6 +17,6 @@ public class UpdateNoticeUseCase { @Transactional public void execute(Long id, NoticeRequest request) { Notice notice = noticeFacade.getNotice(id); - notice.update(request.getTitle(), request.getContent(), request.getFileUrl()); + notice.update(request.getTitle(), request.getContent(), UUID.fromString(request.getFileUuid())); } } diff --git a/src/main/java/com/bamdoliro/maru/application/notice/UploadFileUseCase.java b/src/main/java/com/bamdoliro/maru/application/notice/UploadFileUseCase.java index ea3e41bb..c9911dce 100644 --- a/src/main/java/com/bamdoliro/maru/application/notice/UploadFileUseCase.java +++ b/src/main/java/com/bamdoliro/maru/application/notice/UploadFileUseCase.java @@ -1,31 +1,23 @@ package com.bamdoliro.maru.application.notice; -import com.bamdoliro.maru.domain.notice.domain.Notice; -import com.bamdoliro.maru.domain.user.domain.User; -import com.bamdoliro.maru.infrastructure.s3.UploadFileService; +import com.bamdoliro.maru.infrastructure.s3.FileService; import com.bamdoliro.maru.infrastructure.s3.constants.FolderConstant; -import com.bamdoliro.maru.infrastructure.s3.dto.response.UploadResponse; -import com.bamdoliro.maru.infrastructure.s3.exception.FileSizeLimitExceededException; -import com.bamdoliro.maru.infrastructure.s3.exception.MediaTypeMismatchException; +import com.bamdoliro.maru.infrastructure.s3.dto.response.UrlResponse; +import com.bamdoliro.maru.presentation.notice.dto.response.UploadFileResponse; import com.bamdoliro.maru.shared.annotation.UseCase; import lombok.RequiredArgsConstructor; -import org.springframework.web.multipart.MultipartFile; import java.util.UUID; -import static com.bamdoliro.maru.shared.constants.FileConstant.MB; - @RequiredArgsConstructor @UseCase public class UploadFileUseCase { - private final UploadFileService uploadFileService; + private final FileService fileService; + + public UploadFileResponse execute() { + String uuid = UUID.randomUUID().toString(); - public UploadResponse execute(MultipartFile noticeFile) { - return uploadFileService.execute(noticeFile, FolderConstant.NOTICE_FILE, UUID.randomUUID().toString(), file -> { - if (file.getSize() > 20 * MB) { - throw new FileSizeLimitExceededException(); - } - }); + return new UploadFileResponse(fileService.getPresignedUrl(FolderConstant.NOTICE_FILE, uuid), uuid); } -} +} \ No newline at end of file diff --git a/src/main/java/com/bamdoliro/maru/domain/notice/domain/Notice.java b/src/main/java/com/bamdoliro/maru/domain/notice/domain/Notice.java index b698df53..84bb86ca 100644 --- a/src/main/java/com/bamdoliro/maru/domain/notice/domain/Notice.java +++ b/src/main/java/com/bamdoliro/maru/domain/notice/domain/Notice.java @@ -12,6 +12,8 @@ import lombok.Getter; import lombok.NoArgsConstructor; +import java.util.UUID; + @Getter @NoArgsConstructor(access = AccessLevel.PROTECTED) @Table(name = "tbl_notice") @@ -29,19 +31,19 @@ public class Notice extends BaseTimeEntity { @Column(nullable = false, length = 1024) private String content; - @Column(nullable = true, length = 150) - private String fileUrl; + @Column(unique = true, nullable = true) + private UUID fileUuid; @Builder - public Notice(String title, String content, String fileUrl) { + public Notice(String title, String content, UUID fileUuid) { this.title = title; this.content = content; - this.fileUrl = fileUrl; + this.fileUuid = fileUuid; } - public void update(String title, String content, String fileUrl) { + public void update(String title, String content, UUID fileUuid) { this.title = title; this.content = content; - this.fileUrl = fileUrl; + this.fileUuid = fileUuid; } } diff --git a/src/main/java/com/bamdoliro/maru/presentation/notice/NoticeController.java b/src/main/java/com/bamdoliro/maru/presentation/notice/NoticeController.java index 24e74b11..d8cb474a 100644 --- a/src/main/java/com/bamdoliro/maru/presentation/notice/NoticeController.java +++ b/src/main/java/com/bamdoliro/maru/presentation/notice/NoticeController.java @@ -2,10 +2,11 @@ import com.bamdoliro.maru.application.notice.*; import com.bamdoliro.maru.domain.user.domain.User; -import com.bamdoliro.maru.infrastructure.s3.dto.response.UploadResponse; +import com.bamdoliro.maru.infrastructure.s3.dto.response.UrlResponse; import com.bamdoliro.maru.presentation.notice.dto.request.NoticeRequest; import com.bamdoliro.maru.presentation.notice.dto.response.NoticeResponse; import com.bamdoliro.maru.presentation.notice.dto.response.NoticeSimpleResponse; +import com.bamdoliro.maru.presentation.notice.dto.response.UploadFileResponse; import com.bamdoliro.maru.shared.auth.AuthenticationPrincipal; import com.bamdoliro.maru.shared.auth.Authority; import com.bamdoliro.maru.shared.response.CommonResponse; @@ -16,7 +17,8 @@ import lombok.RequiredArgsConstructor; import org.springframework.http.HttpStatus; import org.springframework.web.bind.annotation.*; -import org.springframework.web.multipart.MultipartFile; + +import java.util.UUID; @RequiredArgsConstructor @RequestMapping("/notice") @@ -42,12 +44,11 @@ public SingleCommonResponse createNotice( @ResponseStatus(HttpStatus.CREATED) @PostMapping("/file") - public SingleCommonResponse uploadFile( - @AuthenticationPrincipal(authority = Authority.ADMIN) User user, - @RequestPart("file") MultipartFile file + public SingleCommonResponse uploadFile( + @AuthenticationPrincipal(authority = Authority.ADMIN) User user ) { return SingleCommonResponse.ok( - uploadFileUseCase.execute(file) + uploadFileUseCase.execute() ); } diff --git a/src/main/java/com/bamdoliro/maru/presentation/notice/dto/request/NoticeRequest.java b/src/main/java/com/bamdoliro/maru/presentation/notice/dto/request/NoticeRequest.java index 1f55d492..a25f3aa1 100644 --- a/src/main/java/com/bamdoliro/maru/presentation/notice/dto/request/NoticeRequest.java +++ b/src/main/java/com/bamdoliro/maru/presentation/notice/dto/request/NoticeRequest.java @@ -7,6 +7,8 @@ import lombok.NoArgsConstructor; import org.hibernate.validator.constraints.URL; +import java.util.UUID; + @Getter @NoArgsConstructor @AllArgsConstructor @@ -20,7 +22,5 @@ public class NoticeRequest { @Size(max = 1024, message = "1024글자 이하여야 합니다.") private String content; - @Size(max = 150, message = "150자 이하여야합니다.") - @URL(message = "올바른 URL 형식이어야 합니다.") - private String fileUrl; + private String fileUuid; } diff --git a/src/main/java/com/bamdoliro/maru/presentation/notice/dto/response/NoticeResponse.java b/src/main/java/com/bamdoliro/maru/presentation/notice/dto/response/NoticeResponse.java index 15b78209..0f4a7c99 100644 --- a/src/main/java/com/bamdoliro/maru/presentation/notice/dto/response/NoticeResponse.java +++ b/src/main/java/com/bamdoliro/maru/presentation/notice/dto/response/NoticeResponse.java @@ -13,10 +13,10 @@ public class NoticeResponse { private final String fileUrl; private final LocalDateTime createdAt; - public NoticeResponse(Notice notice) { + public NoticeResponse(Notice notice, String fileUrl) { this.title = notice.getTitle(); this.content = notice.getContent(); - this.fileUrl = notice.getFileUrl(); + this.fileUrl = fileUrl; this.createdAt = notice.getCreatedAt(); } } diff --git a/src/main/java/com/bamdoliro/maru/presentation/notice/dto/response/UploadFileResponse.java b/src/main/java/com/bamdoliro/maru/presentation/notice/dto/response/UploadFileResponse.java new file mode 100644 index 00000000..82303f49 --- /dev/null +++ b/src/main/java/com/bamdoliro/maru/presentation/notice/dto/response/UploadFileResponse.java @@ -0,0 +1,17 @@ +package com.bamdoliro.maru.presentation.notice.dto.response; + +import com.bamdoliro.maru.infrastructure.s3.dto.response.UrlResponse; +import lombok.Getter; + +@Getter +public class UploadFileResponse { + + private final UrlResponse url; + + private final String fileUuid; + + public UploadFileResponse(UrlResponse url, String fileUuid) { + this.url = url; + this.fileUuid = fileUuid; + } +}