This commit is contained in:
kunfei
2023-03-14 23:41:30 +08:00
parent e127964ec7
commit 2ed33c7474
2 changed files with 13 additions and 14 deletions
@@ -241,7 +241,7 @@ class BookInfoViewModel(application: Application) : BaseViewModel(application) {
webFiles.clear() webFiles.clear()
val fileName = "${book.name} 作者:${book.author}" val fileName = "${book.name} 作者:${book.author}"
book.downloadUrls!!.map { book.downloadUrls!!.map {
val mFileName = "${fileName}.${LocalBook.parseFileSuffix(it)}" val mFileName = UrlUtil.getFileName(it) ?: fileName
val isSupportedFile = AppPattern.bookFileRegex.matches(mFileName) val isSupportedFile = AppPattern.bookFileRegex.matches(mFileName)
val isSupportDecompress = AppPattern.archiveFileRegex.matches(mFileName) val isSupportDecompress = AppPattern.archiveFileRegex.matches(mFileName)
WebFile(it, mFileName, isSupportedFile, isSupportDecompress) WebFile(it, mFileName, isSupportedFile, isSupportDecompress)
@@ -29,9 +29,12 @@ object UrlUtil {
.replace("|", "%7C") .replace("|", "%7C")
} }
fun getFileName(fileUrl: String): String { /**
var fileName = "" * 根据url获取文件名
try { */
fun getFileName(fileUrl: String): String? {
return kotlin.runCatching {
var fileName = ""
val url = URL(fileUrl) val url = URL(fileUrl)
val conn: HttpURLConnection = url.openConnection() as HttpURLConnection val conn: HttpURLConnection = url.openConnection() as HttpURLConnection
conn.requestMethod = "GET" conn.requestMethod = "GET"
@@ -46,23 +49,19 @@ object UrlUtil {
fileName.toByteArray(StandardCharsets.ISO_8859_1), fileName.toByteArray(StandardCharsets.ISO_8859_1),
StandardCharsets.UTF_8 StandardCharsets.UTF_8
) )
} } else {
// 方法二
// 方法二 var newUrl: String = conn.url.file
var newUrl: String? = conn.url.file
if (newUrl == null || newUrl.isEmpty()) {
newUrl = URLDecoder.decode(newUrl, "UTF-8") newUrl = URLDecoder.decode(newUrl, "UTF-8")
var pos = newUrl.indexOf('?') var pos = newUrl.indexOf('?')
if (pos > 0) { if (pos > 0) {
newUrl = newUrl.substring(0, pos) newUrl = newUrl.substring(0, pos)
} }
pos = newUrl!!.lastIndexOf('/') pos = newUrl.lastIndexOf('/')
fileName = newUrl.substring(pos + 1) fileName = newUrl.substring(pos + 1)
} }
} catch (e: Exception) { fileName
e.printStackTrace() }.getOrNull()
}
return fileName
} }
fun getSuffix(url: String, default: String): String { fun getSuffix(url: String, default: String): String {