SNRK-87: moved LocalDataHolder and changed drop to dropWhile

This commit is contained in:
Anton Belyi 2023-05-17 14:17:28 +03:00
parent e609de95cb
commit 45132e4355
2 changed files with 45 additions and 43 deletions

View File

@ -10,15 +10,9 @@ 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 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 kotlin.io.createTempFile import kotlin.io.createTempFile
import kotlin.io.path.*
import kotlin.io.writeBytes import kotlin.io.writeBytes
public interface DataHolder { public interface DataHolder {
@ -27,43 +21,6 @@ public interface DataHolder {
public fun represent(relativePath: String = "/"): String 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() + "<br>"
}
}
}
override fun represent(relativePath: String) : String =
if (source == null) {
"No data was loaded!"
} else {
response = "List of files:<br>"
val path = getPath(relativePath)
buildResponse(path, path)
response
}
}
public class SNARKServer(private val dataHolder: DataHolder, private val port: Int): Runnable { public class SNARKServer(private val dataHolder: DataHolder, private val port: Int): Runnable {
private var relativePath = "/" private var relativePath = "/"

View File

@ -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: 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() + "<br>"
}
}
}
override fun represent(relativePath: String) : 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()
} }