Skip to content

Commit

Permalink
♻️ :: s3 refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
jyk1029 committed Nov 21, 2023
1 parent aaafd74 commit db5a3bf
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ enum class ErrorCode(
) {
PASSWORD_MISMATCED(400, "Password Mis Matched"),
FEIGN_BAD_REQUEST(400, "Feign Bad Request"),
BAD_FILE_EXTENSION(400, "Bad File Extension"),

TOKEN_EXPIRED(401, "Token Expired"),
TOKEN_INVALID(401, "Token Invalid"),
Expand Down
21 changes: 14 additions & 7 deletions src/main/kotlin/com/example/sharing/global/utils/aws/s3/S3Utils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import com.amazonaws.services.s3.model.CannedAccessControlList
import com.amazonaws.services.s3.model.ObjectMetadata
import com.amazonaws.services.s3.model.PutObjectRequest
import com.amazonaws.util.IOUtils
import com.example.sharing.global.utils.aws.s3.exception.BadFileExtensionException
import org.springframework.beans.factory.annotation.Value
import org.springframework.stereotype.Component
import org.springframework.web.multipart.MultipartFile
Expand All @@ -17,15 +18,10 @@ class S3Utils(

@Value("\${cloud.aws.s3.bucket}")
private val bucketName: String,

@Value("\${cloud.aws.s3.base-image-url}")
private val baseImageUrl: String,

@Value("\${cloud.aws.s3.prefix}")
private val prefix: String
) {
fun upload(file: MultipartFile): String {
val fileName = UUID.randomUUID().toString() + "_" + file.originalFilename
val ext = verificationFile(file)
val fileName = UUID.randomUUID().toString() + "." + ext
val objMeta = ObjectMetadata()

val bytes = IOUtils.toByteArray(file.inputStream)
Expand All @@ -41,4 +37,15 @@ class S3Utils(

return s3Client.getUrl(bucketName, bucketName + fileName).toString()
}

private fun verificationFile(file: MultipartFile): String {
val originalFilename = file.originalFilename
val ext = originalFilename!!.substring(originalFilename.lastIndexOf(".") + 1)

if (!(ext == "jpg" || ext == "jpeg" || ext == "png" || ext == "heic")) {
throw BadFileExtensionException.EXCEPTION
}
return ext
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.example.sharing.global.utils.aws.s3.exception

import com.example.sharing.global.error.exception.ErrorCode.*
import com.example.sharing.global.error.exception.SharingException

class BadFileExtensionException : SharingException(BAD_FILE_EXTENSION){
companion object {
@JvmField
val EXCEPTION = BadFileExtensionException()
}
}

0 comments on commit db5a3bf

Please sign in to comment.