Przeglądaj źródła

[skip ci] add UrlUtil.getSuffix

Xwite 3 lat temu
rodzic
commit
20be70b369

+ 1 - 7
app/src/main/java/io/legado/app/help/book/BookHelp.kt

@@ -178,13 +178,7 @@ object BookHelp {
     }
 
     fun getImageSuffix(src: String): String {
-        var suffix = src.substringAfterLast(".").substringBefore(",")
-        //检查截取的后缀字符是否合法 [a-zA-Z0-9]
-        val fileSuffixRegex = Regex("^[a-z0-9]+$", RegexOption.IGNORE_CASE)
-        if (suffix.length > 5 || !suffix.matches(fileSuffixRegex)) {
-            suffix = "jpg"
-        }
-        return suffix
+        return UrlUtil.getSuffix(src, "jpg")
     }
 
     @Throws(IOException::class, FileNotFoundException::class)

+ 1 - 8
app/src/main/java/io/legado/app/model/localBook/LocalBook.kt

@@ -266,16 +266,9 @@ object LocalBook {
 
     /**
      * 分析下载文件类书源的下载链接的文件后缀
-     * https://www.example.com/download/{fileName}.{type} 含有文件名和后缀
-     * https://www.example.com/download/?fileid=1234, {type: "txt"} 规则设置
      */
     fun parseFileSuffix(url: String): String {
-        val analyzeUrl = AnalyzeUrl(url)
-        val urlNoOption = analyzeUrl.url
-        val lastPath = urlNoOption.substringAfterLast("/")
-        val fileType = lastPath.substringAfterLast(".")
-        val type = analyzeUrl.type
-        return type ?: fileType
+        return UrlUtil.getSuffix(url, "ext")
     }
 
     fun saveBookFile(

+ 12 - 0
app/src/main/java/io/legado/app/utils/UrlUtil.kt

@@ -1,5 +1,7 @@
 package io.legado.app.utils
 
+import java.util.regex.Pattern
+
 object UrlUtil {
 
     fun replaceReservedChar(text: String): String {
@@ -24,5 +26,15 @@ object UrlUtil {
             .replace("|", "%7C")
     }
 
+    fun getSuffix(url: String, default: String): String {
+        val suffix = url.ubstringAfterLast(".").substringBeforeLast(",")
+        //检查截取的后缀字符是否合法 [a-zA-Z0-9]
+        val fileSuffixRegex = Regex("^[a-z0-9]+$", RegexOption.IGNORE_CASE)
+        return if (suffix.length > 5 || !suffix.matches(fileSuffixRegex)) {
+            default
+        } else {
+            suffix
+        }
+    }
 
 }