[skip ci] add UrlUtil.getSuffix
This commit is contained in:
@@ -178,13 +178,7 @@ object BookHelp {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fun getImageSuffix(src: String): String {
|
fun getImageSuffix(src: String): String {
|
||||||
var suffix = src.substringAfterLast(".").substringBefore(",")
|
return UrlUtil.getSuffix(src, "jpg")
|
||||||
//检查截取的后缀字符是否合法 [a-zA-Z0-9]
|
|
||||||
val fileSuffixRegex = Regex("^[a-z0-9]+$", RegexOption.IGNORE_CASE)
|
|
||||||
if (suffix.length > 5 || !suffix.matches(fileSuffixRegex)) {
|
|
||||||
suffix = "jpg"
|
|
||||||
}
|
|
||||||
return suffix
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Throws(IOException::class, FileNotFoundException::class)
|
@Throws(IOException::class, FileNotFoundException::class)
|
||||||
|
|||||||
@@ -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 {
|
fun parseFileSuffix(url: String): String {
|
||||||
val analyzeUrl = AnalyzeUrl(url)
|
return UrlUtil.getSuffix(url, "ext")
|
||||||
val urlNoOption = analyzeUrl.url
|
|
||||||
val lastPath = urlNoOption.substringAfterLast("/")
|
|
||||||
val fileType = lastPath.substringAfterLast(".")
|
|
||||||
val type = analyzeUrl.type
|
|
||||||
return type ?: fileType
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fun saveBookFile(
|
fun saveBookFile(
|
||||||
|
|||||||
@@ -1,5 +1,7 @@
|
|||||||
package io.legado.app.utils
|
package io.legado.app.utils
|
||||||
|
|
||||||
|
import java.util.regex.Pattern
|
||||||
|
|
||||||
object UrlUtil {
|
object UrlUtil {
|
||||||
|
|
||||||
fun replaceReservedChar(text: String): String {
|
fun replaceReservedChar(text: String): String {
|
||||||
@@ -24,5 +26,15 @@ object UrlUtil {
|
|||||||
.replace("|", "%7C")
|
.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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user