feat: check if database files are on local filesystem on startup

This commit is contained in:
Gauthier Roebroeck 2025-07-28 13:34:27 +08:00
parent eb1e602b18
commit 93b21307d1
3 changed files with 72 additions and 0 deletions

View file

@ -60,3 +60,7 @@ class ComicRackListException(
class EntryNotFoundException(
message: String,
) : Exception(message)
class ConfigurationException(
message: String,
) : Exception(message)

View file

@ -0,0 +1,66 @@
package org.gotson.komga.infrastructure.configuration
import io.github.oshai.kotlinlogging.KotlinLogging
import jakarta.annotation.PostConstruct
import org.gotson.komga.domain.model.ConfigurationException
import org.springframework.stereotype.Component
import java.nio.file.Files
import java.nio.file.Path
import kotlin.io.path.Path
import kotlin.io.path.exists
private val logger = KotlinLogging.logger {}
@Component
class ConfigurationChecker(
private val komgaProperties: KomgaProperties,
) {
private val blockTypes = listOf("cifs", "nfs")
@PostConstruct
fun checkDatabasesPath() {
checkDatabaseIsLocal(komgaProperties.database, "komga.database.check-local-filesystem: false")
checkDatabaseIsLocal(komgaProperties.tasksDb, "komga.tasks-db.check-local-filesystem: false")
}
private fun checkDatabaseIsLocal(
database: KomgaProperties.Database,
ignoreProp: String,
) {
if (database.checkLocalFilesystem) {
val path =
try {
Path(database.file)
} catch (_: Exception) {
return
}
checkIfRemote(path, ignoreProp)
if (!path.exists()) checkIfRemote(path.parent, ignoreProp)
}
}
private fun checkIfRemote(
path: Path?,
ignoreProp: String,
) {
if (path == null) return
if (path.exists()) {
val storeType =
try {
Files.getFileStore(path).type().lowercase()
} catch (e: Exception) {
logger.warn(e) { "Could not get FileStore type for path: $path" }
"unknown"
}
if (blockTypes.any { storeType.startsWith(it, ignoreCase = true) }) {
val errorMessage = "The path '$path' should be on a local filesystem, but was detected to be on a remote filesystem ($storeType). If this is inaccurate you can set '$ignoreProp' to ignore this check."
logger.error { errorMessage }
throw ConfigurationException(errorMessage)
} else {
logger.debug { "FileStore type: $storeType, path: $path" }
}
}
}
}

View file

@ -73,6 +73,8 @@ class KomgaProperties {
var busyTimeout: Duration? = null
var pragmas: Map<String, String> = emptyMap()
var checkLocalFilesystem: Boolean = true
}
class Fonts {