Skip to content

Commit

Permalink
Merge pull request #55 from Team-PLAT/refactor/#54/imageUpload
Browse files Browse the repository at this point in the history
refactor: #54 이미지 업로드 API 엔드포인트 변경
  • Loading branch information
dgh06175 authored Sep 10, 2024
2 parents 2a75072 + 520b49c commit 159a358
Show file tree
Hide file tree
Showing 11 changed files with 84 additions and 32 deletions.
3 changes: 2 additions & 1 deletion src/main/java/com/cabin/plat/config/SecurityConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
.requestMatchers("/test").authenticated()
.requestMatchers("/members/**").authenticated()
.requestMatchers("/tracks/**").authenticated()
.requestMatchers("address/**").authenticated()
.requestMatchers("/address/**").authenticated()
.requestMatchers("images/**").authenticated()
.anyRequest().denyAll());

http
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.cabin.plat.config.image.controller;

import com.cabin.plat.config.image.dto.ImageResponse;
import com.cabin.plat.config.image.service.ImageService;
import com.cabin.plat.global.common.BaseResponse;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

@RestController
@RequiredArgsConstructor
@RequestMapping("/images")
@Tag(name = "이미지 API")
public class ImageController {
private final ImageService imageService;

@Operation(summary = "사진 업로드", description = "사진을 업로드하여 해당 사진의 URL 을 반환합니다.")
@PostMapping(consumes = MediaType.MULTIPART_FORM_DATA_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
public BaseResponse<ImageResponse.Avatar> uploadAvatarImage(@RequestPart(value = "image") MultipartFile image) {
return BaseResponse.onSuccess(imageService.uploadAvatarImage(image));
}
}
14 changes: 14 additions & 0 deletions src/main/java/com/cabin/plat/config/image/dto/ImageResponse.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.cabin.plat.config.image.dto;

import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Builder;
import lombok.Getter;

public class ImageResponse {
@Getter
@Builder
public static class Avatar {
@Schema(description = "아바타 이미지 URL", example = "https://example.com/avatar.png")
private String avatar;
}
}
13 changes: 13 additions & 0 deletions src/main/java/com/cabin/plat/config/image/mapper/ImageMapper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.cabin.plat.config.image.mapper;

import com.cabin.plat.config.image.dto.ImageResponse;
import org.springframework.stereotype.Component;

@Component
public class ImageMapper {
public ImageResponse.Avatar toAvatar(String avatar) {
return ImageResponse.Avatar.builder()
.avatar(avatar)
.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.cabin.plat.config.image.service;

import com.cabin.plat.config.image.dto.ImageResponse;
import org.springframework.web.multipart.MultipartFile;

public interface ImageService {
ImageResponse.Avatar uploadAvatarImage(MultipartFile image);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.cabin.plat.config.image.service;

import com.cabin.plat.config.image.dto.ImageResponse;
import com.cabin.plat.config.image.mapper.ImageMapper;
import com.cabin.plat.global.util.S3FileComponent;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

@Service
@RequiredArgsConstructor
public class ImageServiceImpl implements ImageService {
private final ImageMapper imageMapper;
private final S3FileComponent s3FileComponent;

@Override
public ImageResponse.Avatar uploadAvatarImage(MultipartFile image) {
return imageMapper.toAvatar(s3FileComponent.uploadFile("image", image));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
@RestController
@RequiredArgsConstructor
@RequestMapping("/members")
@Tag(name = "멤버 API", description = "회원 관련 API입니다. 사용자 프로필 조회, 업데이트 및 삭제, 이미지 업로드 기능을 제공합니다.")
@Tag(name = "멤버 API", description = "회원 관련 API입니다. 사용자 프로필 조회, 업데이트 및 삭제 기능을 제공합니다.")
public class MemberController {
private final MemberService memberService;

Expand Down Expand Up @@ -54,13 +54,7 @@ public BaseResponse<MemberResponse.MemberId> updateStreamType(
return BaseResponse.onSuccess(memberService.updateStreamType(member, streamType));
}

@Operation(summary = "유저 프로필 사진 업로드", description = "프로필 사진을 업로드하고 URL을 반환합니다. 이미지를 `image` 파라미터로 전송하여 프로필 사진을 변경하세요. \"유저 프로필 사진 변경\" API를 호출하기 직전에 사용해서 이미지의 URL을 받으세요.")
@PostMapping(value = "/profile/avatar/upload", consumes = MediaType.MULTIPART_FORM_DATA_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
public BaseResponse<MemberResponse.Avatar> uploadAvatarImage(@RequestPart(value = "image") MultipartFile image) {
return BaseResponse.onSuccess(memberService.uploadAvatarImage(image));
}

@Operation(summary = "유저 프로필 사진 변경", description = "업로드한 이미지의 URL로 프로필 사진을 변경합니다. \"유저 프로필 사진 업로드\" API 에서 받은 이미지 URL을 `avatar` 객체에 담아 요청 본문으로 전달하세요.")
@Operation(summary = "유저 프로필 사진 변경", description = "업로드한 이미지의 URL로 프로필 사진을 변경합니다. \"사진 업로드\" API 에서 받은 이미지 URL을 `avatar` 객체에 담아 요청 본문으로 전달하세요.")
@PatchMapping("/profile/avatar")
public BaseResponse<MemberResponse.MemberId> updateAvatarUrl(@AuthMember Member member,
@RequestBody MemberRequest.Avatar avatar) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,6 @@ public static class MemberId {
private Long memberId;
}

@Getter
@Builder
public static class Avatar {
@Schema(description = "아바타 이미지 URL", example = "https://example.com/avatar.png")
private String avatar;
}

@Getter
@Builder
@AllArgsConstructor
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,4 @@ public Member toMember(final String clientId, SocialType socialType) {
.email("")
.build();
}

public MemberResponse.Avatar toAvatar(String avatar) {
return MemberResponse.Avatar.builder()
.avatar(avatar)
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ public interface MemberService {

MemberResponse.MemberId updateStreamType(Member member, StreamType streamType);

MemberResponse.Avatar uploadAvatarImage(MultipartFile image);

MemberResponse.MemberId updateAvatarUrl(Member member, String avatar);

MemberResponse.MemberSignIn appleSocialSignIn(MemberRequest.MemberAppleSocialSignIn request, SocialType socialType);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,9 @@
import com.cabin.plat.domain.member.entity.SocialType;
import com.cabin.plat.domain.member.mapper.AuthenticationMapper;
import com.cabin.plat.domain.member.repository.RefreshTokenRepository;
import com.cabin.plat.global.util.S3FileComponent;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.util.Optional;

Expand All @@ -32,7 +30,6 @@ public class MemberServiceImpl implements MemberService {

private final MemberRepository memberRepository;
private final JwtUtil jwtUtil;
private final S3FileComponent s3FileComponent;
private final AuthenticationMapper authenticationMapper;
private final RefreshTokenRepository refreshTokenRepository;
private final MemberMapper memberMapper;
Expand Down Expand Up @@ -110,11 +107,6 @@ public MemberId updateStreamType(Member member, StreamType streamType) {
return memberMapper.toMemberId(updateMember.getId());
}

@Override
public Avatar uploadAvatarImage(MultipartFile image) {
return memberMapper.toAvatar(s3FileComponent.uploadFile("image", image));
}

@Override
@Transactional
public MemberId updateAvatarUrl(Member member, String avatar) {
Expand Down

0 comments on commit 159a358

Please sign in to comment.