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
/hideout-core/files/
/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
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
)
}
}

View File

@ -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),

View File

@ -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<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()
)
)
}
}