From a93131b5dc3a74b1f0bbd2efd64b67eea39c3c86 Mon Sep 17 00:00:00 2001 From: usbharu <64310155+usbharu@users.noreply.github.com> Date: Sun, 16 Jun 2024 13:09:15 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20=E3=83=A1=E3=83=87=E3=82=A3=E3=82=A2?= =?UTF-8?q?=E5=87=A6=E7=90=86=E3=82=92=E8=BF=BD=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../hideout/core/application/media/Media.kt | 29 ++++++++++++- .../core/application/media/UploadMedia.kt | 3 +- .../media/UploadMediaApplicationService.kt | 41 ++++++++++++++++++- .../hideout/core/domain/model/media/Media.kt | 10 ++++- .../core/external/mediastore/MediaStore.kt | 8 ++++ 5 files changed, 85 insertions(+), 6 deletions(-) create mode 100644 hideout-core/src/main/kotlin/dev/usbharu/hideout/core/external/mediastore/MediaStore.kt diff --git a/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/application/media/Media.kt b/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/application/media/Media.kt index 04d1b2ee..99490b84 100644 --- a/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/application/media/Media.kt +++ b/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/application/media/Media.kt @@ -16,4 +16,31 @@ 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 + ) + } + } +} diff --git a/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/application/media/UploadMedia.kt b/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/application/media/UploadMedia.kt index 9f409416..4a8af8d3 100644 --- a/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/application/media/UploadMedia.kt +++ b/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/application/media/UploadMedia.kt @@ -16,6 +16,7 @@ package dev.usbharu.hideout.core.application.media +import java.net.URI 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?) diff --git a/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/application/media/UploadMediaApplicationService.kt b/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/application/media/UploadMediaApplicationService.kt index 36c07865..4e9150c7 100644 --- a/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/application/media/UploadMediaApplicationService.kt +++ b/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/application/media/UploadMediaApplicationService.kt @@ -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.CommandExecutor 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.springframework.stereotype.Service +import dev.usbharu.hideout.core.domain.model.media.Media as MediaModel -class UploadMediaApplicationService(transaction: Transaction) : AbstractApplicationService( +@Service +class UploadMediaApplicationService( + private val mediaProcessor: MediaProcessor, + private val mediaStore: MediaStore, + private val mediaRepository: MediaRepository, + private val idGenerateService: IdGenerateService, + transaction: Transaction +) : AbstractApplicationService( transaction, logger ) { companion object { @@ -29,6 +42,30 @@ class UploadMediaApplicationService(transaction: Transaction) : AbstractApplicat } 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) + } } \ No newline at end of file diff --git a/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/domain/model/media/Media.kt b/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/domain/model/media/Media.kt index 5c3eddf6..1d2823c4 100644 --- a/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/domain/model/media/Media.kt +++ b/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/domain/model/media/Media.kt @@ -18,10 +18,10 @@ package dev.usbharu.hideout.core.domain.model.media import java.net.URI -data class Media( +class Media( val id: MediaId, val name: MediaName, - val url: URI, + url: URI, val remoteUrl: URI?, val thumbnailUrl: URI?, val type: FileType, @@ -29,6 +29,12 @@ data class Media( val blurHash: MediaBlurHash?, val description: MediaDescription? = null, ) { + var url = url + private set + + fun setUrl(url: URI) { + this.url = url + } override fun toString(): String { return "Media(" + "id=$id, " + diff --git a/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/external/mediastore/MediaStore.kt b/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/external/mediastore/MediaStore.kt new file mode 100644 index 00000000..dc34540e --- /dev/null +++ b/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/external/mediastore/MediaStore.kt @@ -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 +} \ No newline at end of file