From 87ee7c5a19f8ad2d5778083af49932dff06d3cf0 Mon Sep 17 00:00:00 2001 From: Yiqun Zhang Date: Fri, 20 Sep 2024 15:47:46 +0800 Subject: [PATCH 1/2] :bug: Use chrome-headless-shell for HTML rendering without window creation --- .../com/crosspaste/module/ModuleLoader.kt | 62 +++++++++++-------- .../crosspaste/html/DesktopChromeService.kt | 13 +--- 2 files changed, 37 insertions(+), 38 deletions(-) diff --git a/composeApp/src/commonMain/kotlin/com/crosspaste/module/ModuleLoader.kt b/composeApp/src/commonMain/kotlin/com/crosspaste/module/ModuleLoader.kt index fcc145b3..a448d8f4 100644 --- a/composeApp/src/commonMain/kotlin/com/crosspaste/module/ModuleLoader.kt +++ b/composeApp/src/commonMain/kotlin/com/crosspaste/module/ModuleLoader.kt @@ -54,42 +54,52 @@ interface ModuleLoader : Loader { override fun load(value: ModuleLoaderConfig): Boolean { val installPath = value.installPath - for (moduleItem in value.moduleItems) { - val urls = moduleItem.getUrls() - val installResult: Boolean? = - retryUtils.retry(value.retryNumber) { - val downTempPath = userDataPathProvider.resolve(moduleItem.downloadFileName, AppFileType.TEMP) - if (!fileUtils.existFile(downTempPath)) { - if (!downloadModule(urls[it], downTempPath)) { + + if (!installed(installPath)) { + for (moduleItem in value.moduleItems) { + val urls = moduleItem.getUrls() + val installResult: Boolean? = + retryUtils.retry(value.retryNumber) { + val downTempPath = + userDataPathProvider.resolve( + moduleItem.downloadFileName, + AppFileType.TEMP, + ) + if (!fileUtils.existFile(downTempPath)) { + if (!downloadModule(urls[it], downTempPath)) { + fileUtils.deleteFile(downTempPath) + return@retry null + } + } + + if (!verifyInstall(downTempPath, moduleItem.sha256)) { fileUtils.deleteFile(downTempPath) return@retry null } - } - if (!verifyInstall(downTempPath, moduleItem.sha256)) { - fileUtils.deleteFile(downTempPath) - return@retry null + if (installModule(downTempPath, installPath)) { + return@retry true + } else { + return@retry null + } } - if (installModule(downTempPath, installPath)) { - return@retry true - } else { - return@retry null - } + if (installResult != true) { + return false } - - if (installResult != true) { - return false } + makeInstalled(installPath) } - makeInstalled(installPath) - - for (moduleItem in value.moduleItems) { - val downTempPath = userDataPathProvider.resolve(moduleItem.downloadFileName, AppFileType.TEMP) - fileUtils.deleteFile(downTempPath) + if (installed(installPath)) { + for (moduleItem in value.moduleItems) { + val downTempPath = + userDataPathProvider.resolve(moduleItem.downloadFileName, AppFileType.TEMP) + fileUtils.deleteFile(downTempPath) + } + return true + } else { + return false } - - return true } } diff --git a/composeApp/src/desktopMain/kotlin/com/crosspaste/html/DesktopChromeService.kt b/composeApp/src/desktopMain/kotlin/com/crosspaste/html/DesktopChromeService.kt index 75d73a11..1e4b0964 100644 --- a/composeApp/src/desktopMain/kotlin/com/crosspaste/html/DesktopChromeService.kt +++ b/composeApp/src/desktopMain/kotlin/com/crosspaste/html/DesktopChromeService.kt @@ -99,20 +99,8 @@ class DesktopChromeService( val chromeServiceModule = ChromeServiceServiceModule(chromeDriverProperties) val optLoaderConfig = chromeServiceModule.getModuleLoaderConfig() - optLoaderConfig?.let { loaderConfig -> - if (chromeModuleLoader.installed(loaderConfig.installPath)) { - startByLoaderModule(loaderConfig) - startSuccess = true - return - } - } try { - chromeDriverService = ChromeDriverService.createDefaultService() - chromeDriver = ChromeDriver(chromeDriverService, options) - startSuccess = true - } catch (e: Exception) { - logger.error(e) { "chromeDriver auto init fail" } optLoaderConfig?.let { loaderConfig -> if (chromeModuleLoader.load(loaderConfig)) { startByLoaderModule(loaderConfig) @@ -120,6 +108,7 @@ class DesktopChromeService( return } } + } catch (e: Exception) { startSuccess = false return } From 41e32c24c077095b1dd6685191dc548931d8033a Mon Sep 17 00:00:00 2001 From: Yiqun Zhang Date: Fri, 20 Sep 2024 16:14:01 +0800 Subject: [PATCH 2/2] :bug: Use chrome-headless-shell for HTML rendering without window creation --- .../kotlin/com/crosspaste/module/ModuleLoader.kt | 3 ++- .../kotlin/com/crosspaste/html/ChromeModuleLoader.kt | 10 ++++++---- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/composeApp/src/commonMain/kotlin/com/crosspaste/module/ModuleLoader.kt b/composeApp/src/commonMain/kotlin/com/crosspaste/module/ModuleLoader.kt index a448d8f4..fd34d2be 100644 --- a/composeApp/src/commonMain/kotlin/com/crosspaste/module/ModuleLoader.kt +++ b/composeApp/src/commonMain/kotlin/com/crosspaste/module/ModuleLoader.kt @@ -32,6 +32,7 @@ interface ModuleLoader : Loader { * Install module from path to path */ fun installModule( + fileName: String, downloadPath: Path, installPath: Path, ): Boolean @@ -77,7 +78,7 @@ interface ModuleLoader : Loader { return@retry null } - if (installModule(downTempPath, installPath)) { + if (installModule(moduleItem.downloadFileName, downTempPath, installPath)) { return@retry true } else { return@retry null diff --git a/composeApp/src/desktopMain/kotlin/com/crosspaste/html/ChromeModuleLoader.kt b/composeApp/src/desktopMain/kotlin/com/crosspaste/html/ChromeModuleLoader.kt index 1f7396cd..c8dc88d4 100644 --- a/composeApp/src/desktopMain/kotlin/com/crosspaste/html/ChromeModuleLoader.kt +++ b/composeApp/src/desktopMain/kotlin/com/crosspaste/html/ChromeModuleLoader.kt @@ -15,21 +15,23 @@ class ChromeModuleLoader( override val logger: KLogger = KotlinLogging.logger {} override fun installModule( + fileName: String, downloadPath: Path, installPath: Path, ): Boolean { if (!downloadPath.toString().lowercase().endsWith(".zip")) { - logger.error { "Error: Downloaded file is not a zip archive" } + logger.error { "Error: Downloaded $fileName is not a zip archive" } return false } try { - // Decompress the downloaded file to installPath, this function needs to be idempotent and can be executed repeatedly + // Decompress the downloaded file to installPath, + // this function needs to be idempotent and can be executed repeatedly unzipFile(downloadPath, installPath) - logger.info { "Module installed successfully" } + logger.info { "$fileName installed successfully" } return true } catch (e: Exception) { - logger.error { "Error during module installation: ${e.message}" } + logger.error { "Error during $fileName installation: ${e.message}" } return false } }