diff --git a/.gitignore b/.gitignore index f078ee27..39e4144d 100644 --- a/.gitignore +++ b/.gitignore @@ -47,3 +47,4 @@ out/ *.log /hideout-core/files/ /hideout-core/.kotlin/sessions/ +/hideout-mastodon/.kotlin/sessions/ 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 99490b84..5ff3e4a8 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 @@ -22,9 +22,11 @@ import dev.usbharu.hideout.core.domain.model.media.MimeType import java.net.URI data class Media( + val id: Long, val name: String, val url: URI, val thumbprintURI: URI?, + val remoteURL: URI?, val type: FileType, val mimeType: MimeType, val blurHash: String?, @@ -33,13 +35,15 @@ data class Media( 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 + id = media.id.id, + name = media.name.name, + url = media.url, + thumbprintURI = media.thumbnailUrl, + remoteURL = media.remoteUrl, + type = media.type, + mimeType = media.mimeType, + blurHash = media.blurHash?.hash, + description = media.description?.description ) } } 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 f9d857e8..1ba8034a 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 @@ -46,11 +46,11 @@ class UploadMediaApplicationService( val process = mediaProcessor.process(command.path, command.name, null) val id = idGenerateService.generateId() val thumbnailUri = if (process.thumbnailPath != null) { - mediaStore.upload(process.thumbnailPath, "thumbnail-$id") + mediaStore.upload(process.thumbnailPath, "thumbnail-$id.${process.mimeType.subtype}") } else { null } - val uri = mediaStore.upload(process.path, id.toString()) + val uri = mediaStore.upload(process.path, "$id.${process.mimeType.subtype}") val media = MediaModel( MediaId(id), diff --git a/hideout-mastodon/src/main/kotlin/dev/usbharu/hideout/mastodon/interfaces/api/SpringMediaApi.kt b/hideout-mastodon/src/main/kotlin/dev/usbharu/hideout/mastodon/interfaces/api/SpringMediaApi.kt index 087bcb1c..a3a84a79 100644 --- a/hideout-mastodon/src/main/kotlin/dev/usbharu/hideout/mastodon/interfaces/api/SpringMediaApi.kt +++ b/hideout-mastodon/src/main/kotlin/dev/usbharu/hideout/mastodon/interfaces/api/SpringMediaApi.kt @@ -16,20 +16,62 @@ package dev.usbharu.hideout.mastodon.interfaces.api +import dev.usbharu.hideout.core.application.media.UploadMedia +import dev.usbharu.hideout.core.application.media.UploadMediaApplicationService +import dev.usbharu.hideout.core.domain.model.media.FileType.* +import dev.usbharu.hideout.core.infrastructure.springframework.oauth2.Oauth2CommandExecutorFactory import dev.usbharu.hideout.mastodon.interfaces.api.generated.MediaApi import dev.usbharu.hideout.mastodon.interfaces.api.generated.model.MediaAttachment import org.springframework.http.ResponseEntity import org.springframework.stereotype.Controller import org.springframework.web.multipart.MultipartFile +import java.nio.file.Files @Controller -class SpringMediaApi : MediaApi { +class SpringMediaApi( + private val uploadMediaApplicationService: UploadMediaApplicationService, + private val oauth2CommandExecutorFactory: Oauth2CommandExecutorFactory +) : MediaApi { override suspend fun apiV1MediaPost( file: MultipartFile, thumbnail: MultipartFile?, description: String?, focus: String?, ): ResponseEntity { - return super.apiV1MediaPost(file, thumbnail, description, focus) + val tempFile = Files.createTempFile("hideout-tmp-file", ".tmp") + + Files.newOutputStream(tempFile).use { outputStream -> + file.inputStream.use { + it.transferTo(outputStream) + } + } + + val media = uploadMediaApplicationService.execute( + UploadMedia( + tempFile, + file.originalFilename ?: file.name, + null, + description + ), + oauth2CommandExecutorFactory.getCommandExecutor() + ) + + return ResponseEntity.ok( + MediaAttachment( + media.id.toString(), + when (media.type) { + Image -> MediaAttachment.Type.image + Video -> MediaAttachment.Type.video + Audio -> MediaAttachment.Type.audio + Unknown -> MediaAttachment.Type.unknown + }, + media.url.toString(), + media.thumbprintURI?.toString(), + media.remoteURL?.toString(), + media.description, + media.blurHash, + media.url.toASCIIString() + ) + ) } } \ No newline at end of file