implement fileystem asset manager
parent
949f87800a
commit
4ae5c3bf58
|
|
@ -1,3 +1,5 @@
|
|||
dependencies {
|
||||
api(project(":libraries:core-api"))
|
||||
// used for contentType
|
||||
api("io.ktor:ktor-http-jvm:2.3.7")
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,83 @@
|
|||
package de.itkl.assetmanager.implementation
|
||||
|
||||
import de.itkl.assetmanager.interfaces.AssetManager
|
||||
import de.itkl.assetmanager.interfaces.Assets
|
||||
import de.itkl.core_api.interfaces.Resource
|
||||
import de.itkl.core_api.interfaces.ResourceFactory
|
||||
import io.github.oshai.kotlinlogging.KotlinLogging
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.flow.FlowCollector
|
||||
import kotlinx.coroutines.flow.emitAll
|
||||
import kotlinx.coroutines.flow.map
|
||||
import kotlinx.coroutines.stream.consumeAsFlow
|
||||
import kotlinx.coroutines.withContext
|
||||
import org.koin.core.component.KoinComponent
|
||||
import org.koin.core.component.inject
|
||||
import java.nio.file.Files
|
||||
import java.nio.file.Path
|
||||
import java.nio.file.Paths
|
||||
import kotlin.io.path.deleteExisting
|
||||
import kotlin.io.path.exists
|
||||
import kotlin.io.path.outputStream
|
||||
|
||||
private val Log = KotlinLogging.logger { }
|
||||
class FilesystemAssetManager : AssetManager {
|
||||
override suspend fun assets(name: String): Assets {
|
||||
val path = createAssetsPath(name)
|
||||
withContext(Dispatchers.IO) {
|
||||
Files.createDirectories(path)
|
||||
}
|
||||
return FilesystemAssets(path)
|
||||
}
|
||||
|
||||
override suspend fun delete(name: String) {
|
||||
val path = createAssetsPath(name)
|
||||
withContext(Dispatchers.IO) {
|
||||
Files.delete(path)
|
||||
}
|
||||
}
|
||||
|
||||
private fun createAssetsPath(name: String): Path {
|
||||
return Paths.get("$name.assets.d").toAbsolutePath()
|
||||
}
|
||||
}
|
||||
|
||||
class FilesystemAssets(private val baseDir: Path) : Assets, KoinComponent {
|
||||
|
||||
private val resourceFactory by inject<ResourceFactory>()
|
||||
override suspend fun store(resource: Resource) {
|
||||
val destination = baseDir.resolve(resource.filename)
|
||||
resource.read().use { source ->
|
||||
destination.outputStream().use {output ->
|
||||
withContext(Dispatchers.IO) {
|
||||
source.copyTo(output)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override suspend fun retrieve(name: String): Resource? {
|
||||
val destination = baseDir.resolve(name)
|
||||
if (!destination.exists()) {
|
||||
return null
|
||||
}
|
||||
val resource = resourceFactory.file(destination)
|
||||
return resource
|
||||
}
|
||||
|
||||
|
||||
override suspend fun delete(name: String) {
|
||||
val destination = baseDir.resolve(name)
|
||||
withContext(Dispatchers.IO) {
|
||||
destination.deleteExisting()
|
||||
}
|
||||
}
|
||||
|
||||
override suspend fun collect(collector: FlowCollector<Resource>) {
|
||||
val flow = withContext(Dispatchers.IO) {
|
||||
Files.list(baseDir).consumeAsFlow()
|
||||
}
|
||||
.map { path -> resourceFactory.file(path) }
|
||||
collector.emitAll(flow)
|
||||
}
|
||||
}
|
||||
|
|
@ -1,4 +1,5 @@
|
|||
package de.itkl.assetmanager.interfaces
|
||||
interface AssetManager {
|
||||
fun assets(name: String): Assets
|
||||
suspend fun assets(name: String): Assets
|
||||
suspend fun delete(name: String)
|
||||
}
|
||||
|
|
@ -15,6 +15,7 @@ interface Resource {
|
|||
val file: File?
|
||||
val path: Path?
|
||||
fun read(): InputStream
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -5,10 +5,15 @@ import de.itkl.core_api.implementation.ProgressResource
|
|||
import org.koin.core.component.KoinComponent
|
||||
import org.koin.core.component.inject
|
||||
import java.io.File
|
||||
import java.nio.file.Path
|
||||
|
||||
class ResourceFactory : KoinComponent {
|
||||
|
||||
private val progressBarFactory by inject<ProgressBarFactory>()
|
||||
|
||||
fun file(path: Path): Resource {
|
||||
return file(path.toFile())
|
||||
}
|
||||
fun file(file: File): Resource {
|
||||
val resource = FileResource(file)
|
||||
return ProgressResource(resource, progressBarFactory)
|
||||
|
|
|
|||
Loading…
Reference in New Issue