feat: S3にアップロードできるように

This commit is contained in:
usbharu 2023-10-05 11:54:27 +09:00
parent 97cf5eac65
commit e4ac377404
15 changed files with 102 additions and 44 deletions

View File

@ -3,13 +3,17 @@ package dev.usbharu.hideout.config
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import software.amazon.awssdk.auth.credentials.AwsBasicCredentials
import software.amazon.awssdk.regions.Region
import software.amazon.awssdk.services.s3.S3Client
import java.net.URI
@Configuration
class AwsConfig {
@Bean
fun s3(awsConfig: StorageConfig): S3Client {
return S3Client.builder()
.endpointOverride(URI.create(awsConfig.endpoint))
.region(Region.of(awsConfig.region))
.credentialsProvider { AwsBasicCredentials.create(awsConfig.accessKey, awsConfig.secretKey) }
.build()
}

View File

@ -4,6 +4,7 @@ import dev.usbharu.hideout.controller.mastodon.generated.MediaApi
import dev.usbharu.hideout.domain.mastodon.model.generated.MediaAttachment
import dev.usbharu.hideout.domain.model.hideout.form.Media
import dev.usbharu.hideout.service.api.mastodon.MediaApiService
import kotlinx.coroutines.runBlocking
import org.springframework.http.ResponseEntity
import org.springframework.stereotype.Controller
import org.springframework.web.multipart.MultipartFile
@ -15,8 +16,8 @@ class MastodonMediaApiController(private val mediaApiService: MediaApiService) :
thumbnail: MultipartFile?,
description: String?,
focus: String?
): ResponseEntity<MediaAttachment> {
return ResponseEntity.ok(
): ResponseEntity<MediaAttachment> = runBlocking {
ResponseEntity.ok(
mediaApiService.postMedia(
Media(
file,

View File

@ -0,0 +1,6 @@
package dev.usbharu.hideout.domain.model.hideout.dto
data class ProcessedFile(
val byteArray: ByteArray,
val extension: String
)

View File

@ -0,0 +1,6 @@
package dev.usbharu.hideout.domain.model.hideout.dto
data class ProcessedMedia(
val file: ProcessedFile,
val thumbnail: ProcessedFile?
)

View File

@ -2,13 +2,21 @@ package dev.usbharu.hideout.service.api.mastodon
import dev.usbharu.hideout.domain.mastodon.model.generated.MediaAttachment
import dev.usbharu.hideout.domain.model.hideout.form.Media
import dev.usbharu.hideout.service.core.Transaction
import dev.usbharu.hideout.service.media.MediaService
import org.springframework.stereotype.Service
@Service
class MediaApiServiceImpl(private val mediaService: MediaService, private val transaction: Transaction) :
MediaApiService {
class MediaApiServiceImpl(private val mediaService: MediaService) : MediaApiService {
override suspend fun postMedia(media: Media): MediaAttachment {
val uploadLocalMedia = mediaService.uploadLocalMedia(media)
return MediaAttachment(
return transaction.transaction {
)
val uploadLocalMedia = mediaService.uploadLocalMedia(media)
return@transaction MediaAttachment(
)
}
}
}

View File

@ -28,6 +28,8 @@ class MediaServiceImpl(
private val mediaProcessService: MediaProcessService
) : MediaService {
override suspend fun uploadLocalMedia(media: Media): EntityMedia {
logger.info("Media upload. filename:${media.file.name} size:${media.file.size} contentType:${media.file.contentType}")
if (media.file.size == 0L) {
throw MediaFileSizeIsZeroException("Media file size is zero.")
}
@ -37,13 +39,19 @@ class MediaServiceImpl(
throw UnsupportedMediaException("FileType: $fileType is not supported.")
}
val process = mediaProcessService.process(fileType, media.file.bytes, media.thumbnail?.bytes)
val process = mediaProcessService.process(
fileType,
media.file.contentType.orEmpty(),
media.file.name,
media.file.bytes,
media.thumbnail?.bytes
)
val dataMediaSave = MediaSave(
UUID.randomUUID().toString(),
"${UUID.randomUUID()}.${process.file.extension}",
"",
process.first,
process.second
process.file.byteArray,
process.thumbnail?.byteArray
)
val save = try {
mediaDataStore.save(dataMediaSave)

View File

@ -33,7 +33,10 @@ class S3MediaDataStore(private val s3Client: S3Client, private val storageConfig
awaitAll(
async {
if (dataMediaSave.thumbnailInputStream != null) {
s3Client.putObject(fileUploadRequest, RequestBody.fromBytes(dataMediaSave.thumbnailInputStream))
s3Client.putObject(
thumbnailUploadRequest,
RequestBody.fromBytes(dataMediaSave.thumbnailInputStream)
)
"thumbnail" to s3Client.utilities()
.getUrl(GetUrlRequest.builder().bucket(storageConfig.bucket).key(thumbnailKey).build())
} else {
@ -41,7 +44,7 @@ class S3MediaDataStore(private val s3Client: S3Client, private val storageConfig
}
},
async {
s3Client.putObject(thumbnailUploadRequest, RequestBody.fromBytes(dataMediaSave.fileInputStream))
s3Client.putObject(fileUploadRequest, RequestBody.fromBytes(dataMediaSave.fileInputStream))
"file" to s3Client.utilities()
.getUrl(GetUrlRequest.builder().bucket(storageConfig.bucket).key(dataMediaSave.name).build())
}

View File

@ -1,8 +1,9 @@
package dev.usbharu.hideout.service.media
import dev.usbharu.hideout.domain.model.hideout.dto.ProcessedFile
import java.io.InputStream
interface ThumbnailGenerateService {
fun generate(bufferedImage: InputStream, width: Int, height: Int): ByteArray
fun generate(outputStream: ByteArray, width: Int, height: Int): ByteArray
fun generate(bufferedImage: InputStream, width: Int, height: Int): ProcessedFile?
fun generate(outputStream: ByteArray, width: Int, height: Int): ProcessedFile?
}

View File

@ -1,28 +1,27 @@
package dev.usbharu.hideout.service.media
import dev.usbharu.hideout.domain.model.hideout.dto.ProcessedFile
import org.springframework.stereotype.Service
import java.awt.image.BufferedImage
import java.io.ByteArrayOutputStream
import java.io.InputStream
import java.io.OutputStream
import javax.imageio.ImageIO
import javax.imageio.stream.MemoryCacheImageOutputStream
@Service
class ThumbnailGenerateServiceImpl : ThumbnailGenerateService {
override fun generate(bufferedImage: InputStream, width: Int, height: Int): ByteArrayOutputStream {
override fun generate(bufferedImage: InputStream, width: Int, height: Int): ProcessedFile? {
val image = ImageIO.read(bufferedImage)
return internalGenerate(image)
}
override fun generate(outputStream: OutputStream, width: Int, height: Int): ByteArrayOutputStream {
val image = ImageIO.read(MemoryCacheImageOutputStream(outputStream))
override fun generate(outputStream: ByteArray, width: Int, height: Int): ProcessedFile? {
val image = ImageIO.read(outputStream.inputStream())
return internalGenerate(image)
}
private fun internalGenerate(image: BufferedImage): ByteArrayOutputStream {
private fun internalGenerate(image: BufferedImage): ProcessedFile {
val byteArrayOutputStream = ByteArrayOutputStream()
ImageIO.write(image, "webp", byteArrayOutputStream)
return byteArrayOutputStream
ImageIO.write(image, "jpeg", byteArrayOutputStream)
return ProcessedFile(byteArrayOutputStream.toByteArray(), "jpg")
}
}

View File

@ -1,9 +1,10 @@
package dev.usbharu.hideout.service.media.converter
import dev.usbharu.hideout.domain.model.hideout.dto.FileType
import dev.usbharu.hideout.domain.model.hideout.dto.ProcessedFile
import java.io.InputStream
interface MediaConverter {
fun isSupport(fileType: FileType): Boolean
fun convert(inputStream: InputStream): ByteArray
fun convert(inputStream: InputStream): ProcessedFile
}

View File

@ -1,8 +1,14 @@
package dev.usbharu.hideout.service.media.converter
import dev.usbharu.hideout.domain.model.hideout.dto.FileType
import dev.usbharu.hideout.domain.model.hideout.dto.ProcessedFile
import java.io.InputStream
interface MediaConverterRoot {
suspend fun convert(fileType: FileType, inputStream: InputStream): ByteArray
suspend fun convert(
fileType: FileType,
contentType: String,
filename: String,
inputStream: InputStream
): ProcessedFile
}

View File

@ -1,6 +1,7 @@
package dev.usbharu.hideout.service.media.converter
import dev.usbharu.hideout.domain.model.hideout.dto.FileType
import dev.usbharu.hideout.domain.model.hideout.dto.ProcessedFile
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import org.springframework.stereotype.Service
@ -8,11 +9,24 @@ import java.io.InputStream
@Service
class MediaConverterRootImpl(private val converters: List<MediaConverter>) : MediaConverterRoot {
override suspend fun convert(fileType: FileType, inputStream: InputStream): ByteArray {
return converters.find {
override suspend fun convert(
fileType: FileType,
contentType: String,
filename: String,
inputStream: InputStream
): ProcessedFile {
val convert = converters.find {
it.isSupport(fileType)
}?.convert(inputStream) ?: withContext(Dispatchers.IO) {
inputStream.readAllBytes()
}?.convert(inputStream)
if (convert != null) {
return convert
}
return withContext(Dispatchers.IO) {
if (filename.contains('.')) {
ProcessedFile(inputStream.readAllBytes(), filename.substringAfterLast("."))
} else {
ProcessedFile(inputStream.readAllBytes(), contentType.substringAfterLast("/"))
}
}
}
}

View File

@ -1,11 +1,14 @@
package dev.usbharu.hideout.service.media.converter
import dev.usbharu.hideout.domain.model.hideout.dto.FileType
import dev.usbharu.hideout.domain.model.hideout.dto.ProcessedMedia
interface MediaProcessService {
suspend fun process(
fileType: FileType,
contentType: String,
fileName: String,
file: ByteArray,
thumbnail: ByteArray?
): Pair<ByteArray, ByteArray>
): ProcessedMedia
}

View File

@ -1,6 +1,7 @@
package dev.usbharu.hideout.service.media.converter
import dev.usbharu.hideout.domain.model.hideout.dto.FileType
import dev.usbharu.hideout.domain.model.hideout.dto.ProcessedMedia
import dev.usbharu.hideout.exception.media.MediaConvertException
import dev.usbharu.hideout.service.media.ThumbnailGenerateService
import org.slf4j.LoggerFactory
@ -13,26 +14,30 @@ class MediaProcessServiceImpl(
) : MediaProcessService {
override suspend fun process(
fileType: FileType,
contentType: String,
filename: String,
file: ByteArray,
thumbnail: ByteArray?
): Pair<ByteArray, ByteArray> {
): ProcessedMedia {
val fileInputStream = try {
mediaConverterRoot.convert(fileType, file.inputStream().buffered())
mediaConverterRoot.convert(fileType, contentType, filename, file.inputStream().buffered())
} catch (e: Exception) {
logger.warn("Failed convert media.", e)
throw MediaConvertException("Failed convert media.", e)
}
val thumbnailInputStream = try {
thumbnail?.let { mediaConverterRoot.convert(fileType, it.inputStream().buffered()) }
thumbnail?.let { mediaConverterRoot.convert(fileType, contentType, filename, it.inputStream().buffered()) }
} catch (e: Exception) {
logger.warn("Failed convert thumbnail media.", e)
null
}
return fileInputStream to thumbnailGenerateService.generate(
thumbnailInputStream ?: fileInputStream,
2048,
2048
return ProcessedMedia(
fileInputStream, thumbnailGenerateService.generate(
thumbnailInputStream?.byteArray ?: file,
2048,
2048
)
)
}

View File

@ -7,14 +7,7 @@ hideout:
key-id: a
private-key: "MIIEvwIBADANBgkqhkiG9w0BAQEFAASCBKkwggSlAgEAAoIBAQC7VJTUt9Us8cKjMzEfYyjiWA4R4/M2bS1GB4t7NXp98C3SC6dVMvDuictGeurT8jNbvJZHtCSuYEvuNMoSfm76oqFvAp8Gy0iz5sxjZmSnXyCdPEovGhLa0VzMaQ8s+CLOyS56YyCFGeJZqgtzJ6GR3eqoYSW9b9UMvkBpZODSctWSNGj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mYml3pfVLKGnzmng7H2+cwPLhPIzIuwytXywh2bzbsYEfYx3EoEVgMEpPhoarQnYPukrJO4gwE2o5Te6T5mJSZGlQJQj9q4ZB2Dfzet6INsK0oG8XVGXSpQvQh3RUYekCZQkBBFcpqWpbIEsCgYAnM3DQf3FJoSnXaMhrVBIovic5l0xFkEHskAjFTevO86Fsz1C2aSeRKSqGFoOQ0tmJzBEs1R6KqnHInicDTQrKhArgLXX4v3CddjfTRJkFWDbE/CkvKZNOrcf1nhaGCPspRJj2KUkj1Fhl9Cncdn/RsYEONbwQSjIfMPkvxF+8HQ=="
public-key: "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAu1SU1LfVLPHCozMxH2Mo4lgOEePzNm0tRgeLezV6ffAt0gunVTLw7onLRnrq0/IzW7yWR7QkrmBL7jTKEn5u+qKhbwKfBstIs+bMY2Zkp18gnTxKLxoS2tFczGkPLPgizskuemMghRniWaoLcyehkd3qqGElvW/VDL5AaWTg0nLVkjRo9z+40RQzuVaE8AkAFmxZzow3x+VJYKdjykkJ0iT9wCS0DRTXu269V264Vf/3jvredZiKRkgwlL9xNAwxXFg0x/XFw005UWVRIkdgcKWTjpBP2dPwVZ4WWC+9aGVd+Gyn1o0CLelf4rEjGoXbAAEgAqeGUxrcIlbjXfbcmwIDAQAB"
storage:
use-s3: true
endpoint: ""
public-url: ""
bucket: ""
region: ""
access-key: ""
secret-key: ""