Skip to content

Commit

Permalink
perf(#111): 공지사항 파일 업로드 presigned url 적용
Browse files Browse the repository at this point in the history
- 공지사항 파일 업로드 기능에 presigned url을 적용했습니다.
- presigned url을 이용하면 공지사항을 조회할 때 마다 새로운 presigned url을 요청해야하므로 Notice에서 fileUrl 컬럼을 fileUuid 로 대체했습니다.
  • Loading branch information
cabbage16 committed Aug 5, 2024
1 parent f55dd93 commit 13dacc3
Show file tree
Hide file tree
Showing 9 changed files with 60 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import com.bamdoliro.maru.shared.response.IdResponse;
import lombok.RequiredArgsConstructor;

import java.util.UUID;

@RequiredArgsConstructor
@UseCase
public class CreateNoticeUseCase {
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import lombok.RequiredArgsConstructor;
import org.springframework.transaction.annotation.Transactional;

import java.util.UUID;

@RequiredArgsConstructor
@UseCase
public class UpdateNoticeUseCase {
Expand All @@ -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()));
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
import lombok.Getter;
import lombok.NoArgsConstructor;

import java.util.UUID;

@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@Table(name = "tbl_notice")
Expand All @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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")
Expand All @@ -42,12 +44,11 @@ public SingleCommonResponse<IdResponse> createNotice(

@ResponseStatus(HttpStatus.CREATED)
@PostMapping("/file")
public SingleCommonResponse<UploadResponse> uploadFile(
@AuthenticationPrincipal(authority = Authority.ADMIN) User user,
@RequestPart("file") MultipartFile file
public SingleCommonResponse<UploadFileResponse> uploadFile(
@AuthenticationPrincipal(authority = Authority.ADMIN) User user
) {
return SingleCommonResponse.ok(
uploadFileUseCase.execute(file)
uploadFileUseCase.execute()
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import lombok.NoArgsConstructor;
import org.hibernate.validator.constraints.URL;

import java.util.UUID;

@Getter
@NoArgsConstructor
@AllArgsConstructor
Expand All @@ -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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
}

0 comments on commit 13dacc3

Please sign in to comment.