This commit is contained in:
kunfei
2023-03-15 20:31:05 +08:00
parent 15cc4a6c1b
commit f74695f7f2
3 changed files with 27 additions and 10 deletions
@@ -242,9 +242,7 @@ class BookInfoViewModel(application: Application) : BaseViewModel(application) {
val fileName = "${book.name} 作者:${book.author}" val fileName = "${book.name} 作者:${book.author}"
book.downloadUrls!!.map { book.downloadUrls!!.map {
val mFileName = UrlUtil.getFileName(it) ?: fileName val mFileName = UrlUtil.getFileName(it) ?: fileName
val isSupportedFile = AppPattern.bookFileRegex.matches(mFileName) WebFile(it, mFileName)
val isSupportDecompress = AppPattern.archiveFileRegex.matches(mFileName)
WebFile(it, mFileName, isSupportedFile, isSupportDecompress)
} }
}.onError { }.onError {
context.toastOnUi("LoadWebFileError\n${it.localizedMessage}") context.toastOnUi("LoadWebFileError\n${it.localizedMessage}")
@@ -259,10 +257,18 @@ class BookInfoViewModel(application: Application) : BaseViewModel(application) {
execute { execute {
waitDialogData.postValue(true) waitDialogData.postValue(true)
if (webFile.isSupported) { if (webFile.isSupported) {
val book = LocalBook.importFileOnLine(webFile.url, webFile.name, bookSource) val book = LocalBook.importFileOnLine(
webFile.url,
bookData.value!!.getExportFileName(webFile.suffix),
bookSource
)
changeToLocalBook(book) changeToLocalBook(book)
} else { } else {
LocalBook.saveBookFile(webFile.url, webFile.name, bookSource) LocalBook.saveBookFile(
webFile.url,
bookData.value!!.getExportFileName(webFile.suffix),
bookSource
)
} }
}.onSuccess { }.onSuccess {
@Suppress("unchecked_cast") @Suppress("unchecked_cast")
@@ -437,14 +443,21 @@ class BookInfoViewModel(application: Application) : BaseViewModel(application) {
data class WebFile( data class WebFile(
val url: String, val url: String,
val name: String, val name: String,
// txt epub umd pdf等文件
val isSupported: Boolean,
// 压缩包形式的txt epub umd pdf文件
val isSupportDecompress: Boolean
) { ) {
override fun toString(): String { override fun toString(): String {
return name return name
} }
// 后缀
val suffix: String = name.substringAfterLast(".")
// txt epub umd pdf等文件
val isSupported: Boolean = AppPattern.bookFileRegex.matches(name)
// 压缩包形式的txt epub umd pdf文件
val isSupportDecompress: Boolean = AppPattern.archiveFileRegex.matches(name)
} }
} }
@@ -54,7 +54,7 @@ object ArchiveUtils {
val name = archiveFileDoc.name val name = archiveFileDoc.name
val workPathFileDoc = getCacheFolderFileDoc(name, path) val workPathFileDoc = getCacheFolderFileDoc(name, path)
val workPath = workPathFileDoc.toString() val workPath = workPathFileDoc.toString()
return archiveFileDoc.uri.inputStream(appCtx).getOrThrow().use { return archiveFileDoc.openInputStream().getOrThrow().use {
when { when {
name.endsWith(".zip", ignoreCase = true) -> ZipUtils.unZipToPath(it, workPath) name.endsWith(".zip", ignoreCase = true) -> ZipUtils.unZipToPath(it, workPath)
name.endsWith(".rar", ignoreCase = true) -> RarUtils.unRarToPath(it, workPath) name.endsWith(".rar", ignoreCase = true) -> RarUtils.unRarToPath(it, workPath)
@@ -211,6 +211,10 @@ fun FileDoc.createFolderIfNotExist(
} }
} }
fun FileDoc.openInputStream(): Result<InputStream> {
return uri.inputStream(appCtx)
}
fun FileDoc.exists( fun FileDoc.exists(
fileName: String, fileName: String,
vararg subDirs: String vararg subDirs: String