-
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.
Browse files
Browse the repository at this point in the history
AWS S3를 이용한 프로필 사진 업로드
- Loading branch information
Showing
18 changed files
with
290 additions
and
115 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
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
53 changes: 53 additions & 0 deletions
53
src/main/java/com/market/saessag/domain/photo/controller/ProfileImageController.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,53 @@ | ||
package com.market.saessag.domain.photo.controller; | ||
|
||
import com.market.saessag.domain.photo.service.S3Service; | ||
import com.market.saessag.global.exception.ErrorCode; | ||
import com.market.saessag.global.response.ApiResponse; | ||
import com.market.saessag.global.response.SuccessCode; | ||
import jakarta.servlet.http.HttpSession; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.web.bind.annotation.*; | ||
import org.springframework.web.multipart.MultipartFile; | ||
|
||
import java.io.IOException; | ||
import java.util.Map; | ||
|
||
@RestController | ||
@RequestMapping("/api/profile") | ||
@RequiredArgsConstructor | ||
public class ProfileImageController { | ||
private final S3Service s3Service; | ||
|
||
/* | ||
프로필 사진 업로드 | ||
사용자 관점에서는 프로필 사진 업로드와 수정이 동일한 방식으로 진행됨. | ||
따라서 하나의 Patch 메서드에서 동작함. | ||
*/ | ||
@PatchMapping("/upload-image") | ||
public ApiResponse<String> uploadProfileImage(@RequestParam("file") MultipartFile file, HttpSession session) { | ||
try { | ||
String email = (String) session.getAttribute("email"); | ||
if (email == null) { | ||
return ApiResponse.error(ErrorCode.UNAUTHORIZED); | ||
} | ||
|
||
String fileUrl = s3Service.uploadProfileImage(file, email); | ||
return ApiResponse.success(SuccessCode.UPLOAD_SUCCESS, fileUrl); | ||
} catch (IOException e) { | ||
return ApiResponse.error(ErrorCode.FILE_UPLOAD_ERROR); | ||
} | ||
} | ||
|
||
// 프로필 사진 조회 | ||
@GetMapping | ||
public ApiResponse<Map<String, String>> getProfileImageUrl(HttpSession session) { | ||
String email = (String) session.getAttribute("email"); | ||
if (email == null) { | ||
return ApiResponse.error(ErrorCode.UNAUTHORIZED); | ||
} | ||
|
||
Map<String, String> urls = s3Service.getProfileImageUrl(email); | ||
return ApiResponse.success(SuccessCode.OK, urls); | ||
} | ||
|
||
} |
16 changes: 16 additions & 0 deletions
16
src/main/java/com/market/saessag/domain/photo/dto/UserProfileImageResponse.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,16 @@ | ||
package com.market.saessag.domain.photo.dto; | ||
|
||
import com.market.saessag.domain.user.entity.User; | ||
import lombok.Getter; | ||
|
||
// 프로필 사진 조회 | ||
@Getter | ||
public class UserProfileImageResponse { | ||
private String profileUrl; | ||
|
||
public static UserProfileImageResponse from(User user) { | ||
UserProfileImageResponse response = new UserProfileImageResponse(); | ||
response.profileUrl = user.getProfileUrl(); | ||
return response; | ||
} | ||
} |
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
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
Oops, something went wrong.