-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ [STMT-179] 이미지 업로드를 위한 Presigned URL 생성 기능 추가 (#120)
* ✨ [STMT-179] 이미지 업로드를 위한 Presigned URL 생성 기능 추가 * ✨ [STMT-179] 파일 이름으로 이미지 유효성을 체크하는 기능 추가 * ✅ [STMT-179] 테스트시 동작하는 S3Presigner 객체 추가 * ♻️ [STMT-179] 파일의 유효성 체크를 유즈케이스에서 진행하도록 변경 * ✨ [STMT-179] 파일이름으로만 파일 유효성을 검증하는 기능 추가 * ✅ [STMT-179] Presigned url 경로 생성 기능 테스트 코드 및 문서화 진행 * ♻️ [STMT-179] 이미지 유효 시간을 환경변수로 관리하도록 변경 * ♻️ [STMT-179] 파일 경로를 별도의 Enum 클래스로 관리하도록 변경
- Loading branch information
Showing
22 changed files
with
538 additions
and
132 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
.path로 전달할 수 있는 값 | ||
|
||
PresigendUrl 발급에 사용되는 path 필드는 어떤 기능에 대한 이미지인지를 명시해야합니다. | ||
|
||
현재 지원 가능한 path 값은 다음과 같습니다. | ||
|=== | ||
| 값 | 설명 | ||
|
||
| ACTIVITY | 스터디 활동 관련 이미지 | ||
|=== | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
88 changes: 49 additions & 39 deletions
88
src/main/java/com/stumeet/server/common/util/FileValidator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,51 +1,61 @@ | ||
package com.stumeet.server.common.util; | ||
|
||
import org.springframework.web.multipart.MultipartFile; | ||
|
||
import com.stumeet.server.common.response.ErrorCode; | ||
import com.stumeet.server.file.domain.ImageContentType; | ||
import com.stumeet.server.file.domain.exception.InvalidFileException; | ||
|
||
import lombok.AccessLevel; | ||
import lombok.NoArgsConstructor; | ||
import org.springframework.web.multipart.MultipartFile; | ||
|
||
@NoArgsConstructor(access = AccessLevel.PRIVATE) | ||
public class FileValidator { | ||
|
||
public static boolean isValidImageFile(MultipartFile file) { | ||
String fileName = file.getOriginalFilename(); | ||
String contentType = file.getContentType(); | ||
String extension = FileUtil.extractExtension(fileName); | ||
|
||
return isValidFileName(fileName) && isValidFileContentType(contentType, extension); | ||
} | ||
|
||
public static void validateImageFile(MultipartFile file) { | ||
String fileName = file.getOriginalFilename(); | ||
String contentType = file.getContentType(); | ||
String extension = FileUtil.extractExtension(fileName); | ||
|
||
validateFileName(fileName); | ||
validateImageContentType(contentType, extension); | ||
} | ||
|
||
public static void validateFileName(String fileName) { | ||
if (!isValidFileName(fileName)) { | ||
throw new InvalidFileException(ErrorCode.INVALID_FILE_NAME_EXCEPTION); | ||
} | ||
} | ||
|
||
public static void validateImageContentType(String contentType, String extension) { | ||
if (!isValidFileContentType(contentType, extension)) { | ||
throw new InvalidFileException(ErrorCode.INVALID_FILE_CONTENT_TYPE_EXCEPTION); | ||
} | ||
} | ||
|
||
private static boolean isValidFileName(String fileName) { | ||
return fileName != null && fileName.contains("."); | ||
} | ||
|
||
private static boolean isValidFileContentType(String contentType, String extension) { | ||
return contentType != null && ImageContentType.exists(contentType, extension); | ||
} | ||
public static boolean isValidImageFile(MultipartFile file) { | ||
String fileName = file.getOriginalFilename(); | ||
String contentType = file.getContentType(); | ||
String extension = FileUtil.extractExtension(fileName); | ||
|
||
return isValidFileName(fileName) && isValidFileContentType(contentType, extension); | ||
} | ||
|
||
public static void validateImageFile(String fileName) { | ||
String extension = FileUtil.extractExtension(fileName); | ||
|
||
validateFileName(fileName); | ||
validateImageExtension(extension); | ||
} | ||
|
||
public static void validateImageFile(MultipartFile file) { | ||
String fileName = file.getOriginalFilename(); | ||
String contentType = file.getContentType(); | ||
String extension = FileUtil.extractExtension(fileName); | ||
|
||
validateFileName(fileName); | ||
validateImageContentType(contentType, extension); | ||
} | ||
|
||
public static void validateFileName(String fileName) { | ||
if (!isValidFileName(fileName)) { | ||
throw new InvalidFileException(ErrorCode.INVALID_FILE_NAME_EXCEPTION); | ||
} | ||
} | ||
|
||
public static void validateImageContentType(String contentType, String extension) { | ||
if (!isValidFileContentType(contentType, extension)) { | ||
throw new InvalidFileException(ErrorCode.INVALID_FILE_CONTENT_TYPE_EXCEPTION); | ||
} | ||
} | ||
private static void validateImageExtension(String extension) { | ||
if (!ImageContentType.exists(extension)) { | ||
throw new InvalidFileException(ErrorCode.INVALID_FILE_EXTENSION_EXCEPTION); | ||
} | ||
} | ||
|
||
private static boolean isValidFileName(String fileName) { | ||
return fileName != null && fileName.contains("."); | ||
} | ||
|
||
private static boolean isValidFileContentType(String contentType, String extension) { | ||
return contentType != null && ImageContentType.exists(contentType, extension); | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
src/main/java/com/stumeet/server/file/adapter/in/PresignedUrlGenerateWebAdapter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package com.stumeet.server.file.adapter.in; | ||
|
||
import com.stumeet.server.common.annotation.WebAdapter; | ||
import com.stumeet.server.common.model.ApiResponse; | ||
import com.stumeet.server.common.response.SuccessCode; | ||
import com.stumeet.server.file.application.port.in.PresignedUrlGenerateUseCase; | ||
import com.stumeet.server.file.application.port.in.command.PresignedUrlCommand; | ||
import com.stumeet.server.file.application.port.in.response.PresignedUrlResponse; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.PostMapping; | ||
import org.springframework.web.bind.annotation.RequestBody; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
|
||
@WebAdapter | ||
@RequestMapping("/api/v1/presigned-url") | ||
@RequiredArgsConstructor | ||
public class PresignedUrlGenerateWebAdapter { | ||
|
||
private final PresignedUrlGenerateUseCase presignedUrlGenerateUseCase; | ||
|
||
@PostMapping | ||
public ResponseEntity<ApiResponse<PresignedUrlResponse>> generatePresignedUrl( | ||
@RequestBody PresignedUrlCommand request | ||
) { | ||
PresignedUrlResponse response = presignedUrlGenerateUseCase.generatePresignedUrl(request); | ||
|
||
return ResponseEntity.status(HttpStatus.OK) | ||
.body(ApiResponse.success(SuccessCode.PRESIGNED_URL_SUCCESS, response)); | ||
} | ||
} |
Oops, something went wrong.