feat: メディアアップロードAPIを追加

This commit is contained in:
usbharu 2024-06-16 16:03:39 +09:00
parent 43ef619eee
commit 48a4874fc4
4 changed files with 58 additions and 11 deletions

1
.gitignore vendored
View File

@ -47,3 +47,4 @@ out/
*.log *.log
/hideout-core/files/ /hideout-core/files/
/hideout-core/.kotlin/sessions/ /hideout-core/.kotlin/sessions/
/hideout-mastodon/.kotlin/sessions/

View File

@ -22,9 +22,11 @@ import dev.usbharu.hideout.core.domain.model.media.MimeType
import java.net.URI import java.net.URI
data class Media( data class Media(
val id: Long,
val name: String, val name: String,
val url: URI, val url: URI,
val thumbprintURI: URI?, val thumbprintURI: URI?,
val remoteURL: URI?,
val type: FileType, val type: FileType,
val mimeType: MimeType, val mimeType: MimeType,
val blurHash: String?, val blurHash: String?,
@ -33,13 +35,15 @@ data class Media(
companion object { companion object {
fun of(media: Media): dev.usbharu.hideout.core.application.media.Media { fun of(media: Media): dev.usbharu.hideout.core.application.media.Media {
return Media( return Media(
media.name.name, id = media.id.id,
media.url, name = media.name.name,
media.thumbnailUrl, url = media.url,
media.type, thumbprintURI = media.thumbnailUrl,
media.mimeType, remoteURL = media.remoteUrl,
media.blurHash?.hash, type = media.type,
media.description?.description mimeType = media.mimeType,
blurHash = media.blurHash?.hash,
description = media.description?.description
) )
} }
} }

View File

@ -46,11 +46,11 @@ class UploadMediaApplicationService(
val process = mediaProcessor.process(command.path, command.name, null) val process = mediaProcessor.process(command.path, command.name, null)
val id = idGenerateService.generateId() val id = idGenerateService.generateId()
val thumbnailUri = if (process.thumbnailPath != null) { val thumbnailUri = if (process.thumbnailPath != null) {
mediaStore.upload(process.thumbnailPath, "thumbnail-$id") mediaStore.upload(process.thumbnailPath, "thumbnail-$id.${process.mimeType.subtype}")
} else { } else {
null null
} }
val uri = mediaStore.upload(process.path, id.toString()) val uri = mediaStore.upload(process.path, "$id.${process.mimeType.subtype}")
val media = MediaModel( val media = MediaModel(
MediaId(id), MediaId(id),

View File

@ -16,20 +16,62 @@
package dev.usbharu.hideout.mastodon.interfaces.api 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.MediaApi
import dev.usbharu.hideout.mastodon.interfaces.api.generated.model.MediaAttachment import dev.usbharu.hideout.mastodon.interfaces.api.generated.model.MediaAttachment
import org.springframework.http.ResponseEntity import org.springframework.http.ResponseEntity
import org.springframework.stereotype.Controller import org.springframework.stereotype.Controller
import org.springframework.web.multipart.MultipartFile import org.springframework.web.multipart.MultipartFile
import java.nio.file.Files
@Controller @Controller
class SpringMediaApi : MediaApi { class SpringMediaApi(
private val uploadMediaApplicationService: UploadMediaApplicationService,
private val oauth2CommandExecutorFactory: Oauth2CommandExecutorFactory
) : MediaApi {
override suspend fun apiV1MediaPost( override suspend fun apiV1MediaPost(
file: MultipartFile, file: MultipartFile,
thumbnail: MultipartFile?, thumbnail: MultipartFile?,
description: String?, description: String?,
focus: String?, focus: String?,
): ResponseEntity<MediaAttachment> { ): ResponseEntity<MediaAttachment> {
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()
)
)
} }
} }