Skip to content

Commit

Permalink
Feat: 회원 및 카테고리 이미지 업데이트 s3 연동
Browse files Browse the repository at this point in the history
  • Loading branch information
tioon committed May 13, 2024
1 parent 141d612 commit 0c68ade
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,18 +46,16 @@ public Category createCategory(Long memberId, MultipartFile categoryImage, Strin
Member member = memberRepository.findById(memberId)
.orElseThrow(() -> new BusinessException(LoginException.NOT_EXIST_MEMBER));

String imagePath = s3ImageStorageService.getDefaultFileUrl();

Category category = Category.builder()
.member(member)
.categoryName(categoryName)
.categoryImage(imagePath)
.build();

category = categoryRepository.save(category);

String imageUrl = null;
if(categoryImage.isEmpty())
if(categoryImage == null)
imageUrl = s3ImageStorageService.getDefaultFileUrl();
else
imageUrl = convertImageToObject(memberId, category.getCategoryId(), categoryImage);
Expand Down Expand Up @@ -131,7 +129,7 @@ public String convertImageToObject(Long memberId, Long categoryId, MultipartFile
try (InputStream inputStream = image.getInputStream()) {
long size = image.getSize();
String contentType = image.getContentType();
return s3ImageStorageService.uploadFile(memberId.toString(), null, fileType, index, inputStream, size, contentType);
return s3ImageStorageService.uploadFile(memberId.toString(), categoryId.toString(), fileType, index, inputStream, size, contentType);
} catch (IOException e) {
log.error(e.toString());
throw new RuntimeException(e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public static class SignUpResponse {
private String nickname;
@Schema(example = "KAKAO")
private MemberType memberType;
private String profileImage;
@Schema(example = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJiZWJlLXNlcnZlciIsImlhdCI6MTcxMzQxNjgyNSwiZXhwIjoxNzEzNTAzMjI1LCJzdWIiOiJ1amozOTAwQG5hdmVyLmNvbSIsImp0aSI6IjIifQ.wvQR4Uoa8KtMgIDwRn7AwKy60olwnzP33_WLI1l3q4I")
private String accessToken;
@Schema(example = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJiZWJlLXNlcnZlciIsImlhdCI6MTcxMzQxNjgyNSwiZXhwIjoxNzE0NjI2NDI1LCJzdWIiOiJ1amozOTAwQG5hdmVyLmNvbSIsImp0aSI6IjIifQ.BYrRkhwK1SSAe3nanmRIT_oSZkWyZlNnl3wFLI_nIqY")
Expand All @@ -41,6 +42,7 @@ public static SignUpResponse of(Member member, String accessToken) {
.email(member.getEmail())
.nickname(member.getNickname())
.memberType(member.getMemberType())
.profileImage(member.getProfileImage())
.refreshToken(member.getRefreshToken())
.accessToken(accessToken)
.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.web.baebaeBE.domain.oauth2.service.Oauth2Service;
import com.web.baebaeBE.domain.login.exception.LoginException;
import com.web.baebaeBE.global.error.exception.BusinessException;
import com.web.baebaeBE.global.image.s3.S3ImageStorageService;
import com.web.baebaeBE.global.jwt.JwtTokenProvider;
import com.web.baebaeBE.domain.member.entity.Member;
import com.web.baebaeBE.domain.member.repository.MemberRepository;
Expand All @@ -14,6 +15,7 @@
import java.time.LocalDateTime;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

@Service
Expand All @@ -24,6 +26,7 @@ public class LoginService {
private final MemberRepository memberRepository;
private final Oauth2Service oauth2Service;
private final JwtTokenProvider jwtTokenProvider;
private final S3ImageStorageService s3ImageStorageService;
public static final Duration REFRESH_TOKEN_DURATION = Duration.ofDays(14); // 리프레시 토큰 유효기간.
public static final Duration ACCESS_TOKEN_DURATION = Duration.ofHours(1); // 액세스 토큰 유효기간.

Expand Down Expand Up @@ -85,6 +88,7 @@ public LoginResponse.SignUpResponse signUpNewUser(KakaoUserInfoDto kakaoUserInfo
.nickname(signUpRequest.getNickname())
.memberType(signUpRequest.getMemberType())
.refreshToken(null)
.profileImage(s3ImageStorageService.getDefaultFileUrl())
.tokenExpirationTime(LocalDateTime.now().plus(REFRESH_TOKEN_DURATION))
.build();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,34 +39,31 @@ public MemberResponse.ProfileImageResponse getProfileImage(Long memberId) {
}

public MemberResponse.ProfileImageResponse updateProfileImage(Long memberId, MultipartFile image) {
String imageUrl = null;
try {
imageUrl = convertImageToObject(memberId, image);
} catch (IOException e) {
log.error(String.valueOf(e));
throw new RuntimeException(e);
}
Member member = memberRepository.findById(memberId)
.orElseThrow(() -> new BusinessException(LoginException.NOT_EXIST_MEMBER));

String imageUrl = convertImageToObject(memberId, image);
member.updateProfileImage(imageUrl);
memberRepository.save(member);

return MemberResponse.ProfileImageResponse.of(imageUrl);
}

public String convertImageToObject(Long memberId, MultipartFile image) throws IOException {
public String convertImageToObject(Long memberId, MultipartFile image){
if (image.isEmpty()) {
throw new BusinessException(MemberException.INVAILD_IMAGE_FILE);
}
String fileType = "profile";
int index = 0; // 프로필 이미지에는 인덱스가 필요 없으므로 사용하지 않음
String fileName = memberId + "_profile.jpg";
String path = memberId + "/" + fileName;

try (InputStream inputStream = image.getInputStream()) {
long size = image.getSize();
String contentType = image.getContentType();
return s3ImageStorageService.uploadFile(memberId.toString(), null, fileType, index, inputStream, size, contentType);
} catch (IOException e) {
log.error(e.toString());
throw new RuntimeException(e);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public String generateFilePath(String memberId, String id, String fileType, int
case "audio":
return memberId + "/" + id + "/audio.mp3";
case "category":
return memberId + "/" + id + ".jpg";
return memberId + "/" + id + "/category_image.jpg";
default:
throw new IllegalArgumentException("Unknown file type");
}
Expand Down

0 comments on commit 0c68ade

Please sign in to comment.