feat: メディア処理を追加

This commit is contained in:
usbharu 2024-06-16 13:09:15 +09:00
parent d903d558c1
commit a93131b5dc
5 changed files with 85 additions and 6 deletions

View File

@ -16,4 +16,31 @@
package dev.usbharu.hideout.core.application.media package dev.usbharu.hideout.core.application.media
class Media import dev.usbharu.hideout.core.domain.model.media.FileType
import dev.usbharu.hideout.core.domain.model.media.Media
import dev.usbharu.hideout.core.domain.model.media.MimeType
import java.net.URI
data class Media(
val name: String,
val url: URI,
val thumbprintURI: URI?,
val type: FileType,
val mimeType: MimeType,
val blurHash: String?,
val description: String?
) {
companion object {
fun of(media: Media): dev.usbharu.hideout.core.application.media.Media {
return Media(
media.name.name,
media.url,
media.thumbnailUrl,
media.type,
media.mimeType,
media.blurHash?.hash,
media.description?.description
)
}
}
}

View File

@ -16,6 +16,7 @@
package dev.usbharu.hideout.core.application.media package dev.usbharu.hideout.core.application.media
import java.net.URI
import java.nio.file.Path import java.nio.file.Path
data class UploadMedia(val path: Path) data class UploadMedia(val path: Path, val name: String, val remoteUri: URI?, val description: String?)

View File

@ -19,9 +19,22 @@ package dev.usbharu.hideout.core.application.media
import dev.usbharu.hideout.core.application.shared.AbstractApplicationService import dev.usbharu.hideout.core.application.shared.AbstractApplicationService
import dev.usbharu.hideout.core.application.shared.CommandExecutor import dev.usbharu.hideout.core.application.shared.CommandExecutor
import dev.usbharu.hideout.core.application.shared.Transaction import dev.usbharu.hideout.core.application.shared.Transaction
import dev.usbharu.hideout.core.domain.model.media.*
import dev.usbharu.hideout.core.domain.shared.id.IdGenerateService
import dev.usbharu.hideout.core.external.media.MediaProcessor
import dev.usbharu.hideout.core.external.mediastore.MediaStore
import org.slf4j.LoggerFactory import org.slf4j.LoggerFactory
import org.springframework.stereotype.Service
import dev.usbharu.hideout.core.domain.model.media.Media as MediaModel
class UploadMediaApplicationService(transaction: Transaction) : AbstractApplicationService<UploadMedia, Media>( @Service
class UploadMediaApplicationService(
private val mediaProcessor: MediaProcessor,
private val mediaStore: MediaStore,
private val mediaRepository: MediaRepository,
private val idGenerateService: IdGenerateService,
transaction: Transaction
) : AbstractApplicationService<UploadMedia, Media>(
transaction, logger transaction, logger
) { ) {
companion object { companion object {
@ -29,6 +42,30 @@ class UploadMediaApplicationService(transaction: Transaction) : AbstractApplicat
} }
override suspend fun internalExecute(command: UploadMedia, executor: CommandExecutor): Media { override suspend fun internalExecute(command: UploadMedia, executor: CommandExecutor): Media {
TODO() val process = mediaProcessor.process(command.path)
val id = idGenerateService.generateId()
val thumbnailUri = if (process.thumbnailPath != null) {
mediaStore.upload(process.thumbnailPath, "thumbnail-$id")
} else {
null
}
val uri = mediaStore.upload(process.path, id.toString())
val media = MediaModel(
MediaId(id),
MediaName(command.name),
uri,
command.remoteUri,
thumbnailUri,
process.fileType,
process.mimeType,
MediaBlurHash(process.blurHash),
command.description?.let { MediaDescription(it) }
)
mediaRepository.save(media)
return Media.of(media)
} }
} }

View File

@ -18,10 +18,10 @@ package dev.usbharu.hideout.core.domain.model.media
import java.net.URI import java.net.URI
data class Media( class Media(
val id: MediaId, val id: MediaId,
val name: MediaName, val name: MediaName,
val url: URI, url: URI,
val remoteUrl: URI?, val remoteUrl: URI?,
val thumbnailUrl: URI?, val thumbnailUrl: URI?,
val type: FileType, val type: FileType,
@ -29,6 +29,12 @@ data class Media(
val blurHash: MediaBlurHash?, val blurHash: MediaBlurHash?,
val description: MediaDescription? = null, val description: MediaDescription? = null,
) { ) {
var url = url
private set
fun setUrl(url: URI) {
this.url = url
}
override fun toString(): String { override fun toString(): String {
return "Media(" + return "Media(" +
"id=$id, " + "id=$id, " +

View File

@ -0,0 +1,8 @@
package dev.usbharu.hideout.core.external.mediastore
import java.net.URI
import java.nio.file.Path
interface MediaStore {
suspend fun upload(path: Path, id: String): URI
}