package eu.friedcutlet.yokan.services.files import eu.friedcutlet.yokan.database.DbPageUrl /** A filesystem that stores nothing: writes vanish and every path is absent. */ internal object NoFiles : LocalFileService { override val downloadRoot = "/downloads" override val backupRoot = "/backups" override val cacheRoot = "/cache" override fun writeBytes(path: String, bytes: ByteArray) = Unit override fun readBytes(path: String) = ByteArray(0) override fun canonicalPath(path: String) = path override fun exists(path: String) = false override fun deleteRecursively(path: String) = true override fun directorySize(path: String) = 0L override fun openLocation(path: String) = false override fun listLocalEntries(root: String) = emptyList() override fun listFiles(root: String) = emptyList() } /** * Map-backed filesystem with directory semantics derived from `/`-separated paths. * Methods are `open` so tests can inject failures by subclassing. */ internal open class MemoryFiles : LocalFileService { override val downloadRoot = "/downloads" override val backupRoot = "/backups" override val cacheRoot = "/cache" val files = linkedMapOf() val paths: Set get() = files.keys override fun writeBytes(path: String, bytes: ByteArray) { files[path] = bytes.copyOf() } override fun readBytes(path: String): ByteArray = files[path]?.copyOf() ?: ByteArray(0) override fun canonicalPath(path: String): String? = path override fun exists(path: String): Boolean = path in files || files.keys.any { it.startsWith("$path/") } override fun deleteRecursively(path: String): Boolean { files.keys.removeAll { it == path || it.startsWith("$path/") } return true } override fun directorySize(path: String): Long = files.filterKeys { it == path || it.startsWith("$path/") }.values.sumOf { it.size.toLong() } override fun openLocation(path: String) = false override fun listLocalEntries(root: String): List? = emptyList() override fun listFiles(root: String): List = files.filterKeys { it.substringBeforeLast('/') == root } .map { (path, bytes) -> LocalFileEntry(path, path.substringAfterLast('/'), bytes.size.toLong(), 0L) } } /** Archives that contain nothing and cannot be packaged. */ internal object NoArchives : LocalArchiveService { override fun listPages(archivePath: String) = emptyList() override fun listEpubChapters(archivePath: String) = emptyList() override fun readEpubChapter(archivePath: String, index: Int) = "" override fun readEpubResource(archivePath: String, resourcePath: String): ByteArray? = null override fun zipDirectoryToCbz(directoryPath: String, deleteSource: Boolean): String? = null }