From 45132e4355297449678da0413b157d35efd675fb Mon Sep 17 00:00:00 2001 From: Anton Belyi Date: Wed, 17 May 2023 14:17:28 +0300 Subject: [PATCH] SNRK-87: moved LocalDataHolder and changed drop to dropWhile --- .../space/kscience/snark/ktor/WebInterface.kt | 43 ------------------ .../space/kscience/snark/ktor/testServer.kt | 45 +++++++++++++++++++ 2 files changed, 45 insertions(+), 43 deletions(-) diff --git a/snark-ktor/src/main/kotlin/space/kscience/snark/ktor/WebInterface.kt b/snark-ktor/src/main/kotlin/space/kscience/snark/ktor/WebInterface.kt index 3e181f0..ccdf32a 100644 --- a/snark-ktor/src/main/kotlin/space/kscience/snark/ktor/WebInterface.kt +++ b/snark-ktor/src/main/kotlin/space/kscience/snark/ktor/WebInterface.kt @@ -10,15 +10,9 @@ import io.ktor.server.request.* import io.ktor.server.response.* import kotlinx.html.* import io.ktor.server.routing.* -import java.nio.file.Path import space.kscience.snark.storage.Directory -import space.kscience.snark.storage.local.localStorage -import kotlin.io.path.createTempDirectory -import kotlin.io.path.isDirectory -import kotlin.io.path.listDirectoryEntries import space.kscience.snark.storage.unzip.unzip import kotlin.io.createTempFile -import kotlin.io.path.* import kotlin.io.writeBytes public interface DataHolder { @@ -27,43 +21,6 @@ public interface DataHolder { public fun represent(relativePath: String = "/"): String } -internal class LocalDataHolder: DataHolder { - private var source: Path? = null - private var response: String = "" - - private fun getPath(relativePath: String) : Path { - return source!! / Path(relativePath.drop(1)) - } - override fun init(relativePath: String): Directory { - if (source == null) { - source = createTempDirectory() - } - val path = getPath(relativePath) - path.createDirectories() - path.toFile().deleteRecursively() - path.createDirectory() - return localStorage(path) - } - private fun buildResponse(from: Path, cur: Path) { - for (entry in cur.listDirectoryEntries()) { - if (entry.isDirectory()) { - buildResponse(from, entry) - } else { - response += from.relativize(entry).toString() + "
" - } - } - } - override fun represent(relativePath: String) : String = - if (source == null) { - "No data was loaded!" - } else { - response = "List of files:
" - val path = getPath(relativePath) - buildResponse(path, path) - response - } -} - public class SNARKServer(private val dataHolder: DataHolder, private val port: Int): Runnable { private var relativePath = "/" diff --git a/snark-ktor/src/test/kotlin/space/kscience/snark/ktor/testServer.kt b/snark-ktor/src/test/kotlin/space/kscience/snark/ktor/testServer.kt index 6c4ee11..6a16f68 100644 --- a/snark-ktor/src/test/kotlin/space/kscience/snark/ktor/testServer.kt +++ b/snark-ktor/src/test/kotlin/space/kscience/snark/ktor/testServer.kt @@ -1,5 +1,50 @@ package space.kscience.snark.ktor +import space.kscience.snark.storage.Directory +import space.kscience.snark.storage.local.localStorage +import java.nio.file.Path +import kotlin.io.path.* +import kotlin.io.path.createTempDirectory +import kotlin.io.path.isDirectory +import kotlin.io.path.listDirectoryEntries + +private class LocalDataHolder: DataHolder { + private var source: Path? = null + private var response: String = "" + + private fun getPath(relativePath: String) : Path { + return source!! / Path(relativePath.dropWhile{it == '/'}) + } + override fun init(relativePath: String): Directory { + if (source == null) { + source = createTempDirectory() + } + val path = getPath(relativePath) + path.createDirectories() + path.toFile().deleteRecursively() + path.createDirectory() + return localStorage(path) + } + private fun buildResponse(from: Path, cur: Path) { + for (entry in cur.listDirectoryEntries()) { + if (entry.isDirectory()) { + buildResponse(from, entry) + } else { + response += from.relativize(entry).toString() + "
" + } + } + } + override fun represent(relativePath: String) : String = + if (source == null) { + "No data was loaded!" + } else { + response = "List of files:
" + val path = getPath(relativePath) + buildResponse(path, path) + response + } +} + fun main() { SNARKServer(LocalDataHolder(), 9090).run() } \ No newline at end of file