feat:导出txt时不导出图片,修正epub图片链接

阅读导出的图片其他软件不能直接使用,还需要额外适配,有导出图片需求的使用epub导出
This commit is contained in:
Xwite
2022-04-12 07:30:22 +08:00
parent 6c6103d50e
commit 1f814190a0
@@ -86,19 +86,9 @@ class CacheViewModel(application: Application) : BaseViewModel(application) {
?: throw NoStackTraceException("创建文档失败") ?: throw NoStackTraceException("创建文档失败")
val stringBuilder = StringBuilder() val stringBuilder = StringBuilder()
context.contentResolver.openOutputStream(bookDoc.uri, "wa")?.use { bookOs -> 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))) bookOs.write(text.toByteArray(Charset.forName(AppConfig.exportCharset)))
stringBuilder.append(text) 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) { if (AppConfig.exportToWebDav) {
@@ -114,21 +104,9 @@ class CacheViewModel(application: Application) : BaseViewModel(application) {
val bookPath = FileUtils.getPath(file, filename) val bookPath = FileUtils.getPath(file, filename)
val bookFile = FileUtils.createFileWithReplace(bookPath) val bookFile = FileUtils.createFileWithReplace(bookPath)
val stringBuilder = StringBuilder() val stringBuilder = StringBuilder()
getAllContents(scope, book) { text, srcList -> getAllContents(scope, book) { text ->
bookFile.appendText(text, Charset.forName(AppConfig.exportCharset)) bookFile.appendText(text, Charset.forName(AppConfig.exportCharset))
stringBuilder.append(text) 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) { if (AppConfig.exportToWebDav) {
val byteArray = val byteArray =
@@ -140,7 +118,7 @@ class CacheViewModel(application: Application) : BaseViewModel(application) {
private fun getAllContents( private fun getAllContents(
scope: CoroutineScope, scope: CoroutineScope,
book: Book, book: Book,
append: (text: String, srcList: ArrayList<Triple<String, Int, String>>?) -> Unit append: (text: String) -> Unit
) { ) {
val useReplace = AppConfig.exportUseReplace && book.getUseReplaceRule() val useReplace = AppConfig.exportUseReplace && book.getUseReplaceRule()
val contentProcessor = ContentProcessor.get(book.name, book.origin) val contentProcessor = ContentProcessor.get(book.name, book.origin)
@@ -152,7 +130,7 @@ class CacheViewModel(application: Application) : BaseViewModel(application) {
"\n" + HtmlFormatter.format(book.getDisplayIntro()) "\n" + HtmlFormatter.format(book.getDisplayIntro())
) )
}" }"
append(qy, null) append(qy)
appDb.bookChapterDao.getChapterList(book.bookUrl).forEachIndexed { index, chapter -> appDb.bookChapterDao.getChapterList(book.bookUrl).forEachIndexed { index, chapter ->
scope.ensureActive() scope.ensureActive()
upAdapterLiveData.postValue(book.bookUrl) upAdapterLiveData.postValue(book.bookUrl)
@@ -168,17 +146,7 @@ class CacheViewModel(application: Application) : BaseViewModel(application) {
chineseConvert = false, chineseConvert = false,
reSegment = false reSegment = false
).joinToString("\n") ).joinToString("\n")
val srcList = arrayListOf<Triple<String, Int, String>>() append.invoke("\n\n$content1")
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)
} }
} }
} }
@@ -460,7 +428,7 @@ class CacheViewModel(application: Application) : BaseViewModel(application) {
while (matcher.find()) { while (matcher.find()) {
matcher.group(1)?.let { matcher.group(1)?.let {
val src = NetworkUtils.getAbsoluteURL(chapter.url, it) 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 href = "Images/${MD5Utils.md5Encode16(src)}.${BookHelp.getImageSuffix(src)}"
val vFile = BookHelp.getImage(book, src) val vFile = BookHelp.getImage(book, src)
val fp = FileResourceProvider(vFile.parent) val fp = FileResourceProvider(vFile.parent)