Skip to content

Commit

Permalink
feat: files api
Browse files Browse the repository at this point in the history
  • Loading branch information
y9vad9 committed Sep 27, 2023
1 parent 4d12f6c commit 0327d41
Show file tree
Hide file tree
Showing 9 changed files with 83 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import io.rsocket.kotlin.payload.Payload
import kotlinx.serialization.DeserializationStrategy
import kotlinx.serialization.ExperimentalSerializationApi
import kotlinx.serialization.SerializationStrategy
import kotlinx.serialization.decodeFromString
import kotlinx.serialization.json.Json
import kotlinx.serialization.json.decodeFromStream

Expand All @@ -17,6 +18,10 @@ internal fun <T> T.encodeToJson(serializationStrategy: SerializationStrategy<T>)
return ByteReadPacket(json.encodeToString(serializationStrategy, this).encodeToByteArray())
}

internal inline fun <reified T> ByteArray.decodeFromJson(): T {
return json.decodeFromString(String(this))
}

@OptIn(ExperimentalSerializationApi::class)
internal fun <T> Payload.decodeFromJson(deserializationStrategy: DeserializationStrategy<T>): T {
return json.decodeFromStream(deserializationStrategy, data.inputStream())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ package io.timemates.api.rsocket.files.commands

import io.timemates.api.rsocket.common.commands.RSocketCommandsBuilderScope
import io.timemates.sdk.files.requests.GetFileBytesRequest
import io.timemates.sdk.files.requests.UploadFileRequest

/**
* RSocket commands related to timers.
*/
internal fun RSocketCommandsBuilderScope.files() {
GetFileCommand associatedWith GetFileBytesRequest
UploadFileCommand associatedWith UploadFileRequest
}
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
package io.timemates.api.rsocket.files.commands

import io.rsocket.kotlin.RSocket
import io.rsocket.kotlin.payload.buildPayload
import io.timemates.api.rsocket.common.commands.RSocketCommand
import io.timemates.api.rsocket.common.serialization.encodeToJson
import io.timemates.api.rsocket.files.requests.GetFileRequest
import io.timemates.api.rsocket.common.ext.requestStream
import io.timemates.api.rsocket.common.serialization.decodeFromJson
import io.timemates.api.rsocket.files.requests.RSocketGetFileRequest
import io.timemates.api.rsocket.files.types.sdk
import io.timemates.sdk.files.requests.GetFileBytesRequest
import kotlinx.serialization.serializer
import kotlinx.coroutines.flow.first

internal object GetFileCommand : RSocketCommand<GetFileBytesRequest, GetFileBytesRequest.Result> {
override suspend fun execute(rSocket: RSocket, input: GetFileBytesRequest): GetFileBytesRequest.Result {
return rSocket.requestResponse(
buildPayload {
data(GetFileRequest(input.fileId.string).encodeToJson(serializer()))
}
return rSocket.requestStream(
route = "files.get",
data = RSocketGetFileRequest(input.fileId.string),
).let { result ->
// GetFileBytesRequest.Result(result.data)
TODO()
val metadata = result.first().decodeFromJson<RSocketGetFileRequest.Response.Metadata>()
GetFileBytesRequest.Result(metadata.fileType.sdk(), result)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package io.timemates.api.rsocket.files.commands

import io.ktor.utils.io.core.ByteReadPacket
import io.rsocket.kotlin.ExperimentalMetadataApi
import io.rsocket.kotlin.RSocket
import io.rsocket.kotlin.metadata.RoutingMetadata
import io.rsocket.kotlin.metadata.compositeMetadata
import io.rsocket.kotlin.payload.Payload
import io.rsocket.kotlin.payload.buildPayload
import io.timemates.api.rsocket.common.commands.RSocketCommand
import io.timemates.api.rsocket.common.serialization.decodeFromJson
import io.timemates.api.rsocket.common.serialization.encodeToJson
import io.timemates.api.rsocket.files.requests.RSocketUploadFileRequest
import io.timemates.api.rsocket.files.types.serializable
import io.timemates.sdk.common.constructor.createOrThrow
import io.timemates.sdk.files.requests.UploadFileRequest
import io.timemates.sdk.files.types.value.FileId
import kotlinx.coroutines.flow.last
import kotlinx.coroutines.flow.map
import kotlinx.serialization.serializer

internal object UploadFileCommand : RSocketCommand<UploadFileRequest, UploadFileRequest.Result> {
@OptIn(ExperimentalMetadataApi::class)
override suspend fun execute(rSocket: RSocket, input: UploadFileRequest): UploadFileRequest.Result {
return rSocket.requestChannel(
initPayload = buildPayload {
data(
RSocketUploadFileRequest.Metadata(
fileName = input.fileName.string,
fileType = input.fileType.serializable(),
).encodeToJson(serializer())
)
compositeMetadata {
add(RoutingMetadata("files.upload"))
}
},
payloads = input.bytes.map { Payload(data = ByteReadPacket(it)) }
).let { result ->
val serialized = result.last().decodeFromJson<RSocketUploadFileRequest.Response>(serializer())
UploadFileRequest.Result(fileId = FileId.createOrThrow(serialized.fileId))
}
}
}
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
package io.timemates.api.rsocket.files.requests

import io.timemates.api.rsocket.common.markers.RSocketRequest
import io.timemates.api.rsocket.files.types.SerializableFileType
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable

@Serializable
internal data class GetFileRequest(
internal data class RSocketGetFileRequest(
val fileId: String,
) {
) : RSocketRequest<ByteArray> {
@Serializable
sealed interface Response {
@SerialName("metadata")
data class Metadata(
val fileType: SerializableFileType,
) : Response

@SerialName("chunk")
@JvmInline
value class Chunk(val bytes: ByteArray) : Response
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import kotlinx.coroutines.flow.Flow
import kotlinx.serialization.Serializable

@Serializable
internal data class UploadFileRequest(
internal data class RSocketUploadFileRequest(
val fileType: SerializableFileType,
val bytes: Flow<ByteArray>,
) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
package io.timemates.api.rsocket.files.types

import io.timemates.sdk.files.types.FileType
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable

@Serializable
internal enum class SerializableFileType {
@SerialName("image")
IMAGE,
}

internal fun SerializableFileType.sdk(): FileType = when (this) {
SerializableFileType.IMAGE -> FileType.IMAGE
}

internal fun FileType.serializable(): SerializableFileType = when (this) {
FileType.IMAGE -> SerializableFileType.IMAGE
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package io.timemates.sdk.files.requests

import io.timemates.sdk.common.types.TimeMatesEntity
import io.timemates.sdk.common.types.TimeMatesRequest
import io.timemates.sdk.files.types.FileType
import io.timemates.sdk.files.types.value.FileId
import kotlinx.coroutines.flow.Flow

Expand All @@ -11,6 +12,7 @@ public data class GetFileBytesRequest(
public companion object Key : TimeMatesRequest.Key<GetFileBytesRequest>

public data class Result(
val fileType: FileType,
val bytes: Flow<ByteArray>
) : TimeMatesEntity()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package io.timemates.sdk.files.requests
import io.timemates.sdk.authorization.types.value.AccessHash
import io.timemates.sdk.common.types.AuthorizedTimeMatesRequest
import io.timemates.sdk.common.types.TimeMatesEntity
import io.timemates.sdk.common.types.TimeMatesRequest
import io.timemates.sdk.files.types.FileType
import io.timemates.sdk.files.types.value.FileId
import io.timemates.sdk.files.types.value.FileName
Expand All @@ -14,5 +15,9 @@ public data class UploadFileRequest(
val fileName: FileName,
val fileType: FileType,
) : AuthorizedTimeMatesRequest<UploadFileRequest.Result>() {
public companion object Key : TimeMatesRequest.Key<UploadFileRequest>

public data class Result(val fileId: FileId) : TimeMatesEntity()

override val requestKey: Key get() = Key
}

0 comments on commit 0327d41

Please sign in to comment.