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 12ea580 commit 41e6b1a
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -162,21 +162,23 @@ class AdminFacade(
}
}

// fun makeBlurImage() {
// userProfileService.findAllWithImage().forEach {
// val imageFile = amazonS3FileService.download(it.imageUrl!!, System.getProperty("java.io.tmpdir"))
// val blurImage = imageProcessor.blurImage(imageFile)
// val key = makePathWithUserId(imageFile, it.user.id)
// amazonS3FileService.upload(blurImage, key)
// }
// }
fun makeBlurImage() {
userProfileService.findAllWithImage().forEach {
val imageFile = amazonS3FileService.downloadAsMultipartFile(it.imageUrl!!)
val path = makePathWithUserId(imageFile, it.user.id)
val blurredImageUrl = imageUploader.uploadWithBlur(imageFile, path);

userProfileService.addBlurImageUrl(it.id, blurredImageUrl.toString())
}
}

fun makePathWithUserId(
file: MultipartFile,
userId: Long
) = "" + userId + FILE_NAME_DELIMITER + LocalDateTime.now()
.format(DateTimeFormatter.ofPattern("yyyyMMddHHmmss")) + FILE_NAME_DELIMITER + file.originalFilename


companion object {
private const val FILE_NAME_DELIMITER = "_"
private const val mockMaleUserId = 1L
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import com.amazonaws.services.s3.model.PutObjectRequest
import org.springframework.beans.factory.annotation.Value
import org.springframework.stereotype.Component
import org.springframework.web.multipart.MultipartFile
import java.io.File
import java.io.*
import java.net.URL
import java.nio.file.Files
import java.nio.file.Paths
Expand Down Expand Up @@ -53,14 +53,46 @@ class AmazonS3FileService(
return amazonS3.getUrl(bucket, key)
}

fun download(key: String, downloadFilePath: String): File {
fun downloadAsMultipartFile(key: String): MultipartFile {
// S3에서 파일 가져오기
val s3Object = amazonS3.getObject(GetObjectRequest(bucket, key))
val inputStream = s3Object.objectContent

val filePath = Paths.get(downloadFilePath)
Files.copy(inputStream, filePath)
inputStream.close()
// 바이트 배열로 변환
val outputStream = ByteArrayOutputStream()
inputStream.use { input ->
outputStream.use { output ->
input.copyTo(output)
}
}

// S3 파일 이름 및 MIME 타입 설정
val fileName = key.substringAfterLast("/")
val contentType = Files.probeContentType(Paths.get(fileName)) ?: "application/octet-stream"

// CustomMultipartFile로 변환
return CustomMultipartFile(
outputStream.toByteArray(),
fileName,
contentType
)
}
}

class CustomMultipartFile(
private val content: ByteArray,
private val fileName: String,
private val contentType: String
) : MultipartFile {

return filePath.toFile()
override fun getName(): String = fileName
override fun getOriginalFilename(): String = fileName
override fun getContentType(): String = contentType
override fun isEmpty(): Boolean = content.isEmpty()
override fun getSize(): Long = content.size.toLong()
override fun getBytes(): ByteArray = content
override fun getInputStream(): InputStream = ByteArrayInputStream(content)
override fun transferTo(dest: File) {
FileOutputStream(dest).use { it.write(content) }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ interface UserAlimyRepository: JpaRepository<UserAlimy, Long> {

fun findAllByUserId(userId: Long): List<UserAlimy>

fun findAllByUserIds(userIds: Set<Long>): List<UserAlimy>
fun findAllByUserIdIn(userIds: Set<Long>): List<UserAlimy>
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@ package com.nexters.bottles.app.user.repository

import com.nexters.bottles.app.user.domain.UserProfile
import org.springframework.data.jpa.repository.JpaRepository
import org.springframework.data.jpa.repository.Query

interface UserProfileRepository : JpaRepository<UserProfile, Long> {

fun findByUserId(userId: Long): UserProfile?

@Query("SELECT up FROM UserProfile up JOIN FETCH up.user")
fun findAllWithUser(): List<UserProfile>
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class UserAlimyService(

@Transactional(readOnly = true)
fun findAllowedUserAlimyByUserIdsAndAlimyType(userIds: Set<Long>, alimyType: AlimyType): List<UserAlimy> {
return userAlimyRepository.findAllByUserIds(userIds)
return userAlimyRepository.findAllByUserIdIn(userIds)
.filter { it.alimyType == alimyType }
.filter { it.enabled }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,13 @@ class UserProfileService(

@Transactional(readOnly = true)
fun findAllWithImage(): List<UserProfile> {
return profileRepository.findAll().filter { it.imageUrl != null }
return profileRepository.findAllWithUser().filter { it.imageUrl != null }
}

@Transactional
fun addBlurImageUrl(id: Long, blurredImageUrl: String) {
profileRepository.findByIdOrNull(id)?.let {
it.blurredImageUrl = blurredImageUrl
}
}
}

0 comments on commit 41e6b1a

Please sign in to comment.