From 513630b2a784f4e3e81debb942991dbef91e80e1 Mon Sep 17 00:00:00 2001
From: Corner <wall.corner@outlook.com>
Date: Tue, 16 Jul 2024 11:00:00 +0800
Subject: [PATCH] =?UTF-8?q?optmize:=20jar=E5=8A=A0=E8=BD=BD=E7=BC=93?=
 =?UTF-8?q?=E5=AD=98?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 .../com/corner/catvodcore/loader/JarLoader.kt | 17 +++++++++---
 .../com/corner/catvodcore/util/Utils.kt       | 27 ++++++++++++++++---
 .../src/desktopTest/kotlin/commonTest.kt      |  7 +++++
 3 files changed, 44 insertions(+), 7 deletions(-)

diff --git a/composeApp/src/commonMain/kotlin/com/corner/catvodcore/loader/JarLoader.kt b/composeApp/src/commonMain/kotlin/com/corner/catvodcore/loader/JarLoader.kt
index 2f53f25..b63fda5 100644
--- a/composeApp/src/commonMain/kotlin/com/corner/catvodcore/loader/JarLoader.kt
+++ b/composeApp/src/commonMain/kotlin/com/corner/catvodcore/loader/JarLoader.kt
@@ -30,21 +30,30 @@ object JarLoader {
 
     fun loadJar(key: String, spider: String) {
         val texts = spider.split(Constant.md5Split)
-//        val md5 = if(texts.size<=1) "" else texts[1].trim()
+        val md5 = if(texts.size<=1) "" else texts[1].trim()
         val jar = texts[0]
 
-        if(Paths.jar(jar).exists()){
+        // 可以避免重复下载
+        if(md5.isNotEmpty() && Utils.equals(parseJarUrl(jar), md5)){
             load(key, Paths.jar(jar))
         }else if (jar.startsWith("file")) {
             load(key, Paths.local(jar))
         } else if (jar.startsWith("http")) {
             load(key, download(jar))
         } else {
-             loadJar(key, Urls.convert(ApiConfig.api.url!!, spider))
+             loadJar(key, Urls.convert(ApiConfig.api.url!!, jar))
         }
 
     }
 
+    /**
+     * 如果在配置文件种使用的相对路径, 下载的时候使用的全路径 如果的判断md5是否一致的时候使用相对路径 就会造成重复下载
+     */
+    private fun parseJarUrl(jar: String): String {
+        if(jar.startsWith("file") || jar.startsWith("http")) return jar
+        return Urls.convert(ApiConfig.api.url!!, jar)
+    }
+
     private fun load(key: String, jar: File) {
         loaders[key] =  URLClassLoader(arrayOf(jar.toURI().toURL()),this.javaClass.classLoader)
         putProxy(key)
@@ -83,7 +92,7 @@ object JarLoader {
                 loader!!.loadClass(classPath).getDeclaredConstructor()
                     .newInstance() as Spider
             spider.init(ext)
-            spiders.put(spKey, spider)
+            spiders[spKey] = spider
             return spider
         } catch (e: Exception) {
             e.printStackTrace()
diff --git a/composeApp/src/commonMain/kotlin/com/corner/catvodcore/util/Utils.kt b/composeApp/src/commonMain/kotlin/com/corner/catvodcore/util/Utils.kt
index 611c56f..8f4d828 100644
--- a/composeApp/src/commonMain/kotlin/com/corner/catvodcore/util/Utils.kt
+++ b/composeApp/src/commonMain/kotlin/com/corner/catvodcore/util/Utils.kt
@@ -3,11 +3,10 @@ package com.corner.catvodcore.util
 import com.corner.catvodcore.config.ApiConfig
 import com.corner.database.Db
 import org.apache.commons.lang3.StringUtils
+import java.io.File
+import java.io.FileInputStream
 import java.math.BigInteger
 import java.security.MessageDigest
-import java.time.Instant
-import java.time.format.DateTimeFormatter
-import java.time.format.DateTimeFormatterBuilder
 import java.util.*
 
 object Utils {
@@ -26,6 +25,28 @@ object Utils {
 
     }
 
+    fun equals(name: String, md5: String): Boolean {
+        return md5(Paths.jar(name)).equals(md5, ignoreCase = true)
+    }
+
+    fun md5(file: File?): String {
+        try {
+            val digest = MessageDigest.getInstance("MD5")
+            val fis = FileInputStream(file)
+            val bytes = ByteArray(4096)
+            var count: Int
+            while ((fis.read(bytes).also { count = it }) != -1) digest.update(bytes, 0, count)
+            fis.close()
+            val sb = java.lang.StringBuilder()
+            for (b in digest.digest()) sb.append(((b.toInt() and 0xff) + 0x100).toString(16).substring(1))
+            return sb.toString()
+        } catch (e: java.lang.Exception) {
+            return ""
+        }
+    }
+
+//    private fun md5()
+
     fun base64(s: String): String {
         return base64(s.toByteArray())
     }
diff --git a/composeApp/src/desktopTest/kotlin/commonTest.kt b/composeApp/src/desktopTest/kotlin/commonTest.kt
index 5859ef5..1d90745 100644
--- a/composeApp/src/desktopTest/kotlin/commonTest.kt
+++ b/composeApp/src/desktopTest/kotlin/commonTest.kt
@@ -2,6 +2,7 @@ import com.corner.catvodcore.bean.Result
 import com.corner.catvodcore.util.Jsons
 import com.corner.catvodcore.util.KtorClient
 import com.corner.catvodcore.util.Urls
+import com.corner.catvodcore.util.Utils
 import com.corner.server.KtorD
 import io.ktor.client.request.*
 import io.ktor.client.statement.*
@@ -10,6 +11,7 @@ import kotlinx.coroutines.flow.asFlow
 import kotlinx.coroutines.runBlocking
 import kotlinx.serialization.ExperimentalSerializationApi
 import kotlinx.serialization.json.encodeToJsonElement
+import java.io.File
 import java.net.URI
 import java.net.URL
 import java.util.*
@@ -52,6 +54,11 @@ class commonTest {
 //        println(path)
     }
 
+    @Test
+    fun md5Test(){
+        println(Utils.md5(File("F:\\sync\\compose-mutiplatform-workspace\\My\\CatVodSpider\\jar\\spider.jar")))
+    }
+
     @Test
     fun splitTest() {
         val s = "$$$$$$"