@@ -106,6 +106,10 @@ fun main() {
106106 Logger .setLogWriters(ConsoleLogWriter (), FileLogWriter (logDir))
107107 Logger .withTag(" main" ).i { " Application started. Logs directory: ${logDir.absolutePath} " }
108108
109+ // Cleanup old KCEF directories
110+ val baseDir = kcefBaseDir()
111+ cleanupOldKcefDirs(baseDir, BuildConfig .VERSION_NAME )
112+
109113 application {
110114 LaunchedEffect (Unit ) {
111115 WebViewBootstrap .start(
@@ -494,32 +498,49 @@ fun main() {
494498
495499
496500
497- private fun kcefInstallDir (): File {
501+ private fun kcefBaseDir (): File {
498502 val platform = currentPlatformDesktop()
499- val baseDir = when (platform) {
503+ return when (platform) {
500504 is Platform .Linux -> File (System .getProperty(" user.home" ), " .local/share/fly-narwhal" )
501505 is Platform .MacOS -> File (System .getProperty(" user.home" ), " Library/Application Support/fly-narwhal" )
502506 is Platform .Windows -> {
503507 val localAppData = System .getenv(" LOCALAPPDATA" )?.takeIf { it.isNotBlank() }
504508 File (localAppData ? : System .getProperty(" user.home" ), " FlyNarwhal" )
505509 }
506510 }
511+ }
512+
513+ private fun kcefInstallDir (): File {
507514 val version = BuildConfig .VERSION_NAME .replace(Regex (" [^A-Za-z0-9._-]" ), " _" )
508- return File (baseDir , " kcef-bundle-$version " )
515+ return File (kcefBaseDir() , " kcef-bundle-$version " )
509516}
510517
511518private fun kcefCacheDir (): File {
512- val platform = currentPlatformDesktop()
513- val baseDir = when (platform) {
514- is Platform .Linux -> File (System .getProperty(" user.home" ), " .local/share/fly-narwhal" )
515- is Platform .MacOS -> File (System .getProperty(" user.home" ), " Library/Application Support/fly-narwhal" )
516- is Platform .Windows -> {
517- val localAppData = System .getenv(" LOCALAPPDATA" )?.takeIf { it.isNotBlank() }
518- File (localAppData ? : System .getProperty(" user.home" ), " FlyNarwhal" )
519+ val version = BuildConfig .VERSION_NAME .replace(Regex (" [^A-Za-z0-9._-]" ), " _" )
520+ return File (kcefBaseDir(), " kcef-cache-$version " )
521+ }
522+
523+ /* *
524+ * Cleanup old KCEF bundle and cache directories to save disk space.
525+ * Only keeps the directories for the current version.
526+ */
527+ private fun cleanupOldKcefDirs (baseDir : File , currentVersion : String ) {
528+ val versionTag = currentVersion.replace(Regex (" [^A-Za-z0-9._-]" ), " _" )
529+ val currentBundle = " kcef-bundle-$versionTag "
530+ val currentCache = " kcef-cache-$versionTag "
531+
532+ baseDir.listFiles { file ->
533+ file.isDirectory && (file.name.startsWith(" kcef-bundle-" ) || file.name.startsWith(" kcef-cache-" ))
534+ }?.forEach { file ->
535+ if (file.name != currentBundle && file.name != currentCache) {
536+ try {
537+ file.deleteRecursively()
538+ Logger .withTag(" main" ).i { " Deleted old KCEF directory: ${file.name} " }
539+ } catch (e: Exception ) {
540+ Logger .withTag(" main" ).e(e) { " Failed to delete old KCEF directory: ${file.name} " }
541+ }
519542 }
520543 }
521- val version = BuildConfig .VERSION_NAME .replace(Regex (" [^A-Za-z0-9._-]" ), " _" )
522- return File (baseDir, " kcef-cache-$version " )
523544}
524545
525546/* *
0 commit comments