Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
whyoleg committed Oct 11, 2024
1 parent 82659b7 commit 24c22e3
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 14 deletions.
14 changes: 4 additions & 10 deletions testtool/client/src/commonMain/kotlin/TesttoolClient.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import io.ktor.client.*
import io.ktor.client.call.*
import io.ktor.client.plugins.*
import io.ktor.client.request.*
import io.ktor.client.statement.*
import io.ktor.http.content.*
import kotlinx.coroutines.flow.*
import kotlinx.io.*
Expand Down Expand Up @@ -44,21 +43,16 @@ private val client = HttpClient {

internal suspend fun postData(path: String, bytes: ByteArray): String = client.post(path) {
setBody(ByteArrayContent(bytes))
}.bodyAsText()
}.body<Source>().use { it.readString() }

internal fun getData(path: String): Flow<Pair<String, ByteArray>> = flow {
val source = client.get(path).body<Source>()
try {
client.get(path).body<Source>().use { source ->
while (true) {
val idLength = source.readInt()
val idLength = runCatching { source.readInt() }.getOrNull() ?: break
val id = source.readString(idLength.toLong())
val contentLength = source.readLong().toInt()
val contentLength = source.readInt()
val content = source.readByteArray(contentLength)
emit(id to content)
}
} catch (cause: EOFException) {
// ignore
} finally {
source.close()
}
}
11 changes: 7 additions & 4 deletions testtool/server/src/main/kotlin/compatibility.kt
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,11 @@ internal fun Route.routes(
}

private suspend fun ApplicationCall.saveFile(path: Path, name: String) {
val bytes = request.receiveChannel().readRemaining().readByteArray()
val buffer = request.receiveChannel().readBuffer()

path.createDirectories().resolve(name).writeBytes(bytes, StandardOpenOption.CREATE_NEW)
path.createDirectories().resolve(name)
.outputStream(StandardOpenOption.CREATE_NEW)
.use(buffer::readTo)
}

private suspend fun ApplicationCall.getFiles(path: Path, get: Path.() -> Pair<String, Path>) {
Expand All @@ -79,8 +81,9 @@ private suspend fun ApplicationCall.getFiles(path: Path, get: Path.() -> Pair<St
val idBytes = id.encodeToByteArray()
output.writeInt(idBytes.size)
output.write(idBytes)
output.writeLong(contentPath.fileSize())
output.write(contentPath.inputStream(), contentPath.fileSize())
output.writeInt(contentPath.fileSize().toInt())
contentPath.inputStream()
.use(output::transferFrom)
}

respondSource(output)
Expand Down

0 comments on commit 24c22e3

Please sign in to comment.