From aeb5868353ea62c4a6195a51b105ebaeeaecb911 Mon Sep 17 00:00:00 2001 From: Syer10 Date: Mon, 3 May 2021 15:59:57 -0400 Subject: [PATCH] Check Tachidesk jar manifest and update if needed --- build.gradle.kts | 2 + .../ca/gosyer/data/server/ServerService.kt | 47 +++++++++++++++++-- 2 files changed, 45 insertions(+), 4 deletions(-) diff --git a/build.gradle.kts b/build.gradle.kts index 7747424b..f61b5ab1 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -143,6 +143,8 @@ buildConfig { packageName = project.group.toString() buildConfigField("boolean", "DEBUG", project.hasProperty("debugApp").toString()) + buildConfigField("String", "TACHIDESK_SP_VERSION", "0.2.7") + buildConfigField("String", "TACHIDESK_IM_VERSION", "0") } kotlinter { diff --git a/src/main/kotlin/ca/gosyer/data/server/ServerService.kt b/src/main/kotlin/ca/gosyer/data/server/ServerService.kt index 63b7e27c..f633a4f7 100644 --- a/src/main/kotlin/ca/gosyer/data/server/ServerService.kt +++ b/src/main/kotlin/ca/gosyer/data/server/ServerService.kt @@ -6,15 +6,20 @@ package ca.gosyer.data.server +import ca.gosyer.BuildConfig import ca.gosyer.util.system.userDataDir +import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.GlobalScope import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.launchIn import kotlinx.coroutines.flow.onEach import kotlinx.coroutines.launch +import kotlinx.coroutines.withContext import mu.KotlinLogging import java.io.BufferedReader import java.io.File +import java.io.IOException +import java.util.jar.JarInputStream import javax.inject.Inject import kotlin.concurrent.thread @@ -31,6 +36,14 @@ class ServerService @Inject constructor( ) var process: Process? = null + private fun copyJar(jarFile: File) { + javaClass.getResourceAsStream("/Tachidesk.jar")?.buffered()?.use { input -> + jarFile.outputStream().use { output -> + input.copyTo(output) + } + } + } + init { host.onEach { process?.destroy() @@ -41,16 +54,40 @@ class ServerService @Inject constructor( return@onEach } GlobalScope.launch { - val logger = KotlinLogging.logger("Server") val runtime = Runtime.getRuntime() val jarFile = File(userDataDir, "Tachidesk.jar") if (!jarFile.exists()) { logger.info { "Copying server to resources" } - javaClass.getResourceAsStream("/Tachidesk.jar")?.buffered()?.use { input -> - jarFile.outputStream().use { output -> - input.copyTo(output) + copyJar(jarFile) + } else { + try { + val jarVersion = withContext(Dispatchers.IO) { + JarInputStream(jarFile.inputStream()).use { jar -> + JarVersion( + jar.manifest?.mainAttributes?.getValue("Specification-Version"), + jar.manifest?.mainAttributes?.getValue("Implementation-Version") + ) + } + } + // TODO remove 1.0 version check when we move onto 0.3.0 of Tachidesk + if ( + jarVersion.specification != null && + jarVersion.implementation != null && + jarVersion.implementation != "1.0" + ) { + if ( + jarVersion.specification != BuildConfig.TACHIDESK_SP_VERSION || + jarVersion.implementation != BuildConfig.TACHIDESK_IM_VERSION + ) { + logger.info { "Updating server file from resources" } + copyJar(jarFile) + } + } + } catch (e: IOException) { + logger.error(e) { + "Error accessing server jar, cannot update server, ${BuildConfig.NAME} may not work properly" } } } @@ -101,4 +138,6 @@ class ServerService @Inject constructor( STARTED, FAILED; } + + data class JarVersion(val specification: String?, val implementation: String?) }