4
4
5
5
package dev.whyoleg.cryptography.testtool.server
6
6
7
+ import io.ktor.http.*
7
8
import io.ktor.server.application.*
8
9
import io.ktor.server.response.*
9
10
import io.ktor.server.routing.*
@@ -57,13 +58,19 @@ internal fun Route.routes(
57
58
}
58
59
59
60
private suspend fun ApplicationCall.saveFile (path : Path , name : String ) {
60
- val bytes = request.receiveChannel().readRemaining().readByteArray ()
61
+ val buffer = request.receiveChannel().readBuffer ()
61
62
62
- path.createDirectories().resolve(name).writeBytes(bytes, StandardOpenOption .CREATE_NEW )
63
+ path.createDirectories().resolve(name)
64
+ .outputStream(StandardOpenOption .CREATE_NEW )
65
+ .use(buffer::readTo)
63
66
}
64
67
65
- private suspend fun ApplicationCall.getFiles (path : Path , get : Path .() -> Pair <String , Path >) = respondBytesWriter {
66
- if (! path.exists()) return @respondBytesWriter
68
+ private suspend fun ApplicationCall.getFiles (path : Path , get : Path .() -> Pair <String , Path >) {
69
+ if (! path.exists()) {
70
+ respond(HttpStatusCode .OK )
71
+ }
72
+
73
+ val output = Buffer ()
67
74
68
75
path.forEachDirectoryEntry { entry ->
69
76
val (id, contentPath) = get(entry)
@@ -72,10 +79,11 @@ private suspend fun ApplicationCall.getFiles(path: Path, get: Path.() -> Pair<St
72
79
return @forEachDirectoryEntry
73
80
}
74
81
val idBytes = id.encodeToByteArray()
75
- val contentBytes = contentPath.readBytes()
76
- writeInt(idBytes.size)
77
- writeFully(idBytes)
78
- writeInt(contentBytes.size)
79
- writeFully(contentBytes)
82
+ output.writeInt(idBytes.size)
83
+ output.write(idBytes)
84
+ output.writeInt(contentPath.fileSize().toInt())
85
+ contentPath.inputStream().use(output::transferFrom)
80
86
}
87
+
88
+ respondSource(output)
81
89
}
0 commit comments