Skip to content

Commit

Permalink
feat: 블러 이미지 등록 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
injoon2019 committed Sep 18, 2024
1 parent ba39ab3 commit 12303e2
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import com.nexters.bottles.api.user.facade.dto.UserProfileResponse
import com.nexters.bottles.api.user.facade.dto.UserProfileStatus
import com.nexters.bottles.api.user.facade.dto.UserProfileStatusResponse
import com.nexters.bottles.app.common.component.FileService
import com.nexters.bottles.app.common.component.ImageUploader
import com.nexters.bottles.app.user.domain.User
import com.nexters.bottles.app.user.domain.UserProfile
import com.nexters.bottles.app.user.domain.UserProfileSelect
Expand All @@ -29,7 +30,8 @@ import java.time.format.DateTimeFormatter
class UserProfileFacade(
private val profileService: UserProfileService,
private val userService: UserService,
private val fileService: FileService
private val fileService: FileService,
private val imageUploader: ImageUploader,
) {

private val log = KotlinLogging.logger { }
Expand Down Expand Up @@ -123,8 +125,9 @@ class UserProfileFacade(
val me = userService.findByIdAndNotDeleted(userId)
val path = makePathWithUserId(file, me.id)
val originalImageUrl = fileService.upload(file, path)
val blurredImageUrl = imageUploader.uploadWithBlur(file, path);

profileService.uploadImageUrl(me, originalImageUrl.toString())
profileService.uploadImageUrl(me, originalImageUrl.toString(), blurredImageUrl.toString())
}

private fun makePathWithUserId(
Expand Down
1 change: 1 addition & 0 deletions api/src/main/resources/sql/ddl/table_query.sql
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ CREATE TABLE user_profile
profile_select JSON,
introduction JSON,
image_url VARCHAR(2048),
blurred_image_url VARCHAR(2048),
created_at DATETIME DEFAULT CURRENT_TIMESTAMP NOT NULL,
updated_at DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP NOT NULL
);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.nexters.bottles.app.common.component

import org.springframework.stereotype.Component
import java.awt.image.BufferedImage
import java.awt.image.ConvolveOp
import java.awt.image.Kernel
import java.io.File
import javax.imageio.ImageIO
import kotlin.math.sqrt

@Component
class ImageProcessor {

fun blurImage(file: File): BufferedImage {
val image = ImageIO.read(file)
val blurredImage = applyGaussianBlur(image)
return blurredImage
}

private fun applyGaussianBlur(image: BufferedImage): BufferedImage {
val matrix = FloatArray(1225) { 1 / 1225f }
val size = sqrt(matrix.size.toDouble()).toInt()
val kernel = Kernel(size, size, matrix)
val convolveOp = ConvolveOp(kernel, ConvolveOp.EDGE_ZERO_FILL, null)

val blurredImage = BufferedImage(image.width, image.height, image.type)
convolveOp.filter(image, blurredImage)
return blurredImage
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package com.nexters.bottles.app.common.component

import org.springframework.stereotype.Component
import org.springframework.web.multipart.MultipartFile
import java.io.File
import java.net.URL
import java.nio.file.Files
import java.nio.file.Paths
import java.time.LocalDateTime
import javax.imageio.ImageIO

@Component
class ImageUploader(
private val imageProcessor: ImageProcessor,
private val fileService: FileService
) {

fun upload(file: MultipartFile, path: String): URL {
return fileService.upload(file, path)
}

fun uploadWithBlur(file: MultipartFile, path: String): URL {
val uploadDir = Paths.get("uploads")
if (!Files.exists(uploadDir)) {
Files.createDirectories(uploadDir)
}

val originalFilePath = uploadDir.resolve("original_" + LocalDateTime.now() + file.originalFilename)
Files.copy(file.inputStream, originalFilePath)

val blurredImage = imageProcessor.blurImage(File(originalFilePath.toString()))

val blurredFilePath = uploadDir.resolve("blurred_" + LocalDateTime.now() + file.originalFilename)
ImageIO.write(blurredImage, "jpg", File(blurredFilePath.toString()))

val imageUrlBlur = fileService.upload(blurredFilePath.toString(), "blurred_${path}")

Files.deleteIfExists(originalFilePath)
Files.deleteIfExists(blurredFilePath)

return imageUrlBlur
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ class UserProfile(
var introduction: List<QuestionAndAnswer> = arrayListOf(),

var imageUrl: String? = null,

var blurredImageUrl: String? = null,
) : BaseEntity() {

fun hasCompleteIntroduction(): Boolean {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,10 @@ class UserProfileService(
}

@Transactional
fun uploadImageUrl(user: User, imageUrl: String) {
fun uploadImageUrl(user: User, imageUrl: String, blurredImageUrl: String) {
profileRepository.findByUserId(user.id)?.let {
it.imageUrl = imageUrl
it.blurredImageUrl = blurredImageUrl
} ?: throw IllegalArgumentException("고객센터에 문의해주세요")
}

Expand Down

0 comments on commit 12303e2

Please sign in to comment.