Skip to content

Commit

Permalink
优化
Browse files Browse the repository at this point in the history
  • Loading branch information
gedoor committed Mar 15, 2023
1 parent 93de115 commit 349ab62
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 10 deletions.
31 changes: 22 additions & 9 deletions app/src/main/java/io/legado/app/ui/book/info/BookInfoViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -242,9 +242,7 @@ class BookInfoViewModel(application: Application) : BaseViewModel(application) {
val fileName = "${book.name} 作者:${book.author}"
book.downloadUrls!!.map {
val mFileName = UrlUtil.getFileName(it) ?: fileName
val isSupportedFile = AppPattern.bookFileRegex.matches(mFileName)
val isSupportDecompress = AppPattern.archiveFileRegex.matches(mFileName)
WebFile(it, mFileName, isSupportedFile, isSupportDecompress)
WebFile(it, mFileName)
}
}.onError {
context.toastOnUi("LoadWebFileError\n${it.localizedMessage}")
Expand All @@ -259,10 +257,18 @@ class BookInfoViewModel(application: Application) : BaseViewModel(application) {
execute {
waitDialogData.postValue(true)
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)
} else {
LocalBook.saveBookFile(webFile.url, webFile.name, bookSource)
LocalBook.saveBookFile(
webFile.url,
bookData.value!!.getExportFileName(webFile.suffix),
bookSource
)
}
}.onSuccess {
@Suppress("unchecked_cast")
Expand Down Expand Up @@ -437,14 +443,21 @@ class BookInfoViewModel(application: Application) : BaseViewModel(application) {
data class WebFile(
val url: String,
val name: String,
// txt epub umd pdf等文件
val isSupported: Boolean,
// 压缩包形式的txt epub umd pdf文件
val isSupportDecompress: Boolean
) {

override fun toString(): String {
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)

}

}
2 changes: 1 addition & 1 deletion app/src/main/java/io/legado/app/utils/ArchiveUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ object ArchiveUtils {
val name = archiveFileDoc.name
val workPathFileDoc = getCacheFolderFileDoc(name, path)
val workPath = workPathFileDoc.toString()
return archiveFileDoc.uri.inputStream(appCtx).getOrThrow().use {
return archiveFileDoc.openInputStream().getOrThrow().use {
when {
name.endsWith(".zip", ignoreCase = true) -> ZipUtils.unZipToPath(it, workPath)
name.endsWith(".rar", ignoreCase = true) -> RarUtils.unRarToPath(it, workPath)
Expand Down
4 changes: 4 additions & 0 deletions app/src/main/java/io/legado/app/utils/FileDocExtensions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,10 @@ fun FileDoc.createFolderIfNotExist(
}
}

fun FileDoc.openInputStream(): Result<InputStream> {
return uri.inputStream(appCtx)
}

fun FileDoc.exists(
fileName: String,
vararg subDirs: String
Expand Down

0 comments on commit 349ab62

Please sign in to comment.