-
Notifications
You must be signed in to change notification settings - Fork 1
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
내 정보 수정 API - 유저 프로필 이미지 수정 기능 추가
- Loading branch information
Showing
20 changed files
with
327 additions
and
54 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
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
26 changes: 20 additions & 6 deletions
26
src/main/kotlin/com/routebox/routebox/controller/user/dto/UpdateUserInfoRequest.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 |
---|---|---|
@@ -1,24 +1,38 @@ | ||
package com.routebox.routebox.controller.user.dto | ||
|
||
import com.routebox.routebox.application.user.dto.UpdateUserInfoCommand | ||
import com.routebox.routebox.domain.user.constant.Gender | ||
import com.routebox.routebox.domain.validation.Nickname | ||
import io.swagger.v3.oas.annotations.media.Schema | ||
import org.hibernate.validator.constraints.Length | ||
import org.springframework.web.multipart.MultipartFile | ||
import java.time.LocalDate | ||
|
||
data class UpdateUserInfoRequest( | ||
|
||
@Schema(description = "닉네임", example = "고작가") | ||
@field:Nickname | ||
val nickname: String?, | ||
var nickname: String?, | ||
|
||
@Schema(description = "성별") | ||
val gender: Gender?, | ||
var gender: Gender?, | ||
|
||
@Schema(description = "생일") | ||
val birthDay: LocalDate?, | ||
var birthDay: LocalDate?, | ||
|
||
@Schema(description = "한 줄 소개", example = "평범한 일상 속의 예술을 이야기합니다...") | ||
@field:Length(max = 25) | ||
val introduction: String?, | ||
) | ||
var introduction: String?, | ||
|
||
@Schema(description = "유저 프로필 이미지") | ||
var profileImage: MultipartFile?, | ||
) { | ||
fun toCommand(userId: Long): UpdateUserInfoCommand = | ||
UpdateUserInfoCommand( | ||
id = userId, | ||
nickname = this.nickname, | ||
gender = this.gender, | ||
birthDay = this.birthDay, | ||
introduction = this.introduction, | ||
profileImage = this.profileImage, | ||
) | ||
} |
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
8 changes: 8 additions & 0 deletions
8
src/main/kotlin/com/routebox/routebox/domain/common/FileManager.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,8 @@ | ||
package com.routebox.routebox.domain.common | ||
|
||
import com.routebox.routebox.domain.common.dto.FileInfo | ||
import org.springframework.web.multipart.MultipartFile | ||
|
||
interface FileManager { | ||
fun upload(file: MultipartFile, uploadPath: String): FileInfo | ||
} |
6 changes: 6 additions & 0 deletions
6
src/main/kotlin/com/routebox/routebox/domain/common/dto/FileInfo.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,6 @@ | ||
package com.routebox.routebox.domain.common.dto | ||
|
||
data class FileInfo( | ||
val storedFileName: String, | ||
val fileUrl: String, | ||
) |
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
53 changes: 53 additions & 0 deletions
53
src/main/kotlin/com/routebox/routebox/infrastructure/common/AmazonS3FileManager.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,53 @@ | ||
package com.routebox.routebox.infrastructure.common | ||
|
||
import com.amazonaws.services.s3.AmazonS3 | ||
import com.amazonaws.services.s3.model.CannedAccessControlList | ||
import com.amazonaws.services.s3.model.ObjectMetadata | ||
import com.amazonaws.services.s3.model.PutObjectRequest | ||
import com.routebox.routebox.domain.common.FileManager | ||
import com.routebox.routebox.domain.common.dto.FileInfo | ||
import com.routebox.routebox.properties.AwsProperties | ||
import org.springframework.stereotype.Component | ||
import org.springframework.web.multipart.MultipartFile | ||
import java.util.UUID | ||
|
||
@Component | ||
class AmazonS3FileManager( | ||
private val s3Client: AmazonS3, | ||
private val awsProperties: AwsProperties, | ||
) : FileManager { | ||
override fun upload(file: MultipartFile, uploadPath: String): FileInfo { | ||
val originalFileName = file.originalFilename ?: "" | ||
val storedFileName = generateStoredFileName(originalFileName, uploadPath) | ||
|
||
s3Client.putObject( | ||
PutObjectRequest( | ||
awsProperties.s3.bucketName, | ||
storedFileName, | ||
file.inputStream, | ||
generateObjectMetadata(file), | ||
).withCannedAcl(CannedAccessControlList.PublicRead), | ||
) | ||
val fileUrl = s3Client.getUrl(awsProperties.s3.bucketName, storedFileName).toString() | ||
|
||
return FileInfo(storedFileName, fileUrl) | ||
} | ||
|
||
private fun generateStoredFileName(originalFileName: String, uploadPath: String): String { | ||
val uuid = UUID.randomUUID().toString() | ||
|
||
val posOfExtension = originalFileName.lastIndexOf(".") | ||
if (posOfExtension == -1) { | ||
return uploadPath + uuid | ||
} | ||
|
||
val extension = originalFileName.substring(posOfExtension + 1) | ||
return "$uploadPath$uuid.$extension" | ||
} | ||
|
||
private fun generateObjectMetadata(file: MultipartFile) = | ||
ObjectMetadata().apply { | ||
this.contentType = file.contentType | ||
this.contentLength = file.size | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
src/main/kotlin/com/routebox/routebox/infrastructure/config/AmazonS3Config.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,26 @@ | ||
package com.routebox.routebox.infrastructure.config | ||
|
||
import com.amazonaws.auth.AWSStaticCredentialsProvider | ||
import com.amazonaws.auth.BasicAWSCredentials | ||
import com.amazonaws.regions.Regions | ||
import com.amazonaws.services.s3.AmazonS3 | ||
import com.amazonaws.services.s3.AmazonS3ClientBuilder | ||
import com.routebox.routebox.properties.AwsProperties | ||
import org.springframework.context.annotation.Bean | ||
import org.springframework.context.annotation.Configuration | ||
|
||
@Configuration | ||
class AmazonS3Config(private val awsProperties: AwsProperties) { | ||
@Bean | ||
fun amazonS3Client(): AmazonS3 = | ||
AmazonS3ClientBuilder.standard() | ||
.withCredentials( | ||
AWSStaticCredentialsProvider( | ||
BasicAWSCredentials( | ||
awsProperties.s3.accessKey, | ||
awsProperties.s3.secretKey, | ||
), | ||
), | ||
).withRegion(Regions.AP_NORTHEAST_2).build() | ||
?: throw NoSuchElementException("AWS S3 Bucket에 엑세스 할 수 없습니다. 엑세스 정보와 bucket name을 다시 확인해주세요.") | ||
} |
15 changes: 15 additions & 0 deletions
15
src/main/kotlin/com/routebox/routebox/infrastructure/user/UserProfileImageRepository.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,15 @@ | ||
package com.routebox.routebox.infrastructure.user | ||
|
||
import com.routebox.routebox.domain.user.UserProfileImage | ||
import org.springframework.data.jpa.repository.JpaRepository | ||
import org.springframework.data.jpa.repository.Query | ||
import java.util.Optional | ||
|
||
// UserProfileImage의 경우 soft delete가 적용되어 있으므로 query 작성 시 이를 고려해야 한다. | ||
interface UserProfileImageRepository : JpaRepository<UserProfileImage, Long> { | ||
@Query("SELECT upi FROM UserProfileImage upi WHERE upi.deletedAt != null") | ||
override fun findById(id: Long): Optional<UserProfileImage> | ||
|
||
@Query("SELECT upi FROM UserProfileImage upi WHERE upi.deletedAt != null AND upi.userId = :userId") | ||
fun findByUserId(userId: Long): UserProfileImage? | ||
} |
12 changes: 12 additions & 0 deletions
12
src/main/kotlin/com/routebox/routebox/properties/AwsProperties.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,12 @@ | ||
package com.routebox.routebox.properties | ||
|
||
import org.springframework.boot.context.properties.ConfigurationProperties | ||
|
||
@ConfigurationProperties("aws") | ||
data class AwsProperties(val s3: S3) { | ||
data class S3( | ||
val accessKey: String, | ||
val secretKey: String, | ||
val bucketName: String, | ||
) | ||
} |
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.