-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #77 from HongDam-org/feat/profile-image
[FEAT] profile image 업로드 api
- Loading branch information
Showing
19 changed files
with
263 additions
and
26 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -39,3 +39,5 @@ out/ | |
.vscode/ | ||
|
||
/src/main/generated/ | ||
|
||
/src/main/resources/*.json |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
:doctype: book | ||
:icons: font | ||
:source-highlighter: highlightjs | ||
:toc: left | ||
:toclevels: 4 | ||
|
||
== Image | ||
=== 프로필 이미지 업로드 | ||
operation::post upload image[snippets='http-request,http-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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,3 +15,4 @@ include::place.adoc[] | |
include::member.adoc[] | ||
include::friend.adoc[] | ||
include::group.adoc[] | ||
include::image.adoc[] |
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
31 changes: 31 additions & 0 deletions
31
backend/src/main/java/com/twtw/backend/config/storage/StorageConfig.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,31 @@ | ||
package com.twtw.backend.config.storage; | ||
|
||
import com.google.auth.oauth2.GoogleCredentials; | ||
import com.google.cloud.storage.Storage; | ||
import com.google.cloud.storage.StorageOptions; | ||
import com.twtw.backend.global.properties.StorageProperties; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
|
||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.core.io.ClassPathResource; | ||
|
||
import java.io.IOException; | ||
|
||
@Configuration | ||
@RequiredArgsConstructor | ||
public class StorageConfig { | ||
|
||
private final StorageProperties storageProperties; | ||
|
||
@Bean | ||
public Storage storage() throws IOException { | ||
final GoogleCredentials googleCredentials = | ||
GoogleCredentials.fromStream( | ||
new ClassPathResource(storageProperties.getCredentialsFile()) | ||
.getInputStream()); | ||
|
||
return StorageOptions.newBuilder().setCredentials(googleCredentials).build().getService(); | ||
} | ||
} |
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
27 changes: 27 additions & 0 deletions
27
backend/src/main/java/com/twtw/backend/domain/image/controller/ImageController.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,27 @@ | ||
package com.twtw.backend.domain.image.controller; | ||
|
||
import com.twtw.backend.domain.image.dto.ImageResponse; | ||
import com.twtw.backend.domain.image.service.ImageService; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
|
||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.PostMapping; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
import org.springframework.web.multipart.MultipartFile; | ||
|
||
import java.io.IOException; | ||
|
||
@RestController | ||
@RequiredArgsConstructor | ||
@RequestMapping("images") | ||
public class ImageController { | ||
|
||
private final ImageService imageService; | ||
|
||
@PostMapping | ||
public ResponseEntity<ImageResponse> uploadImage(final MultipartFile image) throws IOException { | ||
return ResponseEntity.ok(imageService.uploadImage(image)); | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
.../src/main/java/com/twtw/backend/domain/image/controller/advice/ImageControllerAdvice.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,24 @@ | ||
package com.twtw.backend.domain.image.controller.advice; | ||
|
||
import com.twtw.backend.domain.image.exception.InvalidFileTypeException; | ||
import com.twtw.backend.global.advice.ErrorResponse; | ||
|
||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.ExceptionHandler; | ||
import org.springframework.web.bind.annotation.RestControllerAdvice; | ||
|
||
import java.io.IOException; | ||
|
||
@RestControllerAdvice | ||
public class ImageControllerAdvice { | ||
|
||
@ExceptionHandler(InvalidFileTypeException.class) | ||
public ResponseEntity<ErrorResponse> handleInvalidFileType(final InvalidFileTypeException e) { | ||
return ResponseEntity.badRequest().body(new ErrorResponse(e.getMessage())); | ||
} | ||
|
||
@ExceptionHandler(IOException.class) | ||
public ResponseEntity<ErrorResponse> io(final IOException e) { | ||
return ResponseEntity.badRequest().body(new ErrorResponse(e.getMessage())); | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
backend/src/main/java/com/twtw/backend/domain/image/dto/ImageResponse.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,10 @@ | ||
package com.twtw.backend.domain.image.dto; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
|
||
@Getter | ||
@AllArgsConstructor | ||
public class ImageResponse { | ||
private String imageUrl; | ||
} |
10 changes: 10 additions & 0 deletions
10
backend/src/main/java/com/twtw/backend/domain/image/exception/InvalidFileTypeException.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,10 @@ | ||
package com.twtw.backend.domain.image.exception; | ||
|
||
public class InvalidFileTypeException extends IllegalArgumentException { | ||
|
||
private static final String MESSAGE = "파일 형식이 잘못되었습니다."; | ||
|
||
public InvalidFileTypeException() { | ||
super(MESSAGE); | ||
} | ||
} |
60 changes: 60 additions & 0 deletions
60
backend/src/main/java/com/twtw/backend/domain/image/service/ImageService.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,60 @@ | ||
package com.twtw.backend.domain.image.service; | ||
|
||
import com.google.cloud.storage.BlobInfo; | ||
import com.google.cloud.storage.Storage; | ||
import com.twtw.backend.domain.image.dto.ImageResponse; | ||
import com.twtw.backend.domain.image.exception.InvalidFileTypeException; | ||
import com.twtw.backend.domain.member.entity.Member; | ||
import com.twtw.backend.domain.member.service.AuthService; | ||
import com.twtw.backend.global.properties.StorageProperties; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
|
||
import org.springframework.stereotype.Service; | ||
import org.springframework.web.multipart.MultipartFile; | ||
|
||
import java.io.IOException; | ||
import java.util.UUID; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
public class ImageService { | ||
|
||
private static final String IMAGE_FILE_TYPE = "image"; | ||
private final AuthService authService; | ||
private final Storage storage; | ||
private final StorageProperties storageProperties; | ||
|
||
public ImageResponse uploadImage(final MultipartFile image) throws IOException { | ||
final Member member = authService.getMemberByJwt(); | ||
final String contentType = image.getContentType(); | ||
validate(contentType); | ||
|
||
final String uuid = UUID.randomUUID().toString(); | ||
upload(image, contentType, uuid); | ||
|
||
final String imagePath = getImageUrl(uuid); | ||
member.updateProfileImage(imagePath); | ||
|
||
return new ImageResponse(imagePath); | ||
} | ||
|
||
private String getImageUrl(final String uuid) { | ||
return storageProperties.getStorageUrl() + uuid; | ||
} | ||
|
||
private void upload(final MultipartFile image, final String contentType, final String uuid) | ||
throws IOException { | ||
storage.create( | ||
BlobInfo.newBuilder(storageProperties.getBucket(), uuid) | ||
.setContentType(contentType) | ||
.build(), | ||
image.getInputStream()); | ||
} | ||
|
||
private void validate(final String contentType) { | ||
if (contentType == null || !contentType.startsWith(IMAGE_FILE_TYPE)) { | ||
throw new InvalidFileTypeException(); | ||
} | ||
} | ||
} |
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
15 changes: 15 additions & 0 deletions
15
backend/src/main/java/com/twtw/backend/global/properties/StorageProperties.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,15 @@ | ||
package com.twtw.backend.global.properties; | ||
|
||
import lombok.Getter; | ||
import lombok.RequiredArgsConstructor; | ||
|
||
import org.springframework.boot.context.properties.ConfigurationProperties; | ||
|
||
@Getter | ||
@RequiredArgsConstructor | ||
@ConfigurationProperties(prefix = "spring.cloud.gcp.storage") | ||
public class StorageProperties { | ||
private final String bucket; | ||
private final String credentialsFile; | ||
private final String storageUrl; | ||
} |
Oops, something went wrong.