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; + } +}