Skip to content

Commit

Permalink
[LH-199] Fix get pre-signed url api
Browse files Browse the repository at this point in the history
  • Loading branch information
YoungJun-L committed Nov 21, 2023
1 parent 50ac742 commit 1f58d8c
Show file tree
Hide file tree
Showing 14 changed files with 67 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public class ImageManager {

public MemberPreSignedUrlResponse createPreSignedUrl(final MemberPreSignedUrlRequest memberPreSignedUrlRequest) {
URL preSignedUrl = imageService.generatePresignedUrl(memberPreSignedUrlRequest.key());
return MemberPreSignedUrlResponse.from(preSignedUrl);
return MemberPreSignedUrlResponse.of(preSignedUrl, imageService.getEndpoint());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import org.springframework.context.annotation.Configuration;

@Configuration
public class S3Config {
public class AmazonS3Config {

@Value("${aws.credentials.access-key}")
private String accessKey;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,31 @@
import org.springframework.context.annotation.Profile;
import org.springframework.stereotype.Service;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.Date;

@Profile("!test")
@Service
public class S3Service implements ImageService {
public class AmazonImageService implements ImageService {

private static final long ONE_HOUR_IN_MS = 3_600_000;

private final AmazonS3 s3;
private final String bucketName;
private final String profileKeyPrefix;
private final String cloudFrontEndpoint;
private final TimeHolder timeHolder;

public S3Service(final AmazonS3 s3,
@Value("${aws.s3.bucket-name}") final String bucketName,
@Value("${aws.s3.profile.prefix}") final String profileKeyPrefix,
final TimeHolder timeHolder) {
public AmazonImageService(final AmazonS3 s3,
@Value("${aws.s3.bucket-name}") final String bucketName,
@Value("${aws.s3.profile.prefix}") final String profileKeyPrefix,
@Value("${aws.distribution.endpoint}") final String cloudFrontEndpoint,
final TimeHolder timeHolder) {
this.s3 = s3;
this.bucketName = bucketName;
this.profileKeyPrefix = profileKeyPrefix;
this.cloudFrontEndpoint = cloudFrontEndpoint;
this.timeHolder = timeHolder;
}

Expand All @@ -39,4 +43,13 @@ public URL generatePresignedUrl(final String key) {
return s3.generatePresignedUrl(req);
}

@Override
public URL getEndpoint() {
try {
return new URL(cloudFrontEndpoint);
} catch (MalformedURLException e) {
throw new IllegalStateException(e);
}
}

}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,6 @@ public interface ImageService {

URL generatePresignedUrl(final String key);

URL getEndpoint();

}
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,12 @@ public void updateProfile(final String uuid, final MemberUpdateProfileRequest me
member.changeProfileImageUri(memberUpdateProfileRequest.profileImageUri());
}

@Transactional
public void updateProfileImage(final String uuid, final MemberUpdateProfileImageRequest memberUpdateProfileRequest) {
Member member = memberRepository.getByUuid(uuid);
member.changeProfileImageUri(memberUpdateProfileRequest.profileImageUri());
}

@Transactional
public void updatePreference(final String uuid, final MemberUpdatePreferenceRequest memberRequest) {
Member member = memberRepository.getByUuid(uuid);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

import java.net.URL;

public record MemberPreSignedUrlResponse(URL url) {
public record MemberPreSignedUrlResponse(URL url, URL endPoint) {

public static MemberPreSignedUrlResponse from(URL url) {
return new MemberPreSignedUrlResponse(url);
public static MemberPreSignedUrlResponse of(URL url, URL endPoint) {
return new MemberPreSignedUrlResponse(url, endPoint);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.lighthouse.lingoswap.member.dto;

import jakarta.validation.constraints.NotNull;

public record MemberUpdateProfileImageRequest(@NotNull String profileImageUri) {

public static MemberUpdateProfileImageRequest of(final String profileImageUri) {
return new MemberUpdateProfileImageRequest(profileImageUri);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@

import com.lighthouse.lingoswap.common.dto.ResponseDto;
import com.lighthouse.lingoswap.member.application.MemberManager;
import com.lighthouse.lingoswap.member.dto.MemberPreferenceResponse;
import com.lighthouse.lingoswap.member.dto.MemberProfileResponse;
import com.lighthouse.lingoswap.member.dto.MemberUpdatePreferenceRequest;
import com.lighthouse.lingoswap.member.dto.MemberUpdateProfileRequest;
import com.lighthouse.lingoswap.member.dto.*;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
Expand Down Expand Up @@ -35,6 +32,13 @@ public ResponseEntity<ResponseDto<Void>> patchProfile(@PathVariable final String
return ResponseEntity.ok(ResponseDto.noData());
}

@PatchMapping("/{uuid}/profile/image")
public ResponseEntity<ResponseDto<Void>> patchProfileImage(@PathVariable final String uuid,
@Valid @RequestBody final MemberUpdateProfileImageRequest memberUpdateProfileImageRequest) {
memberManager.updateProfileImage(uuid, memberUpdateProfileImageRequest);
return ResponseEntity.ok(ResponseDto.noData());
}

@PatchMapping("/{uuid}/preference")
public ResponseEntity<ResponseDto<Void>> patchPreference(@PathVariable final String uuid,
@Valid @RequestBody final MemberUpdatePreferenceRequest memberUpdatePreferenceRequest) {
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/security
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import com.lighthouse.lingoswap.chat.service.SendbirdService;
import com.lighthouse.lingoswap.country.application.CountryManager;
import com.lighthouse.lingoswap.image.application.ImageManager;
import com.lighthouse.lingoswap.infra.service.S3Service;
import com.lighthouse.lingoswap.infra.service.AmazonImageService;
import com.lighthouse.lingoswap.interests.application.InterestsManager;
import com.lighthouse.lingoswap.language.application.LanguageManager;
import com.lighthouse.lingoswap.likemember.application.LikeMemberManager;
Expand Down Expand Up @@ -36,7 +36,7 @@ public abstract class ControllerTestSupport {
protected SendbirdService sendbirdService;

@MockBean
protected S3Service s3Service;
protected AmazonImageService amazonImageService;

@MockBean
protected MemberManager memberManager;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,21 @@
class ImageControllerTest extends ControllerTestSupport {

private static final String IMAGE_KEY = "/2c1a2c3d-4d8b-46f4-9011-189cd1fc8644/1.jpg";
private static final String PRE_SIGNED_URL = "https://abc/profiles/2c1a2c3d-4d8b-46f4-9011-189cd1fc8644/1.jpg";
private static final String PRE_SIGNED_URL = "https://abc.com/profiles/2c1a2c3d-4d8b-46f4-9011-189cd1fc8644/1.jpg";
private static final String END_POINT = "https://abc.com";

@DisplayName("S3 pre-signed url을 발급하면 상태 코드 200을 반환한다.")
@WithAuthorizedUser
@Test
void getPreSignedUrl() throws Exception {
// given
MemberPreSignedUrlRequest request = MemberPreSignedUrlRequest.from(IMAGE_KEY);
MemberPreSignedUrlResponse response = MemberPreSignedUrlResponse.from(new URL(PRE_SIGNED_URL));
MemberPreSignedUrlResponse response = MemberPreSignedUrlResponse.of(new URL(PRE_SIGNED_URL), new URL(END_POINT));
given(imageManager.createPreSignedUrl(request)).willReturn(response);

// when & then
mockMvc.perform(
post("/api/v1/admin/upload/profile")
post("/api/v1/user/upload/profile")
.contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsString(request))
).andDo(print())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,13 @@ public URL generatePresignedUrl(final String key) {
}
}

@Override
public URL getEndpoint() {
try {
return new URL("https://cloudfront.amazonaws.com");
} catch (MalformedURLException e) {
throw new RuntimeException(e);
}
}

}
2 changes: 1 addition & 1 deletion src/test/resources/application-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,4 @@ aws:
profile:
prefix: profiles
distribution:
end-point: https://123.abcd.com/profiles
endpoint: https://123.abcd.com/profiles

0 comments on commit 1f58d8c

Please sign in to comment.