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을 요청해야하므로 Form 에서 formUrl 컬럼을 삭제했습니다.
  • Loading branch information
cabbage16 committed Aug 9, 2024
1 parent caa7208 commit c55e3a9
Show file tree
Hide file tree
Showing 8 changed files with 27 additions and 37 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.bamdoliro.maru.application.form;

import com.bamdoliro.maru.infrastructure.persistence.form.FormRepository;
import com.bamdoliro.maru.infrastructure.s3.FileService;
import com.bamdoliro.maru.infrastructure.s3.constants.FolderConstant;
import com.bamdoliro.maru.presentation.form.dto.response.FormUrlResponse;
import com.bamdoliro.maru.shared.annotation.UseCase;
import lombok.RequiredArgsConstructor;
Expand All @@ -12,10 +14,14 @@
public class QueryFormUrlUseCase {

private final FormRepository formRepository;
private final FileService fileService;

public List<FormUrlResponse> execute(List<Long> formIdList) {
return formRepository.findFormUrlByFormIdList(formIdList).stream()
.map(FormUrlResponse::new)
.toList();
.map(vo -> new FormUrlResponse(
vo.getExaminationNumber(),
vo.getName(),
fileService.getPresignedUrl(FolderConstant.FORM, vo.getUuid()).getDownloadUrl()
)).toList();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import com.bamdoliro.maru.domain.form.exception.FormAlreadySubmittedException;
import com.bamdoliro.maru.domain.form.service.FormFacade;
import com.bamdoliro.maru.domain.user.domain.User;
import com.bamdoliro.maru.presentation.form.dto.request.SubmitFinalFormRequest;
import com.bamdoliro.maru.infrastructure.s3.FileService;
import com.bamdoliro.maru.shared.annotation.UseCase;
import lombok.RequiredArgsConstructor;
import org.springframework.transaction.annotation.Transactional;
Expand All @@ -14,13 +14,14 @@
public class SubmitFinalFormUseCase {

private final FormFacade formFacade;
private final FileService fileService;

@Transactional
public void execute(User user, SubmitFinalFormRequest request) {
public void execute(User user) {
Form form = formFacade.getForm(user);
validateFormStatus(form);

form.submit(request.getFormUrl());
form.submit();
}

private void validateFormStatus(Form form) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,33 +1,19 @@
package com.bamdoliro.maru.application.form;

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.shared.annotation.UseCase;
import lombok.RequiredArgsConstructor;
import org.springframework.http.MediaType;
import org.springframework.web.multipart.MultipartFile;

import static com.bamdoliro.maru.shared.constants.FileConstant.MB;

@RequiredArgsConstructor
@UseCase
public class UploadFormUseCase {

private final UploadFileService uploadFileService;

public UploadResponse execute(User user, MultipartFile formFile) {
return uploadFileService.execute(formFile, FolderConstant.FORM, user.getUuid().toString(), file -> {
if (file.getContentType() != null && !file.getContentType().equals(MediaType.APPLICATION_PDF_VALUE)) {
throw new MediaTypeMismatchException();
}
private final FileService fileService;

if (file.getSize() > 20 * MB) {
throw new FileSizeLimitExceededException();
}
});
public UrlResponse execute(User user) {
return fileService.getPresignedUrl(FolderConstant.FORM, user.getUuid().toString());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,6 @@ public class Form extends BaseTimeEntity {
@Embedded
private Score score;

@Column(nullable = true, length = 150)
private String formUrl;

@Enumerated(EnumType.STRING)
@Column(nullable = false, length = 30)
private FormType type;
Expand Down Expand Up @@ -92,8 +89,7 @@ public Form(Applicant applicant, Parent parent, Education education, Grade grade
this.status = FormStatus.SUBMITTED;
}

public void submit(String formUrl) {
this.formUrl = formUrl;
public void submit() {
this.status = FormStatus.FINAL_SUBMITTED;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ public List<FormUrlVo> findFormUrlByFormIdList(List<Long> idList) {
.select(new QFormUrlVo(
form.examinationNumber,
form.applicant.name,
form.formUrl
form.user.uuid
))
.from(form)
.where(form.id.in(idList))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,19 @@
import com.querydsl.core.annotations.QueryProjection;
import lombok.Getter;

import java.util.UUID;

@Getter
public class FormUrlVo {

private Long examinationNumber;
private String name;
private String formUrl;
private String uuid;

@QueryProjection
public FormUrlVo(Long examinationNumber, String name, String formUrl) {
public FormUrlVo(Long examinationNumber, String name, UUID uuid) {
this.examinationNumber = examinationNumber;
this.name = name;
this.formUrl = formUrl;
this.uuid = uuid.toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,9 @@ public void submitForm(
@ResponseStatus(HttpStatus.NO_CONTENT)
@PatchMapping
public void submitForm(
@AuthenticationPrincipal(authority = Authority.USER) User user,
@RequestBody @Valid SubmitFinalFormRequest request
@AuthenticationPrincipal(authority = Authority.USER) User user
) {
submitFinalFormUseCase.execute(user, request);
submitFinalFormUseCase.execute(user);
}

@ResponseStatus(HttpStatus.NO_CONTENT)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ public class FormUrlResponse {
public FormUrlResponse(FormUrlVo vo) {
this.examinationNumber = vo.getExaminationNumber();
this.name = vo.getName();
this.formUrl = vo.getFormUrl();
this.formUrl = vo.getUuid();
}
}

0 comments on commit c55e3a9

Please sign in to comment.