From f8bc08b84ffd56edd94fdaf1cd808c10d5894d4a Mon Sep 17 00:00:00 2001 From: Liyan Zhao Date: Tue, 3 Dec 2024 05:04:44 +0800 Subject: [PATCH] fix --- .../zly2006/xbackup/BackupDatabaseService.kt | 14 +++++++++++--- .../com/github/zly2006/xbackup/XBackup.kt | 19 ++++++++++++++++++- 2 files changed, 29 insertions(+), 4 deletions(-) diff --git a/common/src/main/kotlin/com/github/zly2006/xbackup/BackupDatabaseService.kt b/common/src/main/kotlin/com/github/zly2006/xbackup/BackupDatabaseService.kt index 57e22b2..fb95364 100644 --- a/common/src/main/kotlin/com/github/zly2006/xbackup/BackupDatabaseService.kt +++ b/common/src/main/kotlin/com/github/zly2006/xbackup/BackupDatabaseService.kt @@ -29,10 +29,18 @@ import kotlin.io.path.* class BackupDatabaseService( val database: Database, private val blobDir: Path, - private val config: Config + config: Config ) : CoroutineScope { - val log = LoggerFactory.getLogger("XBackup")!! - val syncExecutor = newFixedThreadPoolContext(1, "XBackup-Sync") + private val log = LoggerFactory.getLogger("XBackup")!! + private val syncExecutor = newFixedThreadPoolContext(1, "XBackup-Sync") + init { + require(blobDir.isAbsolute) { + "Blob directory must be absolute" + } + if (!blobDir.isDirectory()) { + log.warn("Blob directory not found, creating...") + } + } val oneDriveService: IOnedriveUtils by lazy { ServiceLoader.load(IOnedriveUtils::class.java) diff --git a/src/main/kotlin/com/github/zly2006/xbackup/XBackup.kt b/src/main/kotlin/com/github/zly2006/xbackup/XBackup.kt index 76e36fe..516ee2b 100644 --- a/src/main/kotlin/com/github/zly2006/xbackup/XBackup.kt +++ b/src/main/kotlin/com/github/zly2006/xbackup/XBackup.kt @@ -8,6 +8,7 @@ import kotlinx.serialization.ExperimentalSerializationApi import kotlinx.serialization.encodeToString import kotlinx.serialization.json.Json import kotlinx.serialization.json.buildJsonObject +import kotlinx.serialization.json.decodeFromStream import kotlinx.serialization.json.put import net.fabricmc.api.ModInitializer import net.fabricmc.fabric.api.command.v2.CommandRegistrationCallback @@ -146,7 +147,23 @@ object XBackup : ModInitializer { url = "jdbc:sqlite:$worldPath/x_backup.db" } ) - service = BackupDatabaseService(database, Path(".").absolute().resolve(config.blobPath).normalize(), config) + if (config.mirrorMode) { + val sourceConfig = kotlin.runCatching { + Json.decodeFromStream(Path(config.mirrorFrom!!, "config", "x-backup.config.json").inputStream()) + }.getOrNull() + if (sourceConfig == null) { + log.error("Failed to load config from source server!") + } + val config = sourceConfig ?: config + service = BackupDatabaseService( + database, + Path(config.mirrorFrom!!).resolve(config.blobPath).absolute().normalize(), + config + ) + } + else { + service = BackupDatabaseService(database, Path(".").absolute().resolve(config.blobPath).normalize(), config) + } if (!config.mirrorMode) { startCrontabJob(server) }