mirror of https://github.com/usbharu/Hideout.git
feat: ローカルのアップロードの処理の一部が完成
This commit is contained in:
parent
7fa2e97368
commit
bbf3249c45
|
@ -10,9 +10,23 @@ class SpringConfig {
|
|||
|
||||
@Autowired
|
||||
lateinit var config: ApplicationConfig
|
||||
|
||||
@Autowired
|
||||
lateinit var storageConfig: StorageConfig
|
||||
}
|
||||
|
||||
@ConfigurationProperties("hideout")
|
||||
data class ApplicationConfig(
|
||||
val url: URL
|
||||
)
|
||||
|
||||
@ConfigurationProperties("hideout.storage")
|
||||
data class StorageConfig(
|
||||
val useS3: Boolean,
|
||||
val endpoint: String,
|
||||
val publicUrl: String,
|
||||
val bucket: String,
|
||||
val region: String,
|
||||
val accessKey: String,
|
||||
val secretKey: String
|
||||
)
|
||||
|
|
|
@ -6,5 +6,5 @@ data class MediaSave(
|
|||
val name: String,
|
||||
val prefix: String,
|
||||
val fileInputStream: InputStream,
|
||||
val thumbnailInputStream: InputStream
|
||||
val thumbnailInputStream: InputStream?
|
||||
)
|
||||
|
|
|
@ -0,0 +1,8 @@
|
|||
package dev.usbharu.hideout.domain.model.hideout.dto
|
||||
|
||||
enum class FileType {
|
||||
Image,
|
||||
Video,
|
||||
Audio,
|
||||
Unknown
|
||||
}
|
|
@ -0,0 +1,7 @@
|
|||
package dev.usbharu.hideout.domain.model.hideout.dto
|
||||
|
||||
data class RemoteMedia(
|
||||
val name: String,
|
||||
val url: String,
|
||||
val mediaType: String
|
||||
)
|
|
@ -1,4 +1,4 @@
|
|||
package dev.usbharu.hideout.service.media
|
||||
package dev.usbharu.hideout.domain.model.hideout.dto
|
||||
|
||||
sealed class SavedMedia(val success: Boolean)
|
||||
|
|
@ -1,6 +1,6 @@
|
|||
package dev.usbharu.hideout.domain.model.hideout.entity
|
||||
|
||||
import dev.usbharu.hideout.service.media.FileTypeDeterminationService
|
||||
import dev.usbharu.hideout.domain.model.hideout.dto.FileType
|
||||
|
||||
data class Media(
|
||||
val id: Long,
|
||||
|
@ -8,6 +8,6 @@ data class Media(
|
|||
val url: String,
|
||||
val remoteUrl: String?,
|
||||
val thumbnailUrl: String?,
|
||||
val type: FileTypeDeterminationService.FileType,
|
||||
val type: FileType,
|
||||
val blurHash: String?
|
||||
)
|
||||
|
|
|
@ -0,0 +1,14 @@
|
|||
package dev.usbharu.hideout.exception.media
|
||||
|
||||
open class MediaConvertException : MediaException {
|
||||
constructor() : super()
|
||||
constructor(message: String?) : super(message)
|
||||
constructor(message: String?, cause: Throwable?) : super(message, cause)
|
||||
constructor(cause: Throwable?) : super(cause)
|
||||
constructor(message: String?, cause: Throwable?, enableSuppression: Boolean, writableStackTrace: Boolean) : super(
|
||||
message,
|
||||
cause,
|
||||
enableSuppression,
|
||||
writableStackTrace
|
||||
)
|
||||
}
|
|
@ -0,0 +1,14 @@
|
|||
package dev.usbharu.hideout.exception.media
|
||||
|
||||
open class MediaFileSizeException : MediaException {
|
||||
constructor() : super()
|
||||
constructor(message: String?) : super(message)
|
||||
constructor(message: String?, cause: Throwable?) : super(message, cause)
|
||||
constructor(cause: Throwable?) : super(cause)
|
||||
constructor(message: String?, cause: Throwable?, enableSuppression: Boolean, writableStackTrace: Boolean) : super(
|
||||
message,
|
||||
cause,
|
||||
enableSuppression,
|
||||
writableStackTrace
|
||||
)
|
||||
}
|
|
@ -0,0 +1,14 @@
|
|||
package dev.usbharu.hideout.exception.media
|
||||
|
||||
class MediaFileSizeIsZeroException : MediaFileSizeException {
|
||||
constructor() : super()
|
||||
constructor(message: String?) : super(message)
|
||||
constructor(message: String?, cause: Throwable?) : super(message, cause)
|
||||
constructor(cause: Throwable?) : super(cause)
|
||||
constructor(message: String?, cause: Throwable?, enableSuppression: Boolean, writableStackTrace: Boolean) : super(
|
||||
message,
|
||||
cause,
|
||||
enableSuppression,
|
||||
writableStackTrace
|
||||
)
|
||||
}
|
|
@ -1,6 +1,6 @@
|
|||
package dev.usbharu.hideout.exception.media
|
||||
|
||||
open class MediaUploadException : MediaException {
|
||||
open class MediaSaveException : MediaException {
|
||||
constructor() : super()
|
||||
constructor(message: String?) : super(message)
|
||||
constructor(message: String?, cause: Throwable?) : super(message, cause)
|
|
@ -0,0 +1,14 @@
|
|||
package dev.usbharu.hideout.exception.media
|
||||
|
||||
class UnsupportedMediaException : MediaException {
|
||||
constructor() : super()
|
||||
constructor(message: String?) : super(message)
|
||||
constructor(message: String?, cause: Throwable?) : super(message, cause)
|
||||
constructor(cause: Throwable?) : super(cause)
|
||||
constructor(message: String?, cause: Throwable?, enableSuppression: Boolean, writableStackTrace: Boolean) : super(
|
||||
message,
|
||||
cause,
|
||||
enableSuppression,
|
||||
writableStackTrace
|
||||
)
|
||||
}
|
|
@ -1,9 +1,9 @@
|
|||
package dev.usbharu.hideout.repository
|
||||
|
||||
|
||||
import dev.usbharu.hideout.domain.model.hideout.dto.FileType
|
||||
import dev.usbharu.hideout.exception.FailedToGetResourcesException
|
||||
import dev.usbharu.hideout.service.core.IdGenerateService
|
||||
import dev.usbharu.hideout.service.media.FileTypeDeterminationService
|
||||
import dev.usbharu.hideout.util.singleOr
|
||||
import org.jetbrains.exposed.sql.*
|
||||
import org.jetbrains.exposed.sql.SqlExpressionBuilder.eq
|
||||
|
@ -63,7 +63,7 @@ class MediaRepositoryImpl(private val idGenerateService: IdGenerateService) : Me
|
|||
this[Media.url],
|
||||
this[Media.remoteUrl],
|
||||
this[Media.thumbnailUrl],
|
||||
FileTypeDeterminationService.FileType.values().first { it.ordinal == this[Media.type] },
|
||||
FileType.values().first { it.ordinal == this[Media.type] },
|
||||
this[Media.blurhash],
|
||||
)
|
||||
}
|
||||
|
|
|
@ -6,6 +6,9 @@ import dev.usbharu.hideout.service.media.MediaService
|
|||
|
||||
class MediaApiServiceImpl(private val mediaService: MediaService) : MediaApiService {
|
||||
override suspend fun postMedia(media: Media): MediaAttachment {
|
||||
mediaService.uploadLocalMedia(media)
|
||||
val uploadLocalMedia = mediaService.uploadLocalMedia(media)
|
||||
return MediaAttachment(
|
||||
|
||||
)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,12 +1,8 @@
|
|||
package dev.usbharu.hideout.service.media
|
||||
|
||||
import dev.usbharu.hideout.domain.model.hideout.dto.FileType
|
||||
|
||||
interface FileTypeDeterminationService {
|
||||
fun fileType(byteArray: ByteArray, filename: String, contentType: String?): FileType
|
||||
|
||||
enum class FileType {
|
||||
Image,
|
||||
Video,
|
||||
Audio,
|
||||
Unknown
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
package dev.usbharu.hideout.service.media
|
||||
|
||||
import dev.usbharu.hideout.domain.model.hideout.dto.FileType
|
||||
import org.springframework.stereotype.Component
|
||||
|
||||
@Component
|
||||
|
@ -8,19 +9,19 @@ class FileTypeDeterminationServiceImpl : FileTypeDeterminationService {
|
|||
byteArray: ByteArray,
|
||||
filename: String,
|
||||
contentType: String?
|
||||
): FileTypeDeterminationService.FileType {
|
||||
): FileType {
|
||||
if (contentType == null) {
|
||||
return FileTypeDeterminationService.FileType.Unknown
|
||||
return FileType.Unknown
|
||||
}
|
||||
if (contentType.startsWith("image")) {
|
||||
return FileTypeDeterminationService.FileType.Image
|
||||
return FileType.Image
|
||||
}
|
||||
if (contentType.startsWith("video")) {
|
||||
return FileTypeDeterminationService.FileType.Video
|
||||
return FileType.Video
|
||||
}
|
||||
if (contentType.startsWith("audio")) {
|
||||
return FileTypeDeterminationService.FileType.Audio
|
||||
return FileType.Audio
|
||||
}
|
||||
return FileTypeDeterminationService.FileType.Unknown
|
||||
return FileType.Unknown
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,8 +1,9 @@
|
|||
package dev.usbharu.hideout.service.media
|
||||
|
||||
import dev.usbharu.hideout.domain.model.MediaSave
|
||||
import dev.usbharu.hideout.domain.model.hideout.dto.SavedMedia
|
||||
|
||||
interface MediaDataStore {
|
||||
suspend fun save(dataMediaSave: MediaSave)
|
||||
suspend fun save(dataMediaSave: MediaSave): SavedMedia
|
||||
suspend fun delete(id: Long)
|
||||
}
|
||||
|
|
|
@ -1,7 +1,9 @@
|
|||
package dev.usbharu.hideout.service.media
|
||||
|
||||
import dev.usbharu.hideout.domain.model.hideout.dto.RemoteMedia
|
||||
import dev.usbharu.hideout.domain.model.hideout.form.Media
|
||||
|
||||
interface MediaService {
|
||||
suspend fun uploadLocalMedia(media: Media): SavedMedia
|
||||
suspend fun uploadLocalMedia(media: Media): dev.usbharu.hideout.domain.model.hideout.entity.Media
|
||||
suspend fun uploadRemoteMedia(remoteMedia: RemoteMedia)
|
||||
}
|
||||
|
|
|
@ -1,56 +1,87 @@
|
|||
package dev.usbharu.hideout.service.media
|
||||
|
||||
import dev.usbharu.hideout.domain.model.MediaSave
|
||||
import dev.usbharu.hideout.domain.model.hideout.dto.FaildSavedMedia
|
||||
import dev.usbharu.hideout.domain.model.hideout.dto.FileType
|
||||
import dev.usbharu.hideout.domain.model.hideout.dto.RemoteMedia
|
||||
import dev.usbharu.hideout.domain.model.hideout.dto.SuccessSavedMedia
|
||||
import dev.usbharu.hideout.domain.model.hideout.form.Media
|
||||
import dev.usbharu.hideout.exception.media.MediaException
|
||||
import dev.usbharu.hideout.exception.media.MediaFileSizeIsZeroException
|
||||
import dev.usbharu.hideout.exception.media.MediaSaveException
|
||||
import dev.usbharu.hideout.exception.media.UnsupportedMediaException
|
||||
import dev.usbharu.hideout.repository.MediaRepository
|
||||
import dev.usbharu.hideout.service.media.converter.MediaProcessService
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.withContext
|
||||
import org.slf4j.LoggerFactory
|
||||
import org.springframework.stereotype.Service
|
||||
import java.util.*
|
||||
import javax.imageio.ImageIO
|
||||
import dev.usbharu.hideout.domain.model.hideout.entity.Media as EntityMedia
|
||||
|
||||
@Service
|
||||
class MediaServiceImpl(
|
||||
private val mediaDataStore: MediaDataStore,
|
||||
private val fileTypeDeterminationService: FileTypeDeterminationService,
|
||||
private val mediaBlurhashService: MediaBlurhashService
|
||||
private val mediaBlurhashService: MediaBlurhashService,
|
||||
private val mediaRepository: MediaRepository,
|
||||
private val mediaProcessService: MediaProcessService
|
||||
) : MediaService {
|
||||
override suspend fun uploadLocalMedia(media: Media): SavedMedia {
|
||||
override suspend fun uploadLocalMedia(media: Media): EntityMedia {
|
||||
if (media.file.size == 0L) {
|
||||
return FaildSavedMedia(
|
||||
"File size is 0.",
|
||||
"Cannot upload a file with a file size of 0."
|
||||
)
|
||||
throw MediaFileSizeIsZeroException("Media file size is zero.")
|
||||
}
|
||||
|
||||
val fileType = fileTypeDeterminationService.fileType(media.file.bytes, media.file.name, media.file.contentType)
|
||||
if (fileType != FileTypeDeterminationService.FileType.Image) {
|
||||
return FaildSavedMedia("Unsupported file type.", "FileType: $fileType is not supported.")
|
||||
if (fileType != FileType.Image) {
|
||||
throw UnsupportedMediaException("FileType: $fileType is not supported.")
|
||||
}
|
||||
|
||||
try {
|
||||
mediaDataStore.save(
|
||||
MediaSave(
|
||||
media.file.name,
|
||||
"",
|
||||
media.file.inputStream,
|
||||
media.thumbnail.inputStream
|
||||
)
|
||||
val process = mediaProcessService.process(fileType, media.file.inputStream, media.thumbnail?.inputStream)
|
||||
|
||||
val dataMediaSave = MediaSave(
|
||||
UUID.randomUUID().toString(),
|
||||
"",
|
||||
process.first,
|
||||
process.second
|
||||
)
|
||||
val save = try {
|
||||
mediaDataStore.save(dataMediaSave)
|
||||
} catch (e: Exception) {
|
||||
logger.warn("Failed save media", e)
|
||||
throw MediaSaveException("Failed save media.", e)
|
||||
}
|
||||
|
||||
if (save.success.not()) {
|
||||
save as FaildSavedMedia
|
||||
logger.warn("Failed save media. reason: ${save.reason}")
|
||||
logger.warn(save.description, save.trace)
|
||||
throw MediaSaveException("Failed save media.")
|
||||
}
|
||||
save as SuccessSavedMedia
|
||||
|
||||
val blurHash = withContext(Dispatchers.IO) {
|
||||
mediaBlurhashService.generateBlurhash(ImageIO.read(media.file.bytes.inputStream()))
|
||||
}
|
||||
|
||||
return mediaRepository.save(
|
||||
EntityMedia(
|
||||
id = mediaRepository.generateId(),
|
||||
name = media.file.name,
|
||||
url = save.url,
|
||||
remoteUrl = null,
|
||||
thumbnailUrl = save.thumbnailUrl,
|
||||
type = fileType,
|
||||
blurHash = blurHash
|
||||
)
|
||||
} catch (e: MediaException) {
|
||||
return FaildSavedMedia(
|
||||
"Faild to upload.",
|
||||
e.localizedMessage,
|
||||
e
|
||||
)
|
||||
}
|
||||
|
||||
val withContext = withContext(Dispatchers.IO) {
|
||||
mediaBlurhashService.generateBlurhash(ImageIO.read(media.file.inputStream))
|
||||
}
|
||||
|
||||
return SuccessSavedMedia(
|
||||
media.file.name, "", "",
|
||||
withContext
|
||||
)
|
||||
}
|
||||
|
||||
override suspend fun uploadRemoteMedia(remoteMedia: RemoteMedia) {
|
||||
|
||||
}
|
||||
|
||||
companion object {
|
||||
private val logger = LoggerFactory.getLogger(MediaServiceImpl::class.java)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,7 @@
|
|||
package dev.usbharu.hideout.service.media
|
||||
|
||||
import java.io.InputStream
|
||||
|
||||
interface ThumbnailGenerateService {
|
||||
fun generate(bufferedImage: InputStream, width: Int, height: Int): InputStream
|
||||
}
|
|
@ -0,0 +1,9 @@
|
|||
package dev.usbharu.hideout.service.media.converter
|
||||
|
||||
import dev.usbharu.hideout.domain.model.hideout.dto.FileType
|
||||
import java.io.InputStream
|
||||
|
||||
interface MediaConverter {
|
||||
fun isSupport(fileType: FileType): Boolean
|
||||
fun convert(inputStream: InputStream): InputStream
|
||||
}
|
|
@ -0,0 +1,8 @@
|
|||
package dev.usbharu.hideout.service.media.converter
|
||||
|
||||
import dev.usbharu.hideout.domain.model.hideout.dto.FileType
|
||||
import java.io.InputStream
|
||||
|
||||
interface MediaConverterRoot {
|
||||
suspend fun convert(fileType: FileType, inputStream: InputStream): InputStream
|
||||
}
|
|
@ -0,0 +1,8 @@
|
|||
package dev.usbharu.hideout.service.media.converter
|
||||
|
||||
import dev.usbharu.hideout.domain.model.hideout.dto.FileType
|
||||
import java.io.InputStream
|
||||
|
||||
interface MediaProcessService {
|
||||
suspend fun process(fileType: FileType, file: InputStream, thumbnail: InputStream?): Pair<InputStream, InputStream>
|
||||
}
|
|
@ -0,0 +1,41 @@
|
|||
package dev.usbharu.hideout.service.media.converter
|
||||
|
||||
import dev.usbharu.hideout.domain.model.hideout.dto.FileType
|
||||
import dev.usbharu.hideout.exception.media.MediaConvertException
|
||||
import dev.usbharu.hideout.service.media.ThumbnailGenerateService
|
||||
import org.slf4j.LoggerFactory
|
||||
import java.io.InputStream
|
||||
|
||||
class MediaProcessServiceImpl(
|
||||
private val mediaConverterRoot: MediaConverterRoot,
|
||||
private val thumbnailGenerateService: ThumbnailGenerateService
|
||||
) : MediaProcessService {
|
||||
override suspend fun process(
|
||||
fileType: FileType,
|
||||
file: InputStream,
|
||||
thumbnail: InputStream?
|
||||
): Pair<InputStream, InputStream> {
|
||||
|
||||
val fileInputStream = try {
|
||||
mediaConverterRoot.convert(fileType, file)
|
||||
} catch (e: Exception) {
|
||||
logger.warn("Failed convert media.", e)
|
||||
throw MediaConvertException("Failed convert media.", e)
|
||||
}
|
||||
val thumbnailInputStream = try {
|
||||
thumbnail?.let { mediaConverterRoot.convert(fileType, it) }
|
||||
} catch (e: Exception) {
|
||||
logger.warn("Failed convert thumbnail media.", e)
|
||||
null
|
||||
}
|
||||
return fileInputStream to (thumbnailInputStream ?: thumbnailGenerateService.generate(
|
||||
fileInputStream,
|
||||
2048,
|
||||
2048
|
||||
))
|
||||
}
|
||||
|
||||
companion object {
|
||||
private val logger = LoggerFactory.getLogger(MediaProcessServiceImpl::class.java)
|
||||
}
|
||||
}
|
|
@ -7,6 +7,18 @@ hideout:
|
|||
key-id: a
|
||||
private-key: "MIIEvwIBADANBgkqhkiG9w0BAQEFAASCBKkwggSlAgEAAoIBAQC7VJTUt9Us8cKjMzEfYyjiWA4R4/M2bS1GB4t7NXp98C3SC6dVMvDuictGeurT8jNbvJZHtCSuYEvuNMoSfm76oqFvAp8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2HgxOBvbhOT+9in1BzA+YJ99UzC85O0Qz06A+CmtHEy4aZ2kj5hHjECgYEAmNS4+A8Fkss8Js1RieK2LniBxMgmYml3pfVLKGnzmng7H2+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: ""
|
||||
|
||||
|
||||
|
||||
|
||||
spring:
|
||||
jackson:
|
||||
serialization:
|
||||
|
|
Loading…
Reference in New Issue