Skip to content
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

회원가입할때 프로필 업로드도 같이 할 수 있도록 변경 #59

Merged
merged 4 commits into from
Dec 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@ import com.msg.gauth.domain.auth.presentation.dto.response.RefreshResponseDto
import com.msg.gauth.domain.auth.presentation.dto.response.SigninResponseDto
import com.msg.gauth.domain.auth.services.*
import com.msg.gauth.domain.auth.presentation.dto.request.PasswordInitReqDto
import com.msg.gauth.domain.auth.presentation.dto.response.SignupImageResDto
import com.msg.gauth.domain.auth.services.InitPasswordService
import org.springframework.http.HttpStatus
import org.springframework.http.ResponseEntity
import org.springframework.web.bind.annotation.*
import org.springframework.web.multipart.MultipartFile
import javax.validation.Valid

@RestController
Expand All @@ -19,7 +21,8 @@ class AuthController(
private val logoutService: LogoutService,
private val signInService: SignInService,
private val signUpService: SignUpService,
private val initPasswordService: InitPasswordService
private val initPasswordService: InitPasswordService,
private val signupImageUploadService: SignupImageUploadService,
) {
@PatchMapping
fun refresh(@RequestHeader("RefreshToken") refreshToken: String): ResponseEntity<RefreshResponseDto> =
Expand All @@ -42,6 +45,10 @@ class AuthController(
return ResponseEntity(HttpStatus.CREATED)
}

@PatchMapping("/image")
fun uploadSignupImage(@RequestPart("image") image: MultipartFile, @RequestPart("imageUrl") previousUrl: String?): ResponseEntity<SignupImageResDto> =
ResponseEntity.ok(signupImageUploadService.execute(image, previousUrl))


@PatchMapping("/password/initialize")
fun initPassword(@Valid @RequestBody passwordInitReqDto: PasswordInitReqDto): ResponseEntity<Void> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,16 @@ data class SignUpDto(

@field:NotBlank
@field:Pattern(regexp = "^(?=.*[A-Za-z])(?=.*[0-9])(?=.*[\$@\$!%*#?&])[A-Za-z[0-9]\$@\$!%*#?&]{8,20}$")
val password: String
val password: String,

val profileUrl: String?,
) {
fun toEntity(password: String): User =
User(
email = email,
password = password,
roles = mutableListOf(UserRole.ROLE_STUDENT),
state = UserState.PENDING,
profileUrl = null,
profileUrl = profileUrl,
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.msg.gauth.domain.auth.presentation.dto.response

data class SignupImageResDto(
val imageUrl: String,
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.msg.gauth.domain.auth.services

import com.msg.gauth.domain.auth.presentation.dto.response.SignupImageResDto
import com.msg.gauth.global.aws.s3.S3Util
import org.springframework.stereotype.Service
import org.springframework.web.multipart.MultipartFile

@Service
class SignupImageUploadService(
private val s3Util: S3Util,
){
fun execute(image: MultipartFile, previousUrl: String?): SignupImageResDto{
if(previousUrl != null)
s3Util.deleteImage(previousUrl)
return SignupImageResDto(s3Util.upload(image))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,7 @@ import com.msg.gauth.domain.user.presentation.dto.request.PasswordChangeReqDto
import com.msg.gauth.domain.user.services.ChangePasswordService
import com.msg.gauth.domain.user.services.UploadProfileService
import org.springframework.http.ResponseEntity
import org.springframework.web.bind.annotation.PatchMapping
import org.springframework.web.bind.annotation.RequestBody
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RequestParam
import org.springframework.web.bind.annotation.RestController
import org.springframework.web.bind.annotation.*
import org.springframework.web.multipart.MultipartFile
import javax.validation.Valid

Expand All @@ -25,7 +21,7 @@ class UserController(
}

@PatchMapping("/image")
fun uploadProfile(@RequestParam("image") image: MultipartFile): ResponseEntity<Void>{
fun uploadProfile(@RequestPart("image") image: MultipartFile): ResponseEntity<Void>{
uploadProfileService.execute(image)
return ResponseEntity.noContent().build()
}
Expand Down
5 changes: 5 additions & 0 deletions src/main/kotlin/com/msg/gauth/global/aws/s3/S3Util.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,9 @@ class S3Util(
amazonS3.putObject(bucket, profileName, file.inputStream, metadata)
return amazonS3.getUrl(bucket, profileName).toString()
}

fun deleteImage(url: String){
val key = url.split("/")[3]
amazonS3.deleteObject(bucket, key)
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.msg.gauth.global.security

import com.fasterxml.jackson.databind.ObjectMapper
import com.msg.gauth.domain.user.enums.UserRole
import com.msg.gauth.global.security.config.FilterConfig
import com.msg.gauth.global.security.jwt.JwtTokenProvider
import org.springframework.context.annotation.Bean
Expand All @@ -15,7 +14,6 @@ import org.springframework.security.crypto.password.PasswordEncoder
import org.springframework.security.web.SecurityFilterChain
import org.springframework.security.web.util.matcher.RequestMatcher
import org.springframework.web.cors.CorsUtils
import javax.servlet.http.HttpServletRequest

@Configuration
@EnableWebSecurity
Expand Down Expand Up @@ -45,6 +43,8 @@ class SecurityConfig(
.antMatchers(HttpMethod.DELETE, "/auth").authenticated()
.antMatchers(HttpMethod.POST, "/auth/signup").permitAll()
.antMatchers(HttpMethod.PATCH, "/auth/password/initialize").permitAll()
.antMatchers(HttpMethod.PATCH, "/auth/image").permitAll()
.antMatchers(HttpMethod.DELETE, "/auth/image").permitAll()

// Email
.antMatchers(HttpMethod.POST, "/email").permitAll()
Expand Down