illust-front: apiを追加

This commit is contained in:
usbharu 2024-04-12 17:21:08 +09:00
parent 83bf04ce84
commit d5da51d11f
Signed by: usbharu
GPG Key ID: 6556747BF94EEBC8
6 changed files with 169 additions and 7 deletions

View File

@ -43,4 +43,30 @@ dependencies {
testImplementation("io.ktor:ktor-server-tests-jvm") testImplementation("io.ktor:ktor-server-tests-jvm")
testImplementation("org.jetbrains.kotlin:kotlin-test-junit:1.9.22") testImplementation("org.jetbrains.kotlin:kotlin-test-junit:1.9.22")
protobuf(files("../unos-proto/src/main/proto")) protobuf(files("../unos-proto/src/main/proto"))
implementation("software.amazon.awssdk:s3:2.25.16")
}
protobuf {
protoc {
artifact = "com.google.protobuf:protoc:3.25.3"
}
plugins {
create("grpc") {
artifact = "io.grpc:protoc-gen-grpc-java:1.61.1"
}
create("grpckt") {
artifact = "io.grpc:protoc-gen-grpc-kotlin:1.4.1:jdk8@jar"
}
}
generateProtoTasks {
all().forEach {
it.plugins {
create("grpc")
create("grpckt")
}
it.builtins {
create("kotlin")
}
}
}
} }

View File

@ -0,0 +1,39 @@
package dev.usbharu.unos.illust.front
data class CreateIllust(
val name: String,
val sha256: ByteArray,
val s3Id: String,
val tags: List<String>,
val characters: List<String>,
val originals: List<String>,
val description: String
) {
override fun equals(other: Any?): Boolean {
if (this === other) return true
if (javaClass != other?.javaClass) return false
other as CreateIllust
if (name != other.name) return false
if (!sha256.contentEquals(other.sha256)) return false
if (s3Id != other.s3Id) return false
if (tags != other.tags) return false
if (characters != other.characters) return false
if (originals != other.originals) return false
if (description != other.description) return false
return true
}
override fun hashCode(): Int {
var result = name.hashCode()
result = 31 * result + sha256.contentHashCode()
result = 31 * result + s3Id.hashCode()
result = 31 * result + tags.hashCode()
result = 31 * result + characters.hashCode()
result = 31 * result + originals.hashCode()
result = 31 * result + description.hashCode()
return result
}
}

View File

@ -0,0 +1,7 @@
package dev.usbharu.unos.illust.front
data class S3Config(
val bucket: String,
val region: String,
val endpoint: String
)

View File

@ -1,10 +1,25 @@
package dev.usbharu.unos.illust.front.controller package dev.usbharu.unos.illust.front.controller
import com.google.protobuf.kotlin.toByteString
import dev.usbharu.unos.illust.front.CreateIllust
import dev.usbharu.unos.illust.front.service.UploadIllustService
import illust.crud.IllustCRUDServiceGrpcKt import illust.crud.IllustCRUDServiceGrpcKt
import illust.crud.createIllust import illust.crud.createIllust
class IllustController(private val illustCrudStub:IllustCRUDServiceGrpcKt.IllustCRUDServiceCoroutineStub) { class IllustController(
suspend fun create(){ private val uploadIllustService: UploadIllustService,
illustCrudStub.create(createIllust { }) private val illustCrudStub: IllustCRUDServiceGrpcKt.IllustCRUDServiceCoroutineStub
) {
suspend fun create(createIllust: CreateIllust, illust: ByteArray) {
uploadIllustService.upload(illust)
illustCrudStub.create(createIllust {
name = createIllust.name
sha256 = createIllust.sha256.toByteString()
s3Id = createIllust.s3Id
tags.addAll(createIllust.tags)
characters.addAll(createIllust.characters)
originals.addAll(createIllust.originals)
description = createIllust.description
})
} }
} }

View File

@ -1,13 +1,66 @@
package dev.usbharu.unos.illust.front.plugins package dev.usbharu.unos.illust.front.plugins
import dev.usbharu.unos.illust.front.controller.IllustController
import io.ktor.http.content.*
import io.ktor.server.application.* import io.ktor.server.application.*
import io.ktor.server.response.* import io.ktor.server.request.*
import io.ktor.server.routing.* import io.ktor.server.routing.*
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.asFlow
import kotlinx.coroutines.flow.flowOn
import kotlinx.coroutines.flow.onEach
fun Application.configureRouting() { fun Application.configureRouting(controller: IllustController) {
routing { routing {
get("/") { route("/api/v1") {
call.respondText("Hello World!") post("/illusts") {
val receiveMultipart = call.receiveMultipart()
var name: String
var sha256: String
var s3Id: String
var tags: List<String>
var originals: List<String>
var characters: List<String>
var description: String
var file: ByteArray
receiveMultipart.forEachPart {
when (it) {
is PartData.BinaryChannelItem -> {
}
is PartData.BinaryItem -> {
}
is PartData.FileItem -> {
if (it.name == "file") {
file = it.streamProvider.invoke().readAllBytes()
}
}
is PartData.FormItem -> {
when (it.name) {
"name" -> name = it.value
"sha256" -> sha256 = it.value
"s3Id" -> s3Id = it.value
"tags" -> tags = it.value.split(";")
"originals" -> originals = it.value.split(";")
"characters" -> characters = it.value.split(";")
"description" -> description = it.value
else -> {
}
}
}
}
}
}
} }
} }
} }

View File

@ -0,0 +1,22 @@
package dev.usbharu.unos.illust.front.service
import dev.usbharu.unos.illust.front.S3Config
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import software.amazon.awssdk.core.sync.RequestBody
import software.amazon.awssdk.services.s3.S3Client
import software.amazon.awssdk.services.s3.model.PutObjectRequest
import java.util.UUID
class UploadIllustService(private val s3: S3Client, private val s3Config: S3Config) {
suspend fun upload(byteArray: ByteArray): UUID {
val randomUUID = UUID.randomUUID()
val putObjectRequest = PutObjectRequest.builder().bucket(s3Config.bucket).key(randomUUID.toString()).build()
withContext(Dispatchers.IO) {
s3.putObject(putObjectRequest, RequestBody.fromBytes(byteArray))
}
return randomUUID
}
}