diff --git a/illust-front/build.gradle.kts b/illust-front/build.gradle.kts index 001d537..1597663 100644 --- a/illust-front/build.gradle.kts +++ b/illust-front/build.gradle.kts @@ -43,4 +43,30 @@ dependencies { testImplementation("io.ktor:ktor-server-tests-jvm") testImplementation("org.jetbrains.kotlin:kotlin-test-junit:1.9.22") 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") + } + } + } +} \ No newline at end of file diff --git a/illust-front/src/main/kotlin/dev/usbharu/unos/illust/front/CreateIllust.kt b/illust-front/src/main/kotlin/dev/usbharu/unos/illust/front/CreateIllust.kt new file mode 100644 index 0000000..d5527bd --- /dev/null +++ b/illust-front/src/main/kotlin/dev/usbharu/unos/illust/front/CreateIllust.kt @@ -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, + val characters: List, + val originals: List, + 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 + } +} diff --git a/illust-front/src/main/kotlin/dev/usbharu/unos/illust/front/S3Config.kt b/illust-front/src/main/kotlin/dev/usbharu/unos/illust/front/S3Config.kt new file mode 100644 index 0000000..bee6982 --- /dev/null +++ b/illust-front/src/main/kotlin/dev/usbharu/unos/illust/front/S3Config.kt @@ -0,0 +1,7 @@ +package dev.usbharu.unos.illust.front + +data class S3Config( + val bucket: String, + val region: String, + val endpoint: String +) diff --git a/illust-front/src/main/kotlin/dev/usbharu/unos/illust/front/controller/IllustController.kt b/illust-front/src/main/kotlin/dev/usbharu/unos/illust/front/controller/IllustController.kt index 292c8e7..7098e58 100644 --- a/illust-front/src/main/kotlin/dev/usbharu/unos/illust/front/controller/IllustController.kt +++ b/illust-front/src/main/kotlin/dev/usbharu/unos/illust/front/controller/IllustController.kt @@ -1,10 +1,25 @@ 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.createIllust -class IllustController(private val illustCrudStub:IllustCRUDServiceGrpcKt.IllustCRUDServiceCoroutineStub) { - suspend fun create(){ - illustCrudStub.create(createIllust { }) +class IllustController( + private val uploadIllustService: UploadIllustService, + 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 + }) } } \ No newline at end of file diff --git a/illust-front/src/main/kotlin/dev/usbharu/unos/illust/front/plugins/Routing.kt b/illust-front/src/main/kotlin/dev/usbharu/unos/illust/front/plugins/Routing.kt index f9c8bfe..324a2e3 100644 --- a/illust-front/src/main/kotlin/dev/usbharu/unos/illust/front/plugins/Routing.kt +++ b/illust-front/src/main/kotlin/dev/usbharu/unos/illust/front/plugins/Routing.kt @@ -1,13 +1,66 @@ 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.response.* +import io.ktor.server.request.* 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 { - get("/") { - call.respondText("Hello World!") + route("/api/v1") { + post("/illusts") { + val receiveMultipart = call.receiveMultipart() + + var name: String + var sha256: String + var s3Id: String + var tags: List + var originals: List + var characters: List + 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 -> { + + } + } + } + } + } + + + } } } } diff --git a/illust-front/src/main/kotlin/dev/usbharu/unos/illust/front/service/UploadIllustService.kt b/illust-front/src/main/kotlin/dev/usbharu/unos/illust/front/service/UploadIllustService.kt new file mode 100644 index 0000000..fdff52f --- /dev/null +++ b/illust-front/src/main/kotlin/dev/usbharu/unos/illust/front/service/UploadIllustService.kt @@ -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 + } +} \ No newline at end of file