Skip to content

Commit e65dc83

Browse files
authored
Merge pull request #84 from newjeans-bunnies/83-aws-sdk-for-java-2x
83 aws sdk for java 2x
2 parents ef2a09b + db50349 commit e65dc83

23 files changed

+245
-306
lines changed

build.gradle.kts

+8-4
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ allOpen {
3737
dependencyManagement {
3838

3939
imports {
40-
mavenBom ("org.springframework.cloud:spring-cloud-dependencies:2022.0.3")
40+
mavenBom("org.springframework.cloud:spring-cloud-dependencies:2022.0.3")
4141
}
4242
}
4343

@@ -58,9 +58,14 @@ dependencies {
5858
implementation("org.springframework.boot:spring-boot-starter-data-redis")
5959
implementation("org.springframework.boot:spring-boot-starter-oauth2-client")
6060
implementation("org.springframework.cloud:spring-cloud-starter-openfeign")
61+
implementation(platform("com.google.api-client:google-api-client-bom:2.6.0"))
62+
implementation(platform("software.amazon.awssdk:bom:2.29.45"))
6163

62-
implementation("com.google.api-client:google-api-client-jackson2:2.2.0")
63-
implementation("com.google.api-client:google-api-client:2.2.0")
64+
implementation("software.amazon.awssdk:s3")
65+
66+
67+
implementation("com.google.api-client:google-api-client-jackson2")
68+
implementation("com.google.api-client:google-api-client")
6469

6570
implementation("ognl:ognl:3.3.4")
6671

@@ -73,7 +78,6 @@ dependencies {
7378
implementation("io.viascom.nanoid:nanoid:1.0.1")
7479

7580
// s3
76-
implementation("com.amazonaws:aws-java-sdk-s3:1.12.261")
7781

7882
implementation("com.bucket4j:bucket4j-core:8.3.0")
7983

src/main/kotlin/com/photo/server/starsnap/domain/auth/controller/ValidController.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import org.springframework.web.bind.annotation.RequestParam
88
import org.springframework.web.bind.annotation.RestController
99

1010
@RestController
11-
@RequestMapping("/auth/valid")
11+
@RequestMapping("/api/auth/valid")
1212
class ValidController(
1313
private val validService: ValidService
1414
) {

src/main/kotlin/com/photo/server/starsnap/domain/auth/dto/EmailDto.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ data class EmailDto(
88
regexp = "^[a-zA-Z0-9+-_.]+@[0-9a-zA-Z]+\\.[a-zA-Z]{2,3}\$",
99
message = "only email"
1010
) val email: String, @field:NotBlank(message = "VerifyCode can't be blank.") @field:Pattern(
11-
regexp = "^\\d{6}\$", message = "Verify Code can only be 6 characters long"
11+
regexp = "^\\d{4}\$", message = "Verify Code can only be 4 characters long"
1212
) val verifyCode: String
1313
)
1414

src/main/kotlin/com/photo/server/starsnap/domain/auth/service/Oauth2Service.kt

+3-3
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ import com.photo.server.starsnap.domain.auth.type.toOauth2
1717
import com.photo.server.starsnap.domain.user.entity.Oauth2Entity
1818
import com.photo.server.starsnap.domain.user.entity.UserEntity
1919
import com.photo.server.starsnap.domain.user.repository.UserRepository
20-
import com.photo.server.starsnap.domain.user.service.UserAwsS3Service
2120
import com.photo.server.starsnap.global.security.jwt.JwtProvider
21+
import com.photo.server.starsnap.global.service.AwsS3Service
2222
import org.springframework.stereotype.Service
2323

2424
@Service
@@ -29,7 +29,7 @@ class Oauth2Service(
2929
private val appleOauthHelper: AppleOauthHelper,
3030
private val refreshTokenRepository: RefreshTokenRepository,
3131
private val jwtProvider: JwtProvider,
32-
private val userAwsS3Service: UserAwsS3Service
32+
private val awsS3Service: AwsS3Service
3333
) {
3434
fun login(loginDto: Oauth2LoginDto): TokenDto {
3535
val oidcDecodePayload = when (loginDto.type.toOauth2()) {
@@ -69,7 +69,7 @@ class Oauth2Service(
6969
userId = user
7070
)
7171
oauth2Repository.save(oauth2)
72-
userAwsS3Service.addOauthProfileImage(user.id, oidcDecodePayload.profileImageUrl)
72+
awsS3Service.uploadUrlToS3(oidcDecodePayload.profileImageUrl, "profile/${user.id}", user.id)
7373
}
7474

7575
fun unconnected(idToken: String, user: UserEntity, type: String) {

src/main/kotlin/com/photo/server/starsnap/domain/auth/service/ValidService.kt

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,15 @@ class ValidService(
1515
) {
1616
fun validUsername(username: String): StatusDto {
1717
val usernameExists = userRepository.existsByUsername(username)
18-
if(!usernameExists) throw ExistUserNameException
18+
if(usernameExists) throw ExistUserNameException
1919
if (!Pattern.USERNAME.toPattern().matcher(username).matches()) throw InvalidUserNameFormatException
2020

2121
return StatusDto("사용가능한 닉네임입니다.", 200)
2222
}
2323

2424
fun validEmail(email: String): StatusDto {
2525
val emailExists = userRepository.existsByEmail(email)
26-
if(!emailExists) throw ExistEmailException
26+
if(emailExists) throw ExistEmailException
2727
if(!Pattern.EMAIL.toPattern().matcher(email).matches()) throw InvalidEmailFormatException
2828

2929
return StatusDto("사용가능한 이메일입니다", 200)

src/main/kotlin/com/photo/server/starsnap/domain/snap/controller/SnapController.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ class SnapController(
5858
@ModelAttribute @Valid snapDto: UpdateSnapRequestDto,
5959
@AuthenticationPrincipal user: CustomUserDetails
6060
): SnapResponseDto {
61-
val snapData = snapService.updateSnap(user.userId, snapDto)
61+
val snapData = snapService.updateSnap(user.userId, snapDto, user.user)
6262
return snapData
6363
}
6464

src/main/kotlin/com/photo/server/starsnap/domain/snap/repository/SnapRepository.kt

+6-6
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,14 @@ interface SnapRepository : JpaRepository<SnapEntity, String> {
2727
AND (:blockUser IS NULL OR snap.user NOT IN :blockUser)
2828
AND (:tags IS NULL OR snap.tags IN :tags)
2929
AND (:title IS NULL OR snap.title LIKE %:title%)
30-
AND (:user IS NULL OR snap.user IN :user)
30+
AND (:user IS NULL OR snap.user.id IN :userId)
3131
""")
3232
fun findFilteredSnaps(
3333
pageable: Pageable,
3434
state: Boolean,
35-
blockUser: List<UserEntity>?,
36-
tags: List<String>?,
37-
title: String?,
38-
user: UserEntity?
39-
): Slice<SnapEntity>
35+
blockUser: List<UserEntity>? = null,
36+
tags: List<String>? = null,
37+
title: String? = null,
38+
userId: String? = null
39+
): Slice<SnapEntity>?
4040
}

src/main/kotlin/com/photo/server/starsnap/domain/snap/service/DeleteSnapService.kt

+9-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ import com.photo.server.starsnap.global.dto.SnapDto
88
import com.photo.server.starsnap.global.dto.toSnapDto
99
import com.photo.server.starsnap.global.error.exception.InvalidRoleException
1010
import jakarta.transaction.Transactional
11+
import org.springframework.data.domain.PageRequest
1112
import org.springframework.data.domain.Slice
13+
import org.springframework.data.domain.Sort
1214
import org.springframework.data.repository.findByIdOrNull
1315
import org.springframework.stereotype.Service
1416

@@ -19,7 +21,13 @@ class DeleteSnapService(
1921
) {
2022
@Transactional
2123
fun getDeleteSnap(page: Int, size: Int, userId: String): Slice<SnapDto> {
22-
val snaps = snapRepository.findSliceByStateAndUserId(false, userId) ?: throw NotFoundSnapIdException
24+
val pageRequest = PageRequest.of(
25+
page, size, Sort.by(
26+
Sort.Direction.DESC, "createdAt"
27+
)
28+
)
29+
val snaps =
30+
snapRepository.findFilteredSnaps(pageRequest, false, userId = userId) ?: throw NotFoundSnapIdException
2331
return snaps.map {
2432
it.toSnapDto()
2533
}

src/main/kotlin/com/photo/server/starsnap/domain/snap/service/SnapAwsS3Service.kt

-66
This file was deleted.

src/main/kotlin/com/photo/server/starsnap/domain/snap/service/SnapService.kt

+9-6
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import com.photo.server.starsnap.domain.snap.entity.SnapEntity
66
import com.photo.server.starsnap.domain.snap.dto.SnapResponseDto
77
import com.photo.server.starsnap.domain.snap.dto.UpdateSnapRequestDto
88
import com.photo.server.starsnap.domain.snap.entity.TagEntity
9+
import com.photo.server.starsnap.domain.snap.error.exception.NotFoundSnapException
910
import com.photo.server.starsnap.domain.snap.error.exception.NotFoundSnapIdException
1011
import com.photo.server.starsnap.domain.snap.error.exception.NotFoundTagException
1112
import com.photo.server.starsnap.domain.snap.error.exception.UnsupportedFileTypeException
@@ -18,6 +19,7 @@ import com.photo.server.starsnap.domain.user.repository.UserRepository
1819
import com.photo.server.starsnap.global.dto.toSnapDto
1920
import com.photo.server.starsnap.global.dto.toSnapUserDto
2021
import com.photo.server.starsnap.global.error.exception.InvalidRoleException
22+
import com.photo.server.starsnap.global.service.AwsS3Service
2123
import com.photo.server.starsnap.global.utils.type.isValid
2224
import io.viascom.nanoid.NanoId
2325
import jakarta.transaction.Transactional
@@ -34,7 +36,7 @@ import javax.imageio.ImageIO
3436
class SnapService(
3537
private val snapRepository: SnapRepository,
3638
private val tagRepository: TagRepository,
37-
private val snapAwsS3Service: SnapAwsS3Service,
39+
private val awsS3Service: AwsS3Service,
3840
private val blackUserRepository: BlackUserRepository,
3941
private val userRepository: UserRepository
4042
) {
@@ -48,7 +50,7 @@ class SnapService(
4850
val imageKey = NanoId.generate(16)
4951
try {
5052
val bufferedImage: BufferedImage = ImageIO.read(snapDto.image.inputStream)
51-
snapAwsS3Service.uploadImage(snapDto.image, imageKey)
53+
awsS3Service.uploadFileToS3(snapDto.image, imageKey, userData.id)
5254
val snapData = SnapEntity(
5355
title = snapDto.title,
5456
imageSize = snapDto.image.size,
@@ -84,12 +86,13 @@ class SnapService(
8486
@Transactional
8587
fun updateSnap(
8688
userId: String,
87-
snapDto: UpdateSnapRequestDto
89+
snapDto: UpdateSnapRequestDto,
90+
userData: UserEntity
8891
): SnapResponseDto {
8992
val snapData = snapRepository.findByIdOrNull(snapDto.snapId) ?: throw NotFoundSnapIdException
9093

9194
if (snapData.user.id != userId) throw InvalidRoleException
92-
if (snapDto.image != null) snapAwsS3Service.updateImage(snapDto.image, snapData.imageKey)
95+
awsS3Service.uploadFileToS3(snapDto.image, snapData.imageKey, userData.id)
9396

9497
snapData.title = snapDto.title
9598
snapData.source = snapDto.source
@@ -117,8 +120,8 @@ class SnapService(
117120
blockUser = blockUser,
118121
tags = getSnapResponseDto.tag,
119122
title = getSnapResponseDto.title,
120-
user = user
121-
)
123+
userId = user?.id
124+
) ?: throw NotFoundSnapException
122125

123126
return snapData.map {
124127
SnapResponseDto(

src/main/kotlin/com/photo/server/starsnap/domain/star/controller/StarController.kt

+10-7
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
package com.photo.server.starsnap.domain.star.controller
22

33
import com.photo.server.starsnap.domain.star.dto.*
4-
import com.photo.server.starsnap.domain.star.service.StarImageService
54
import com.photo.server.starsnap.domain.star.service.StarService
65
import com.photo.server.starsnap.global.dto.StatusDto
76
import com.photo.server.starsnap.global.security.principle.CustomUserDetails
7+
import com.photo.server.starsnap.global.service.AwsS3Service
88
import jakarta.validation.Valid
99
import org.springframework.security.core.annotation.AuthenticationPrincipal
1010
import org.springframework.web.bind.annotation.*
@@ -14,7 +14,7 @@ import org.springframework.web.multipart.MultipartFile
1414
@RequestMapping("api/star")
1515
class StarController(
1616
private val starService: StarService,
17-
private val starImageService: StarImageService,
17+
private val awsS3Service: AwsS3Service,
1818
) {
1919
@PostMapping("/create")
2020
fun createStar(@Valid @RequestBody starDto: CreateStarRequestDto, @AuthenticationPrincipal user: CustomUserDetails) {
@@ -46,25 +46,28 @@ class StarController(
4646
@PostMapping("image/upload")
4747
fun uploadImage(
4848
@RequestPart("image") image: MultipartFile, // 사진
49+
@AuthenticationPrincipal user: CustomUserDetails
4950
): StarImageResponseDto {
50-
val imageKey = starImageService.uploadImage(image, "star")
51+
val imageKey = awsS3Service.uploadFileToS3(image, "star", user.user.id)
5152
return StarImageResponseDto(imageKey)
5253
}
5354

5455
@PostMapping("image/update")
5556
fun updateImage(
5657
@RequestPart("image") image: MultipartFile, // 사진
57-
@RequestPart("image-key") imageKey: String
58+
@RequestPart("image-key") imageKey: String,
59+
@AuthenticationPrincipal user: CustomUserDetails
5860
): StatusDto {
59-
starImageService.updateImage(image, imageKey)
61+
awsS3Service.uploadFileToS3(image, imageKey, user.user.id)
6062
return StatusDto("OK", 200)
6163
}
6264

6365
@PatchMapping("image/delete")
6466
fun deleteImage(
65-
@RequestPart("image-key") imageKey: String
67+
@RequestPart("image-key") imageKey: String,
68+
@AuthenticationPrincipal user: CustomUserDetails
6669
): StatusDto {
67-
starImageService.deleteImage(imageKey)
70+
awsS3Service.deleteFileTos3(imageKey)
6871
return StatusDto("OK", 200)
6972
}
7073
}

src/main/kotlin/com/photo/server/starsnap/domain/star/controller/StarGroupController.kt

+8-6
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ package com.photo.server.starsnap.domain.star.controller
22

33
import com.photo.server.starsnap.domain.star.dto.*
44
import com.photo.server.starsnap.domain.star.service.StarGroupService
5-
import com.photo.server.starsnap.domain.star.service.StarImageService
65
import com.photo.server.starsnap.global.dto.StatusDto
76
import com.photo.server.starsnap.global.security.principle.CustomUserDetails
7+
import com.photo.server.starsnap.global.service.AwsS3Service
88
import jakarta.validation.Valid
99
import org.springframework.data.domain.Slice
1010
import org.springframework.security.core.annotation.AuthenticationPrincipal
@@ -15,7 +15,7 @@ import org.springframework.web.multipart.MultipartFile
1515
@RequestMapping("/api/star-group")
1616
class StarGroupController(
1717
private val starGroupService: StarGroupService,
18-
private val starImageService: StarImageService
18+
private val awsS3Service: AwsS3Service
1919
) {
2020
@GetMapping("/get")
2121
fun getStarGroups(
@@ -53,25 +53,27 @@ class StarGroupController(
5353
@PostMapping("image/upload")
5454
fun uploadImage(
5555
@RequestPart("image") image: MultipartFile, // 사진
56+
@AuthenticationPrincipal user: CustomUserDetails
5657
): StarGroupImageResponseDto {
57-
val imageKey = starImageService.uploadImage(image, "star-group")
58+
val imageKey = awsS3Service.uploadFileToS3(image, "star-group", user.user.id)
5859
return StarGroupImageResponseDto(imageKey)
5960
}
6061

6162
@PostMapping("image/update")
6263
fun updateImage(
6364
@RequestPart("image") image: MultipartFile, // 사진
64-
@RequestPart("image-key") imageKey: String
65+
@RequestPart("image-key") imageKey: String,
66+
@AuthenticationPrincipal user: CustomUserDetails
6567
): StatusDto {
66-
starImageService.updateImage(image, imageKey)
68+
awsS3Service.uploadFileToS3(image, imageKey, user.user.id)
6769
return StatusDto("OK", 200)
6870
}
6971

7072
@PatchMapping("image/delete")
7173
fun deleteImage(
7274
@RequestPart("image-key") imageKey: String
7375
): StatusDto {
74-
starImageService.deleteImage(imageKey)
76+
awsS3Service.deleteFileTos3(imageKey)
7577
return StatusDto("OK", 200)
7678
}
7779
}

0 commit comments

Comments
 (0)