generated from y9vad9/kotlin-project-template
-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
83 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 10 additions & 10 deletions
20
...et-engine/src/commonMain/kotlin/io/timemates/api/rsocket/files/commands/GetFileCommand.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
...engine/src/commonMain/kotlin/io/timemates/api/rsocket/files/commands/UploadFileCommand.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
} | ||
} | ||
} |
8 changes: 6 additions & 2 deletions
8
.../rsocket/files/requests/GetFileRequest.kt → ...t/files/requests/RSocketGetFileRequest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
...engine/src/commonMain/kotlin/io/timemates/api/rsocket/files/types/SerializableFileType.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters