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 bfdaae0 commit 82659b7
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 9 deletions.
4 changes: 3 additions & 1 deletion testtool/client/src/commonMain/kotlin/TesttoolClient.kt
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,13 @@ internal fun getData(path: String): Flow<Pair<String, ByteArray>> = flow {
while (true) {
val idLength = source.readInt()
val id = source.readString(idLength.toLong())
val contentLength = source.readInt()
val contentLength = source.readLong().toInt()
val content = source.readByteArray(contentLength)
emit(id to content)
}
} catch (cause: EOFException) {
// ignore
} finally {
source.close()
}
}
21 changes: 13 additions & 8 deletions testtool/server/src/main/kotlin/compatibility.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

package dev.whyoleg.cryptography.testtool.server

import io.ktor.http.*
import io.ktor.server.application.*
import io.ktor.server.response.*
import io.ktor.server.routing.*
Expand Down Expand Up @@ -62,8 +63,12 @@ private suspend fun ApplicationCall.saveFile(path: Path, name: String) {
path.createDirectories().resolve(name).writeBytes(bytes, StandardOpenOption.CREATE_NEW)
}

private suspend fun ApplicationCall.getFiles(path: Path, get: Path.() -> Pair<String, Path>) = respondBytesWriter {
if (!path.exists()) return@respondBytesWriter
private suspend fun ApplicationCall.getFiles(path: Path, get: Path.() -> Pair<String, Path>) {
if (!path.exists()) {
respond(HttpStatusCode.OK)
}

val output = Buffer()

path.forEachDirectoryEntry { entry ->
val (id, contentPath) = get(entry)
Expand All @@ -72,11 +77,11 @@ private suspend fun ApplicationCall.getFiles(path: Path, get: Path.() -> Pair<St
return@forEachDirectoryEntry
}
val idBytes = id.encodeToByteArray()
val contentBytes = contentPath.readBytes()
writeInt(idBytes.size)
writeByteArray(idBytes)
writeInt(contentBytes.size)
writeByteArray(contentBytes)
flush()
output.writeInt(idBytes.size)
output.write(idBytes)
output.writeLong(contentPath.fileSize())
output.write(contentPath.inputStream(), contentPath.fileSize())
}

respondSource(output)
}

0 comments on commit 82659b7

Please sign in to comment.