From 82cd925e358895c5ffd932db7c605feb5e320d64 Mon Sep 17 00:00:00 2001 From: Anton Belyi Date: Mon, 15 May 2023 16:47:35 +0300 Subject: [PATCH 1/6] SNRK-87: simple text-insertion of path --- .../space/kscience/snark/ktor/WebInterface.kt | 68 ++++++++++++++----- 1 file changed, 50 insertions(+), 18 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 368bad6..61d7d78 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 @@ -15,48 +15,65 @@ import kotlinx.css.html 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 kotlin.io.path.name import space.kscience.snark.storage.unzip.unzip +import java.io.File +import kotlin.io.createTempFile +import kotlin.io.path.* +import kotlin.io.writeBytes public interface DataHolder { - fun init() : Directory - fun represent(): String + fun init(relativePath: String = "/") : Directory + fun represent(relativePath: String = "/"): String //will be HTML later } + class LocalDataHolder: DataHolder { private var source: Path? = null private var response: String = "" - override fun init(): Directory { - source?.toFile()?.deleteRecursively() - source = createTempDirectory() - return localStorage(source!!) + + private fun getPath(relativePath: String) : Path { + return source!! / Path(relativePath.drop(1)) } - private fun buildResponse(path: Path) { - for (entry in path.listDirectoryEntries()) { + 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(entry) + buildResponse(from, entry) } else { - response += source!!.relativize(entry).toString() + "\n" + response += from.relativize(entry).toString() + "\n" } } } - override fun represent() : String = + override fun represent(relativePath: String) : String = if (source == null) { "No data was loaded!" } else { response = "List of files:\n" - buildResponse(source!!) + val path = getPath(relativePath) + buildResponse(path, path) response } } public class SNARKServer(val dataHolder: DataHolder, val port: Int): Runnable { + private var relativePath = "/" + + private suspend fun receivePath(call: ApplicationCall) { + relativePath = call.receiveText().drop(5).replace("%2F", "/") + call.respondText("Path is successfully changed to: " + relativePath) + } private suspend fun renderGet(call: ApplicationCall) { - call.respondText(dataHolder.represent()) + call.respondText(dataHolder.represent(relativePath)) } private suspend fun renderUpload(call: ApplicationCall) { val multipartData = call.receiveMultipart() @@ -72,7 +89,7 @@ public class SNARKServer(val dataHolder: DataHolder, val port: Int): Runnable { } part.dispose() } - unzip(tmp.toPath().toString(), dataHolder.init()) + unzip(tmp.toPath().toString(), dataHolder.init(relativePath)) call.respondText("File is successfully uploaded") } private suspend fun renderMainPage(call: ApplicationCall) { @@ -86,8 +103,20 @@ public class SNARKServer(val dataHolder: DataHolder, val port: Int): Runnable { h1 { +"SNARK" } + p { + +("Path: " + relativePath) + } } body { + postForm(action = "/changePath") { + label { + + "Enter new path:" + } + input(name = "path", type = InputType.text) {} + button { + +"Change path" + } + } postForm (action = "/upload", encType = FormEncType.multipartFormData) { label { +"Choose zip archive: " @@ -109,6 +138,9 @@ public class SNARKServer(val dataHolder: DataHolder, val port: Int): Runnable { get("/") { renderMainPage(call) } + post("/changePath") { + receivePath(call) + } post("/upload") { renderUpload(call) } From 32f8a0d54003ff895b1450ab0ccde09c386b1496 Mon Sep 17 00:00:00 2001 From: Anton Belyi Date: Mon, 15 May 2023 17:10:33 +0300 Subject: [PATCH 2/6] removed replace %2F --- .../src/main/kotlin/space/kscience/snark/ktor/WebInterface.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 61d7d78..ea597d2 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 @@ -69,7 +69,7 @@ public class SNARKServer(val dataHolder: DataHolder, val port: Int): Runnable { private var relativePath = "/" private suspend fun receivePath(call: ApplicationCall) { - relativePath = call.receiveText().drop(5).replace("%2F", "/") + relativePath = call.receiveParameters()["path"]?:"/" call.respondText("Path is successfully changed to: " + relativePath) } private suspend fun renderGet(call: ApplicationCall) { From 62e8d859d8517f9ec30de22f6fa8720e71fa64ea Mon Sep 17 00:00:00 2001 From: Anton Belyi Date: Mon, 15 May 2023 18:51:46 +0300 Subject: [PATCH 3/6] SNRK-87: added redirect and resolved some conflicts --- .../space/kscience/snark/ktor/WebInterface.kt | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 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 ea597d2..98791e1 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 @@ -8,15 +8,12 @@ import io.ktor.server.application.* import io.ktor.server.html.* import io.ktor.server.request.* import io.ktor.server.response.* -import io.ktor.server.http.content.* import kotlinx.html.* import io.ktor.server.routing.* -import kotlinx.css.html import java.nio.file.Path import space.kscience.snark.storage.Directory import space.kscience.snark.storage.local.localStorage import space.kscience.snark.storage.unzip.unzip -import java.io.File import kotlin.io.createTempFile import kotlin.io.path.* import kotlin.io.writeBytes @@ -50,7 +47,7 @@ class LocalDataHolder: DataHolder { if (entry.isDirectory()) { buildResponse(from, entry) } else { - response += from.relativize(entry).toString() + "\n" + response += from.relativize(entry).toString() + "
" } } } @@ -58,7 +55,7 @@ class LocalDataHolder: DataHolder { if (source == null) { "No data was loaded!" } else { - response = "List of files:\n" + response = "List of files:
" val path = getPath(relativePath) buildResponse(path, path) response @@ -70,10 +67,11 @@ public class SNARKServer(val dataHolder: DataHolder, val port: Int): Runnable { private suspend fun receivePath(call: ApplicationCall) { relativePath = call.receiveParameters()["path"]?:"/" - call.respondText("Path is successfully changed to: " + relativePath) + call.respondRedirect("/") + //call.respondText("Path is successfully changed to: " + relativePath) } private suspend fun renderGet(call: ApplicationCall) { - call.respondText(dataHolder.represent(relativePath)) + call.respondText(dataHolder.represent(relativePath), ContentType.Text.Html) } private suspend fun renderUpload(call: ApplicationCall) { val multipartData = call.receiveMultipart() @@ -90,7 +88,8 @@ public class SNARKServer(val dataHolder: DataHolder, val port: Int): Runnable { part.dispose() } unzip(tmp.toPath().toString(), dataHolder.init(relativePath)) - call.respondText("File is successfully uploaded") + //call.respondText("File is successfully uploaded") + call.respondRedirect("/") } private suspend fun renderMainPage(call: ApplicationCall) { call.respondHtml(HttpStatusCode.OK) { From f28c5c6263e99a4f6e370969bbdde76c44bf074f Mon Sep 17 00:00:00 2001 From: Anton Belyi Date: Wed, 17 May 2023 14:06:34 +0300 Subject: [PATCH 4/6] SNRK-98: minor fixes --- .../kotlin/space/kscience/snark/ktor/WebInterface.kt | 12 +++++------- 1 file changed, 5 insertions(+), 7 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 98791e1..3753f60 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 @@ -20,12 +20,12 @@ import kotlin.io.writeBytes public interface DataHolder { - fun init(relativePath: String = "/") : Directory - fun represent(relativePath: String = "/"): String - //will be HTML later + public fun init(relativePath: String = "/") : Directory + + public fun represent(relativePath: String = "/"): String } -class LocalDataHolder: DataHolder { +internal class LocalDataHolder: DataHolder { private var source: Path? = null private var response: String = "" @@ -62,13 +62,12 @@ class LocalDataHolder: DataHolder { } } -public class SNARKServer(val dataHolder: DataHolder, val port: Int): Runnable { +public class SNARKServer(private val dataHolder: DataHolder, private val port: Int): Runnable { private var relativePath = "/" private suspend fun receivePath(call: ApplicationCall) { relativePath = call.receiveParameters()["path"]?:"/" call.respondRedirect("/") - //call.respondText("Path is successfully changed to: " + relativePath) } private suspend fun renderGet(call: ApplicationCall) { call.respondText(dataHolder.represent(relativePath), ContentType.Text.Html) @@ -88,7 +87,6 @@ public class SNARKServer(val dataHolder: DataHolder, val port: Int): Runnable { part.dispose() } unzip(tmp.toPath().toString(), dataHolder.init(relativePath)) - //call.respondText("File is successfully uploaded") call.respondRedirect("/") } private suspend fun renderMainPage(call: ApplicationCall) { From 45132e4355297449678da0413b157d35efd675fb Mon Sep 17 00:00:00 2001 From: Anton Belyi Date: Wed, 17 May 2023 14:17:28 +0300 Subject: [PATCH 5/6] 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 From 4ee51ebc0c7088c0f9956ec3b820b09be70a5353 Mon Sep 17 00:00:00 2001 From: Anton Belyi Date: Wed, 17 May 2023 14:58:50 +0300 Subject: [PATCH 6/6] SNRK-98: fixed snark-main --- .../space/kscience/snark/ktor/WebInterface.kt | 14 +++++++++----- .../kotlin/space/kscience/snark/ktor/testServer.kt | 8 ++++---- .../space/kscience/snark/main/ServerDataHolder.kt | 11 ++++++----- 3 files changed, 19 insertions(+), 14 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 ccdf32a..5bae320 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 @@ -12,20 +12,24 @@ import kotlinx.html.* import io.ktor.server.routing.* import space.kscience.snark.storage.Directory import space.kscience.snark.storage.unzip.unzip +import java.io.File +import java.nio.file.Path import kotlin.io.createTempFile import kotlin.io.writeBytes +import kotlin.io.path.Path public interface DataHolder { - public fun init(relativePath: String = "/") : Directory + public suspend fun init(relativePath: Path) : Directory - public fun represent(relativePath: String = "/"): String + public suspend fun represent(relativePath: Path): String } public class SNARKServer(private val dataHolder: DataHolder, private val port: Int): Runnable { - private var relativePath = "/" + private var relativePath = Path("") private suspend fun receivePath(call: ApplicationCall) { - relativePath = call.receiveParameters()["path"]?:"/" + val pathString = call.receiveParameters()["path"]?:"" + relativePath = Path(pathString.dropWhile{it == '/'}) call.respondRedirect("/") } private suspend fun renderGet(call: ApplicationCall) { @@ -60,7 +64,7 @@ public class SNARKServer(private val dataHolder: DataHolder, private val port: I +"SNARK" } p { - +("Path: " + relativePath) + +("Path: /" + relativePath.toString()) } } body { 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 6a16f68..9cc5e74 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 @@ -12,10 +12,10 @@ 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 == '/'}) + private fun getPath(relativePath: Path) : Path { + return source!! / relativePath } - override fun init(relativePath: String): Directory { + override suspend fun init(relativePath: Path): Directory { if (source == null) { source = createTempDirectory() } @@ -34,7 +34,7 @@ private class LocalDataHolder: DataHolder { } } } - override fun represent(relativePath: String) : String = + override suspend fun represent(relativePath: Path) : String = if (source == null) { "No data was loaded!" } else { diff --git a/snark-main/src/main/kotlin/space/kscience/snark/main/ServerDataHolder.kt b/snark-main/src/main/kotlin/space/kscience/snark/main/ServerDataHolder.kt index ce1a9e1..1b7b4a7 100644 --- a/snark-main/src/main/kotlin/space/kscience/snark/main/ServerDataHolder.kt +++ b/snark-main/src/main/kotlin/space/kscience/snark/main/ServerDataHolder.kt @@ -3,13 +3,14 @@ package space.kscience.snark.main import space.kscience.snark.ktor.DataHolder import space.kscience.snark.storage.Directory import documentBuilder.* -import kotlinx.html.HTML +import java.nio.file.Path internal class ServerDataHolder(private val directory: Directory): DataHolder { - override fun init(): Directory = - directory - override suspend fun represent(): String { - return buildDocument(directory).toString() + override suspend fun init(relativePath: Path): Directory = directory.getSubdir(relativePath) + + + override suspend fun represent(relativePath: Path): String { + return buildDocument(init(relativePath)) } } \ No newline at end of file