Skip to content

Commit

Permalink
Merge pull request #357 from tlsgmltjd/fix/profile-img-extension-vali…
Browse files Browse the repository at this point in the history
…dation

이미지 업로드시 확장자 vadliation 추가
  • Loading branch information
esperar committed Jul 4, 2024
2 parents 0ccfa2d + 8709c42 commit 4a10ff8
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 18 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.dotori.v2.domain.member.exception

import com.dotori.v2.global.error.ErrorCode
import com.dotori.v2.global.error.exception.BasicException

class NotAcceptImgExtensionException : BasicException(ErrorCode.MEMBER_PROFILE_IMG_NOT_ACCEPT_EXTENSION)
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.dotori.v2.domain.member.service

import com.dotori.v2.domain.member.domain.entity.Member
import com.dotori.v2.domain.member.exception.NotAcceptImgExtensionException
import com.dotori.v2.global.config.redis.service.RedisCacheService
import com.dotori.v2.global.thirdparty.aws.s3.S3Service
import org.springframework.stereotype.Service
import org.springframework.web.multipart.MultipartFile

@Service
class ProfileImageService (
private val s3Service: S3Service,
private val redisCacheService: RedisCacheService
) {
fun imageUpload(member: Member, multipartFiles: MultipartFile?, isUpdate: Boolean = false) {

validateExtension(multipartFiles)

val uploadFileUrl: String? = s3Service.uploadSingleFile(multipartFiles)

if (isUpdate) s3Service.deleteFile(member.profileImage!!)

member.updateProfileImage(uploadFileUrl)

redisCacheService.updateCacheFromProfile(member.id, uploadFileUrl)
}

private fun validateExtension(multipartFiles: MultipartFile?) {
val acceptList = listOf("jpg", "jpeg", "png")
val splitFile = multipartFiles?.originalFilename.toString().split(".")
val extension = splitFile.last().lowercase()

if (acceptList.none { it == extension })
throw NotAcceptImgExtensionException()
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package com.dotori.v2.domain.member.service.impl

import com.dotori.v2.domain.member.domain.entity.Member
import com.dotori.v2.domain.member.service.ProfileImageService
import com.dotori.v2.domain.member.service.UpdateProfileImageService
import com.dotori.v2.global.config.redis.service.RedisCacheService
import com.dotori.v2.global.thirdparty.aws.s3.S3Service
import com.dotori.v2.global.util.UserUtil
import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional
Expand All @@ -12,15 +12,10 @@ import org.springframework.web.multipart.MultipartFile
@Transactional(rollbackFor = [Exception::class])
class UpdateProfileImageServiceImpl(
private val userUtil: UserUtil,
private val s3Service: S3Service,
private val redisCacheService: RedisCacheService
private val profileImageService: ProfileImageService
): UpdateProfileImageService {
override fun execute(multipartFiles: MultipartFile?) {
val member = userUtil.fetchCurrentUser()
var uploadFile: String? = s3Service.uploadSingleFile(multipartFiles)
s3Service.deleteFile(member.profileImage!!)
member.updateProfileImage(uploadFile)

redisCacheService.updateCacheFromProfile(member.id, uploadFile)
val member: Member = userUtil.fetchCurrentUser()
profileImageService.imageUpload(member = member, multipartFiles = multipartFiles, isUpdate = true)
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package com.dotori.v2.domain.member.service.impl

import com.dotori.v2.domain.member.domain.entity.Member
import com.dotori.v2.domain.member.service.ProfileImageService
import com.dotori.v2.domain.member.service.UploadProfileImageService
import com.dotori.v2.global.config.redis.service.RedisCacheService
import com.dotori.v2.global.thirdparty.aws.s3.S3Service
import com.dotori.v2.global.util.UserUtil
import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional
Expand All @@ -13,14 +12,10 @@ import org.springframework.web.multipart.MultipartFile
@Transactional(rollbackFor = [Exception::class])
class UploadProfileImageServiceImpl(
private val userUtil: UserUtil,
private val s3Service: S3Service,
private val redisCacheService: RedisCacheService
private val profileImageService: ProfileImageService
): UploadProfileImageService {
override fun execute(multipartFiles: MultipartFile?) {
val member: Member = userUtil.fetchCurrentUser()
var uploadFile: String? = s3Service.uploadSingleFile(multipartFiles)
member.updateProfileImage(uploadFile)

redisCacheService.updateCacheFromProfile(member.id, uploadFile)
profileImageService.imageUpload(member = member, multipartFiles = multipartFiles)
}
}
1 change: 1 addition & 0 deletions src/main/kotlin/com/dotori/v2/global/error/ErrorCode.kt
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ enum class ErrorCode(
MEMBER_EMAIL_HAS_NOT_AUTH_KEY(HttpStatus.NOT_FOUND.value(), "인증번호가 존재 하지 않습니다"),
MEMBER_EMAIL_HAS_NOT_BEEN_CERTIFICATE(HttpStatus.ACCEPTED.value(), "이메일 인증이 되지않았습니다."),
MEMBER_NOT_SAME(HttpStatus.UNAUTHORIZED.value(), "유저가 일치하지 않습니다."),
MEMBER_PROFILE_IMG_NOT_ACCEPT_EXTENSION(HttpStatus.BAD_REQUEST.value(), "지원하지 않는 프로필 사진 확장자 입니다."),


// *** SELF STUDY ***
Expand Down

0 comments on commit 4a10ff8

Please sign in to comment.