-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feature/8 : 이미지 추가 및 수정 #22
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
178f990
feat : s3 이미지 업로드 구현 #7
soleu 8cfd92a
feat : s3 이미지 업로드 구현 #7
soleu efe5464
feat : 상품 올리기 구현 #7
soleu 0f1f887
feat : 에러 처리 수정 #7
soleu c2a6a50
feat : 상품 등록 통합 테스트 작성 #7
soleu 350e80d
feat : 상품 등록 유닛 테스트 작성 #7
soleu 55b34ff
Merge branch 'main' of https://github.com/f-lab-edu/get-offer into fe…
soleu 6acc5d5
feat : 상품 수정 api 구현 #8
soleu e1ca3bc
feat : 에러 코드 추가 #8
soleu e39fdbb
feat : 테스트 코드 추가 #8
soleu ad1c62c
feat: 테스트 코드 추가 #8
soleu 0c8fbee
feat : S3 버전 업
soleu 868bc46
feat : oauth2 구글 로그인 권한 설정 #8
soleu f622741
feat : oauth2 구글 로그인 토근 발급 #8
soleu 7f75d88
feat : 코드리뷰 피드백 수정 #8
soleu File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
65 changes: 65 additions & 0 deletions
65
src/main/kotlin/com/get_offer/common/oauth/OAuth2LoginConfig.kt
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,65 @@ | ||
package com.get_offer.common.oauth | ||
|
||
import org.springframework.beans.factory.annotation.Value | ||
import org.springframework.context.annotation.Bean | ||
import org.springframework.context.annotation.Configuration | ||
import org.springframework.security.config.annotation.web.builders.HttpSecurity | ||
import org.springframework.security.oauth2.client.endpoint.DefaultAuthorizationCodeTokenResponseClient | ||
import org.springframework.security.oauth2.client.endpoint.OAuth2AccessTokenResponseClient | ||
import org.springframework.security.oauth2.client.endpoint.OAuth2AuthorizationCodeGrantRequest | ||
import org.springframework.security.oauth2.client.registration.ClientRegistration | ||
import org.springframework.security.oauth2.client.registration.ClientRegistrationRepository | ||
import org.springframework.security.oauth2.client.registration.InMemoryClientRegistrationRepository | ||
import org.springframework.security.oauth2.core.AuthorizationGrantType | ||
import org.springframework.security.oauth2.core.ClientAuthenticationMethod | ||
import org.springframework.security.oauth2.core.oidc.IdTokenClaimNames | ||
import org.springframework.security.web.SecurityFilterChain | ||
|
||
|
||
@Configuration | ||
class OAuth2LoginConfig( | ||
@Value("\${spring.security.oauth2.client.registration.google-login.client-id}") | ||
private val clientId: String, | ||
@Value("\${spring.security.oauth2.client.registration.google-login.client-secret}") | ||
private val clientSecret: String, | ||
) { | ||
@Bean | ||
fun clientRegistrationRepository(): ClientRegistrationRepository { | ||
return InMemoryClientRegistrationRepository(googleClientRegistration()) | ||
} | ||
|
||
private fun googleClientRegistration(): ClientRegistration { | ||
val baseUrl = "http://localhost:8080" | ||
val registrationId = "google" | ||
return ClientRegistration.withRegistrationId("google").clientId(clientId).clientSecret(clientSecret) | ||
.clientAuthenticationMethod(ClientAuthenticationMethod.CLIENT_SECRET_BASIC) | ||
.authorizationGrantType(AuthorizationGrantType.AUTHORIZATION_CODE) | ||
.redirectUri("${baseUrl}/login/oauth2/code/${registrationId}") | ||
.scope("openid", "profile", "email", "address", "phone") | ||
.authorizationUri("https://accounts.google.com/o/oauth2/v2/auth") | ||
.tokenUri("https://www.googleapis.com/oauth2/v4/token") | ||
.userInfoUri("https://www.googleapis.com/oauth2/v3/userinfo").userNameAttributeName(IdTokenClaimNames.SUB) | ||
.jwkSetUri("https://www.googleapis.com/oauth2/v3/certs").clientName("Google").build() | ||
} | ||
|
||
@Bean | ||
fun filterChain(http: HttpSecurity): SecurityFilterChain { | ||
http.authorizeHttpRequests { authorizeHttpRequests -> | ||
authorizeHttpRequests.requestMatchers("/oauth_login", "/loginSuccess").permitAll() | ||
.anyRequest() | ||
.authenticated() | ||
}.oauth2Login { it -> | ||
it.loginPage("/oauth_login") | ||
it.defaultSuccessUrl("/loginSuccess", true) | ||
it.tokenEndpoint { | ||
it.accessTokenResponseClient(accessTokenResponseClient()) | ||
} | ||
} | ||
return http.build() | ||
} | ||
|
||
@Bean | ||
fun accessTokenResponseClient(): OAuth2AccessTokenResponseClient<OAuth2AuthorizationCodeGrantRequest> { | ||
return DefaultAuthorizationCodeTokenResponseClient() | ||
} | ||
} |
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,26 @@ | ||
package com.get_offer.common.s3 | ||
|
||
|
||
import org.springframework.beans.factory.annotation.Value | ||
import org.springframework.context.annotation.Bean | ||
import org.springframework.context.annotation.Configuration | ||
import software.amazon.awssdk.auth.credentials.AwsBasicCredentials | ||
import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider | ||
import software.amazon.awssdk.regions.Region | ||
import software.amazon.awssdk.services.s3.S3Client | ||
|
||
@Configuration | ||
class S3Config( | ||
@Value("\${aws.credentials.accessKey}") | ||
private val accessKey: String, | ||
@Value("\${aws.credentials.secretKey}") | ||
private val secretKey: String, | ||
) { | ||
@Bean | ||
fun amazonS3Client(): S3Client { | ||
val credentials = AwsBasicCredentials.create(accessKey, secretKey) | ||
return S3Client.builder().region(Region.AP_NORTHEAST_2) | ||
.credentialsProvider(StaticCredentialsProvider.create(credentials)) | ||
.build() | ||
} | ||
} |
60 changes: 60 additions & 0 deletions
60
src/main/kotlin/com/get_offer/common/s3/S3FileManagement.kt
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.get_offer.common.s3 | ||
|
||
import com.get_offer.multipart.FileValidate | ||
import java.util.* | ||
import org.springframework.beans.factory.annotation.Value | ||
import org.springframework.stereotype.Component | ||
import org.springframework.web.multipart.MultipartFile | ||
import software.amazon.awssdk.core.sync.RequestBody | ||
import software.amazon.awssdk.services.s3.S3Client | ||
import software.amazon.awssdk.services.s3.model.DeleteObjectRequest | ||
import software.amazon.awssdk.services.s3.model.PutObjectRequest | ||
|
||
@Component | ||
class S3FileManagement( | ||
@Value("\${aws.s3.bucket}") | ||
private val bucket: String, | ||
private val amazonS3: S3Client, | ||
) { | ||
companion object { | ||
const val TYPE_IMAGE = "image" | ||
} | ||
|
||
fun uploadImages(multipartFiles: List<MultipartFile>): List<String> { | ||
return multipartFiles.map { uploadImage(it) } | ||
} | ||
|
||
fun uploadImage(multipartFile: MultipartFile): String { | ||
val originalFilename = multipartFile.originalFilename | ||
?: throw IllegalStateException() | ||
FileValidate.checkImageFormat(originalFilename) | ||
val fileName = "${UUID.randomUUID()}-${originalFilename}" | ||
|
||
val putObjectRequest = PutObjectRequest.builder() | ||
.bucket(bucket) | ||
.key(fileName) | ||
.contentType("/${TYPE_IMAGE}/${getFileExtension(getFileExtension(originalFilename))}") | ||
.contentLength(multipartFile.inputStream.available().toLong()) | ||
.build() | ||
|
||
amazonS3.putObject(putObjectRequest, RequestBody.fromBytes(multipartFile.bytes)) | ||
return fileName | ||
} | ||
|
||
fun delete(fileNames: List<String>) { | ||
fileNames.forEach { delete(it) } | ||
} | ||
|
||
private fun delete(fileName: String) { | ||
val deleteObjectRequest = DeleteObjectRequest.builder() | ||
.bucket(bucket) | ||
.key(fileName) | ||
.build() | ||
amazonS3.deleteObject(deleteObjectRequest) | ||
} | ||
|
||
private fun getFileExtension(fileName: String): String { | ||
val extensionIndex = fileName.lastIndexOf('.') | ||
return fileName.substring(extensionIndex + 1) | ||
} | ||
} |
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,20 @@ | ||
package com.get_offer.multipart | ||
|
||
import com.get_offer.common.exception.UnsupportedFileExtensionException | ||
|
||
class FileValidate { | ||
companion object { | ||
private val IMAGE_EXTENSIONS: List<String> = listOf("jpg", "png", "jpeg", "JPG", "PNG", "JPEG") | ||
|
||
fun checkImageFormat(fileName: String) { | ||
val extensionIndex = fileName.lastIndexOf('.') | ||
if (extensionIndex == -1) { | ||
throw UnsupportedFileExtensionException() | ||
} | ||
val extension = fileName.substring(extensionIndex + 1) | ||
require(IMAGE_EXTENSIONS.contains(extension)) { | ||
mason136 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
throw UnsupportedFileExtensionException() | ||
} | ||
} | ||
} | ||
} |
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,18 @@ | ||
package com.get_offer.multipart | ||
|
||
import com.get_offer.common.s3.S3FileManagement | ||
import org.springframework.stereotype.Service | ||
import org.springframework.web.multipart.MultipartFile | ||
|
||
@Service | ||
class ImageService( | ||
private val s3FileManagement: S3FileManagement, | ||
) { | ||
fun saveImages(images: List<MultipartFile>): List<String> { | ||
return s3FileManagement.uploadImages(images) | ||
} | ||
|
||
fun deleteImages(imageUrls: List<String>) { | ||
s3FileManagement.delete(imageUrls) | ||
} | ||
} |
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
13 changes: 13 additions & 0 deletions
13
src/main/kotlin/com/get_offer/product/controller/ProductEditReqDto.kt
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,13 @@ | ||
package com.get_offer.product.controller | ||
|
||
import com.get_offer.product.domain.Category | ||
import java.time.LocalDateTime | ||
|
||
data class ProductEditReqDto( | ||
val title: String?, | ||
val category: Category?, | ||
val description: String?, | ||
val startPrice: Int?, | ||
val startDate: LocalDateTime?, | ||
val endDate: LocalDateTime?, | ||
) |
13 changes: 13 additions & 0 deletions
13
src/main/kotlin/com/get_offer/product/controller/ProductPostReqDto.kt
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,13 @@ | ||
package com.get_offer.product.controller | ||
|
||
import com.get_offer.product.domain.Category | ||
import java.time.LocalDateTime | ||
|
||
data class ProductPostReqDto( | ||
val title: String, | ||
val category: Category, | ||
val description: String, | ||
val startPrice: Int, | ||
val startDate: LocalDateTime, | ||
val endDate: LocalDateTime, | ||
) |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
예외가 발생할 때마다 클래스를 만들 수 없으니 응답 코드, 메세지를 받는 예외 클래스를 만들어보시죠