This commit is contained in:
Horis
2023-11-29 11:19:23 +08:00
parent df909e0cb5
commit a350758c72
2 changed files with 39 additions and 26 deletions
@@ -41,6 +41,7 @@ import io.legado.app.utils.activityPendingIntent
import io.legado.app.utils.cnCompare
import io.legado.app.utils.createFolderIfNotExist
import io.legado.app.utils.isContentScheme
import io.legado.app.utils.outputStream
import io.legado.app.utils.postEvent
import io.legado.app.utils.readBytes
import io.legado.app.utils.readText
@@ -230,9 +231,11 @@ class ExportBookService : BaseService() {
DocumentUtils.delete(doc, filename)
val bookDoc = DocumentUtils.createFileIfNotExist(doc, filename)
?: throw NoStackTraceException("创建文档失败,请尝试重新设置导出文件夹")
val charset = Charset.forName(AppConfig.exportCharset)
contentResolver.openOutputStream(bookDoc.uri, "wa")?.use { bookOs ->
BufferedOutputStream(bookOs, 64 * 1024).use { bos ->
getAllContents(book) { text, srcList ->
bookOs.write(text.toByteArray(Charset.forName(AppConfig.exportCharset)))
bos.write(text.toByteArray(charset))
srcList?.forEach {
val vFile = BookHelp.getImage(book, it.src)
if (vFile.exists()) {
@@ -249,6 +252,7 @@ class ExportBookService : BaseService() {
}
}
}
}
if (AppConfig.exportToWebDav) {
// 导出到webdav
AppWebDav.exportWebDav(bookDoc.uri, filename)
@@ -259,8 +263,11 @@ class ExportBookService : BaseService() {
val filename = book.getExportFileName("txt")
val bookPath = FileUtils.getPath(file, filename)
val bookFile = FileUtils.createFileWithReplace(bookPath)
val charset = Charset.forName(AppConfig.exportCharset)
val bos = BufferedOutputStream(bookFile.outputStream(true), 64 * 1024)
bos.use {
getAllContents(book) { text, srcList ->
bookFile.appendText(text, Charset.forName(AppConfig.exportCharset))
bos.write(text.toByteArray(charset))
srcList?.forEach {
val vFile = BookHelp.getImage(book, it.src)
if (vFile.exists()) {
@@ -274,6 +281,7 @@ class ExportBookService : BaseService() {
}
}
}
}
if (AppConfig.exportToWebDav) {
AppWebDav.exportWebDav(Uri.fromFile(bookFile), filename) // 导出到webdav
}
@@ -4,6 +4,7 @@ package io.legado.app.utils
import android.net.Uri
import java.io.File
import java.io.FileOutputStream
fun File.getFile(vararg subDirFiles: String): File {
val path = FileUtils.getPath(this, *subDirFiles)
@@ -79,3 +80,7 @@ fun File.checkWrite(): Boolean {
false
}
}
fun File.outputStream(append: Boolean = false): FileOutputStream {
return FileOutputStream(this, append)
}