From 9d7c896842ffdc306136d46551b64d086d48ec97 Mon Sep 17 00:00:00 2001 From: Xwite <1797350009@qq.com> Date: Tue, 12 Apr 2022 07:25:16 +0800 Subject: [PATCH] =?UTF-8?q?feat:=E5=AF=BC=E5=87=BAtxt=E6=97=B6=E4=B8=8D?= =?UTF-8?q?=E5=AF=BC=E5=87=BA=E5=9B=BE=E7=89=87,=E4=BF=AE=E6=AD=A3epub?= =?UTF-8?q?=E5=9B=BE=E7=89=87=E9=93=BE=E6=8E=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 阅读导出的图片其他软件不能直接使用,还需要额外适配,有导出图片需求的使用epub导出 --- .../app/ui/book/cache/CacheViewModel.kt | 44 +++---------------- 1 file changed, 6 insertions(+), 38 deletions(-) diff --git a/app/src/main/java/io/legado/app/ui/book/cache/CacheViewModel.kt b/app/src/main/java/io/legado/app/ui/book/cache/CacheViewModel.kt index 74d13a090..f43d6ee46 100644 --- a/app/src/main/java/io/legado/app/ui/book/cache/CacheViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/book/cache/CacheViewModel.kt @@ -86,19 +86,9 @@ class CacheViewModel(application: Application) : BaseViewModel(application) { ?: throw NoStackTraceException("创建文档失败") val stringBuilder = StringBuilder() context.contentResolver.openOutputStream(bookDoc.uri, "wa")?.use { bookOs -> - getAllContents(scope, book) { text, srcList -> + getAllContents(scope, book) { text -> bookOs.write(text.toByteArray(Charset.forName(AppConfig.exportCharset))) stringBuilder.append(text) - srcList?.forEach { - val vFile = BookHelp.getImage(book, it.third) - if (vFile.exists()) { - DocumentUtils.createFileIfNotExist( - doc, - "${it.second}-${MD5Utils.md5Encode16(it.third)}.jpg", - subDirs = arrayOf("${book.name}_${book.author}", "images", it.first) - )?.writeBytes(context, vFile.readBytes()) - } - } } } if (AppConfig.exportToWebDav) { @@ -114,21 +104,9 @@ class CacheViewModel(application: Application) : BaseViewModel(application) { val bookPath = FileUtils.getPath(file, filename) val bookFile = FileUtils.createFileWithReplace(bookPath) val stringBuilder = StringBuilder() - getAllContents(scope, book) { text, srcList -> + getAllContents(scope, book) { text -> bookFile.appendText(text, Charset.forName(AppConfig.exportCharset)) stringBuilder.append(text) - srcList?.forEach { - val vFile = BookHelp.getImage(book, it.third) - if (vFile.exists()) { - FileUtils.createFileIfNotExist( - file, - "${book.name}_${book.author}", - "images", - it.first, - "${it.second}-${MD5Utils.md5Encode16(it.third)}.jpg" - ).writeBytes(vFile.readBytes()) - } - } } if (AppConfig.exportToWebDav) { val byteArray = @@ -140,7 +118,7 @@ class CacheViewModel(application: Application) : BaseViewModel(application) { private fun getAllContents( scope: CoroutineScope, book: Book, - append: (text: String, srcList: ArrayList>?) -> Unit + append: (text: String) -> Unit ) { val useReplace = AppConfig.exportUseReplace && book.getUseReplaceRule() val contentProcessor = ContentProcessor.get(book.name, book.origin) @@ -152,7 +130,7 @@ class CacheViewModel(application: Application) : BaseViewModel(application) { "\n" + HtmlFormatter.format(book.getDisplayIntro()) ) }" - append(qy, null) + append(qy) appDb.bookChapterDao.getChapterList(book.bookUrl).forEachIndexed { index, chapter -> scope.ensureActive() upAdapterLiveData.postValue(book.bookUrl) @@ -168,17 +146,7 @@ class CacheViewModel(application: Application) : BaseViewModel(application) { chineseConvert = false, reSegment = false ).joinToString("\n") - val srcList = arrayListOf>() - content?.split("\n")?.forEachIndexed { index, text -> - val matcher = AppPattern.imgPattern.matcher(text) - while (matcher.find()) { - matcher.group(1)?.let { - val src = NetworkUtils.getAbsoluteURL(chapter.url, it) - srcList.add(Triple(chapter.title, index, src)) - } - } - } - append.invoke("\n\n$content1", srcList) + append.invoke("\n\n$content1") } } } @@ -460,7 +428,7 @@ class CacheViewModel(application: Application) : BaseViewModel(application) { while (matcher.find()) { matcher.group(1)?.let { val src = NetworkUtils.getAbsoluteURL(chapter.url, it) - val originalHref = "${MD5Utils.md5Encode16(src)}${BookHelp.getImageSuffix(src)}" + val originalHref = "${MD5Utils.md5Encode16(src)}.${BookHelp.getImageSuffix(src)}" val href = "Images/${MD5Utils.md5Encode16(src)}.${BookHelp.getImageSuffix(src)}" val vFile = BookHelp.getImage(book, src) val fp = FileResourceProvider(vFile.parent)