-
Notifications
You must be signed in to change notification settings - Fork 1
[6차 스프린트 - Steady] 스테디용 Presigned URL 요청 기능 추가 #176
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
na-yk
wants to merge
3
commits into
dev
Choose a base branch
from
feat/#175
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,31 @@ | ||
| package dev.steady.storage; | ||
|
|
||
| import dev.steady.global.exception.InvalidValueException; | ||
| import lombok.Getter; | ||
|
|
||
| import java.util.Arrays; | ||
|
|
||
| import static dev.steady.storage.exception.StorageErrorCode.NOT_SUPPORTED_PURPOSE; | ||
|
|
||
| @Getter | ||
| public enum ImageUploadPurpose { | ||
|
|
||
| USER_PROFILE_IMAGE("profile", "profile/%s"), | ||
| STEADY_CONTENT_IMAGE("steady", "steady/content/%s"); | ||
|
|
||
| private final String purpose; | ||
| private final String keyPattern; | ||
|
|
||
| ImageUploadPurpose(String purpose, String keyPattern) { | ||
| this.purpose = purpose; | ||
| this.keyPattern = keyPattern; | ||
| } | ||
|
|
||
| public static ImageUploadPurpose from(String purpose) { | ||
| return Arrays.stream(ImageUploadPurpose.values()) | ||
| .filter(v -> v.getPurpose().equals(purpose)) | ||
| .findAny() | ||
| .orElseThrow(() -> new InvalidValueException(NOT_SUPPORTED_PURPOSE)); | ||
| } | ||
|
|
||
| } |
32 changes: 32 additions & 0 deletions
32
src/main/java/dev/steady/storage/controller/StorageImageController.java
This file contains hidden or 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 dev.steady.storage.controller; | ||
|
|
||
| import dev.steady.global.auth.Auth; | ||
| import dev.steady.global.auth.UserInfo; | ||
| import dev.steady.storage.ImageUploadPurpose; | ||
| import dev.steady.storage.service.StorageService; | ||
| import dev.steady.user.dto.response.PutObjectUrlResponse; | ||
| import lombok.RequiredArgsConstructor; | ||
| import org.springframework.http.ResponseEntity; | ||
| import org.springframework.web.bind.annotation.GetMapping; | ||
| import org.springframework.web.bind.annotation.PathVariable; | ||
| import org.springframework.web.bind.annotation.RequestMapping; | ||
| import org.springframework.web.bind.annotation.RequestParam; | ||
| import org.springframework.web.bind.annotation.RestController; | ||
|
|
||
| @RestController | ||
| @RequiredArgsConstructor | ||
| @RequestMapping("/api/v1/storage/image") | ||
| public class StorageImageController { | ||
|
|
||
| private final StorageService storageService; | ||
|
|
||
| @GetMapping("/{purpose}") | ||
| public ResponseEntity<PutObjectUrlResponse> getImageUploadUrl(@PathVariable String purpose, | ||
| @RequestParam String fileName, | ||
| @Auth UserInfo userInfo) { | ||
| String keyPattern = ImageUploadPurpose.from(purpose).getKeyPattern(); | ||
| PutObjectUrlResponse response = storageService.generatePutObjectUrl(fileName, keyPattern); | ||
| return ResponseEntity.ok(response); | ||
| } | ||
|
Comment on lines
+23
to
+30
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. api 설계가 좋네요! |
||
|
|
||
| } | ||
This file contains hidden or 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
2 changes: 1 addition & 1 deletion
2
...va/dev/steady/storage/StorageService.java → ...teady/storage/service/StorageService.java
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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
68 changes: 68 additions & 0 deletions
68
src/test/java/dev/steady/storage/controller/StorageImageControllerTest.java
This file contains hidden or 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,68 @@ | ||
| package dev.steady.storage.controller; | ||
|
|
||
| import com.epages.restdocs.apispec.Schema; | ||
| import dev.steady.global.auth.Authentication; | ||
| import dev.steady.global.config.ControllerTestConfig; | ||
| import dev.steady.storage.ImageUploadPurpose; | ||
| import org.junit.jupiter.api.DisplayName; | ||
| import org.junit.jupiter.params.ParameterizedTest; | ||
| import org.junit.jupiter.params.provider.EnumSource; | ||
|
|
||
| import static com.epages.restdocs.apispec.MockMvcRestDocumentationWrapper.document; | ||
| import static com.epages.restdocs.apispec.MockMvcRestDocumentationWrapper.resourceDetails; | ||
| import static dev.steady.global.auth.AuthFixture.createUserInfo; | ||
| import static dev.steady.storage.fixture.StorageFixture.createPutObjectUrlResponse; | ||
| import static org.mockito.BDDMockito.given; | ||
| import static org.springframework.http.HttpHeaders.AUTHORIZATION; | ||
| import static org.springframework.restdocs.headers.HeaderDocumentation.headerWithName; | ||
| import static org.springframework.restdocs.headers.HeaderDocumentation.requestHeaders; | ||
| import static org.springframework.restdocs.mockmvc.RestDocumentationRequestBuilders.get; | ||
| import static org.springframework.restdocs.payload.JsonFieldType.STRING; | ||
| import static org.springframework.restdocs.payload.PayloadDocumentation.fieldWithPath; | ||
| import static org.springframework.restdocs.payload.PayloadDocumentation.responseFields; | ||
| import static org.springframework.restdocs.request.RequestDocumentation.parameterWithName; | ||
| import static org.springframework.restdocs.request.RequestDocumentation.queryParameters; | ||
| import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; | ||
| import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; | ||
|
|
||
| class StorageImageControllerTest extends ControllerTestConfig { | ||
|
|
||
| @ParameterizedTest | ||
| @EnumSource(ImageUploadPurpose.class) | ||
| @DisplayName("이미지 업로드용 Presigned Url을 반환할 수 있다.") | ||
| void getImageUploadUrl(ImageUploadPurpose imageUploadPurpose) throws Exception { | ||
| // given | ||
| var userId = 1L; | ||
| var userInfo = createUserInfo(userId); | ||
| var authentication = new Authentication(userId); | ||
|
|
||
| given(jwtResolver.getAuthentication(TOKEN)).willReturn(authentication); | ||
| var response = createPutObjectUrlResponse(); | ||
| var fileName = "image.png"; | ||
| var purpose = imageUploadPurpose.getPurpose(); | ||
| var keyPattern = imageUploadPurpose.getKeyPattern(); | ||
| given(storageService.generatePutObjectUrl(fileName, keyPattern)).willReturn(response); | ||
|
|
||
| // when, then | ||
| mockMvc.perform(get("/api/v1/storage/image/{purpose}", purpose) | ||
| .queryParam("fileName", fileName) | ||
| .header(AUTHORIZATION, TOKEN)) | ||
| .andDo(document("storage-v1-get-PutObjectUrlResponse", | ||
| resourceDetails().tag("스토리지").description("이미지 업로드 URL 불러오기") | ||
| .responseSchema(Schema.schema("PutObjectUrlResponse")), | ||
| queryParameters( | ||
| parameterWithName("fileName").description("확장자를 포함한 이미지 파일 이름") | ||
| ), | ||
| requestHeaders( | ||
| headerWithName(AUTHORIZATION).description("토큰") | ||
| ), | ||
| responseFields( | ||
| fieldWithPath("presignedUrl").type(STRING).description("사용자 프로필 이미지 업로드 URL"), | ||
| fieldWithPath("objectUrl").type(STRING).description("업로드된 이미지 URL") | ||
| )) | ||
| ) | ||
| .andExpect(status().isOk()) | ||
| .andExpect(content().string(objectMapper.writeValueAsString(response))); | ||
| } | ||
|
|
||
| } |
22 changes: 22 additions & 0 deletions
22
src/test/java/dev/steady/storage/fixture/StorageFixture.java
This file contains hidden or 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,22 @@ | ||
| package dev.steady.storage.fixture; | ||
|
|
||
| import dev.steady.user.dto.response.PutObjectUrlResponse; | ||
| import org.springframework.web.util.UriComponentsBuilder; | ||
|
|
||
| public class StorageFixture { | ||
|
|
||
| public static PutObjectUrlResponse createPutObjectUrlResponse() { | ||
| String presignedUrl = UriComponentsBuilder | ||
| .fromUriString("bucket-name.s3.region.amazonaws.com/path/{fileName}") | ||
| .queryParam("X-Amz-Algorithm", "{Algorithm}") | ||
| .queryParam("X-Amz-Date", "{Date}") | ||
| .queryParam("X-Amz-SignedHeaders", "{SignedHeaders}") | ||
| .queryParam("X-Amz-Credential", "{Credential}") | ||
| .queryParam("X-Amz-Expires", "{Expires}") | ||
| .queryParam("X-Amz-Signature", "{Signature}") | ||
| .build().toString(); | ||
| String objectUrl = "https:{bucket_name}.s3.{region}.com/{key}"; | ||
| return PutObjectUrlResponse.of(presignedUrl, objectUrl); | ||
| } | ||
|
|
||
| } |
This file contains hidden or 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 hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
userInfo가 사용되지 않는 거라면 지워도 좋을 것 같아요.
아니면 기존의
JwtAuthenticationInterceptor의 동작을 좀 추가해서 파라미터 어노테이션 정보만 확인하는 것이 아니라 메서드 어노태이션 정보도 확인하게끔 하면UserInfo를 사용하지 않을 수 있을 것 같네요!