Merge SNARK-MR-25: feature/SNRK-98/fix_snark_main
This commit is contained in:
commit
98e713c909
@ -10,53 +10,30 @@ import io.ktor.server.request.*
|
|||||||
import io.ktor.server.response.*
|
import io.ktor.server.response.*
|
||||||
import kotlinx.html.*
|
import kotlinx.html.*
|
||||||
import io.ktor.server.routing.*
|
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.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 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 interface DataHolder {
|
||||||
public fun init() : Directory
|
public suspend fun init(relativePath: Path) : Directory
|
||||||
public suspend fun represent(): String
|
|
||||||
//will be HTML later
|
public suspend fun represent(relativePath: Path): String
|
||||||
}
|
|
||||||
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 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) {
|
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) {
|
private suspend fun renderUpload(call: ApplicationCall) {
|
||||||
val multipartData = call.receiveMultipart()
|
val multipartData = call.receiveMultipart()
|
||||||
@ -72,8 +49,8 @@ public class SNARKServer(val dataHolder: DataHolder, val port: Int): Runnable {
|
|||||||
}
|
}
|
||||||
part.dispose()
|
part.dispose()
|
||||||
}
|
}
|
||||||
unzip(tmp.toPath().toString(), dataHolder.init())
|
unzip(tmp.toPath().toString(), dataHolder.init(relativePath))
|
||||||
call.respondText("File is successfully uploaded")
|
call.respondRedirect("/")
|
||||||
}
|
}
|
||||||
private suspend fun renderMainPage(call: ApplicationCall) {
|
private suspend fun renderMainPage(call: ApplicationCall) {
|
||||||
call.respondHtml(HttpStatusCode.OK) {
|
call.respondHtml(HttpStatusCode.OK) {
|
||||||
@ -86,8 +63,20 @@ public class SNARKServer(val dataHolder: DataHolder, val port: Int): Runnable {
|
|||||||
h1 {
|
h1 {
|
||||||
+"SNARK"
|
+"SNARK"
|
||||||
}
|
}
|
||||||
|
p {
|
||||||
|
+("Path: /" + relativePath.toString())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
body {
|
body {
|
||||||
|
postForm(action = "/changePath") {
|
||||||
|
label {
|
||||||
|
+ "Enter new path:"
|
||||||
|
}
|
||||||
|
input(name = "path", type = InputType.text) {}
|
||||||
|
button {
|
||||||
|
+"Change path"
|
||||||
|
}
|
||||||
|
}
|
||||||
postForm (action = "/upload", encType = FormEncType.multipartFormData) {
|
postForm (action = "/upload", encType = FormEncType.multipartFormData) {
|
||||||
label {
|
label {
|
||||||
+"Choose zip archive: "
|
+"Choose zip archive: "
|
||||||
@ -109,6 +98,9 @@ public class SNARKServer(val dataHolder: DataHolder, val port: Int): Runnable {
|
|||||||
get("/") {
|
get("/") {
|
||||||
renderMainPage(call)
|
renderMainPage(call)
|
||||||
}
|
}
|
||||||
|
post("/changePath") {
|
||||||
|
receivePath(call)
|
||||||
|
}
|
||||||
post("/upload") {
|
post("/upload") {
|
||||||
renderUpload(call)
|
renderUpload(call)
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,50 @@
|
|||||||
package space.kscience.snark.ktor
|
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() {
|
fun main() {
|
||||||
SNARKServer(LocalDataHolder(), 9090).run()
|
SNARKServer(LocalDataHolder(), 9090).run()
|
||||||
}
|
}
|
@ -3,13 +3,14 @@ package space.kscience.snark.main
|
|||||||
import space.kscience.snark.ktor.DataHolder
|
import space.kscience.snark.ktor.DataHolder
|
||||||
import space.kscience.snark.storage.Directory
|
import space.kscience.snark.storage.Directory
|
||||||
import documentBuilder.*
|
import documentBuilder.*
|
||||||
import kotlinx.html.HTML
|
import java.nio.file.Path
|
||||||
|
|
||||||
internal class ServerDataHolder(private val directory: Directory): DataHolder {
|
internal class ServerDataHolder(private val directory: Directory): DataHolder {
|
||||||
override fun init(): Directory =
|
|
||||||
directory
|
|
||||||
|
|
||||||
override suspend fun represent(): String {
|
override suspend fun init(relativePath: Path): Directory = directory.getSubdir(relativePath)
|
||||||
return buildDocument(directory).toString()
|
|
||||||
|
|
||||||
|
override suspend fun represent(relativePath: Path): String {
|
||||||
|
return buildDocument(init(relativePath))
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user