[skip ci] add UrlUtil.getSuffix

This commit is contained in:
Xwite 2023-03-13 10:45:23 +08:00
parent 5cc9f044f6
commit aeafc57c03
3 changed files with 14 additions and 15 deletions

View File

@ -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)

View File

@ -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(

View File

@ -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
}
}
}