Merge SNARK-MR-25: feature/SNRK-98/fix_snark_main

This commit is contained in:
Leonid Pereverzin 2023-05-18 11:19:26 +00:00 committed by Space Cloud
commit 98e713c909
No known key found for this signature in database
GPG Key ID: 2F4D45726235F749
3 changed files with 85 additions and 47 deletions

View File

@ -10,53 +10,30 @@ import io.ktor.server.request.*
import io.ktor.server.response.*
import kotlinx.html.*
import io.ktor.server.routing.*
import kotlinx.css.h1
import kotlinx.css.html
import kotlinx.html.dom.create
import kotlinx.html.dom.document
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 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() : Directory
public suspend fun represent(): 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 buildResponse(path: Path) {
for (entry in path.listDirectoryEntries()) {
if (entry.isDirectory()) {
buildResponse(entry)
} else {
response += source!!.relativize(entry).toString() + "\n"
}
}
}
override suspend fun represent(): String =
if (source == null) {
"No data was loaded!"
} else {
response = "List of files:\n"
buildResponse(source!!)
response
}
public suspend fun init(relativePath: Path) : Directory
public suspend fun represent(relativePath: Path): String
}
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 = Path("")
private suspend fun receivePath(call: ApplicationCall) {
val pathString = call.receiveParameters()["path"]?:""
relativePath = Path(pathString.dropWhile{it == '/'})
call.respondRedirect("/")
}
private suspend fun renderGet(call: ApplicationCall) {
call.respondText(dataHolder.represent(), ContentType.Text.Html)
call.respondText(dataHolder.represent(relativePath), ContentType.Text.Html)
}
private suspend fun renderUpload(call: ApplicationCall) {
val multipartData = call.receiveMultipart()
@ -72,8 +49,8 @@ public class SNARKServer(val dataHolder: DataHolder, val port: Int): Runnable {
}
part.dispose()
}
unzip(tmp.toPath().toString(), dataHolder.init())
call.respondText("File is successfully uploaded")
unzip(tmp.toPath().toString(), dataHolder.init(relativePath))
call.respondRedirect("/")
}
private suspend fun renderMainPage(call: ApplicationCall) {
call.respondHtml(HttpStatusCode.OK) {
@ -86,8 +63,20 @@ public class SNARKServer(val dataHolder: DataHolder, val port: Int): Runnable {
h1 {
+"SNARK"
}
p {
+("Path: /" + relativePath.toString())
}
}
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 +98,9 @@ public class SNARKServer(val dataHolder: DataHolder, val port: Int): Runnable {
get("/") {
renderMainPage(call)
}
post("/changePath") {
receivePath(call)
}
post("/upload") {
renderUpload(call)
}

View File

@ -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: Path) : Path {
return source!! / relativePath
}
override suspend fun init(relativePath: Path): 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() + "<br>"
}
}
}
override suspend fun represent(relativePath: Path) : String =
if (source == null) {
"No data was loaded!"
} else {
response = "List of files:<br>"
val path = getPath(relativePath)
buildResponse(path, path)
response
}
}
fun main() {
SNARKServer(LocalDataHolder(), 9090).run()
}

View File

@ -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))
}
}