This commit is contained in:
Horis 2024-03-13 22:33:25 +08:00
parent 005839257f
commit bbc2f035aa
2 changed files with 87 additions and 75 deletions

View File

@ -31,6 +31,7 @@ import io.legado.app.help.config.AppConfig
import io.legado.app.model.localBook.LocalBook import io.legado.app.model.localBook.LocalBook
import io.legado.app.ui.book.cache.CacheActivity import io.legado.app.ui.book.cache.CacheActivity
import io.legado.app.utils.DocumentUtils import io.legado.app.utils.DocumentUtils
import io.legado.app.utils.FileDoc
import io.legado.app.utils.FileUtils import io.legado.app.utils.FileUtils
import io.legado.app.utils.HtmlFormatter import io.legado.app.utils.HtmlFormatter
import io.legado.app.utils.MD5Utils import io.legado.app.utils.MD5Utils
@ -38,13 +39,13 @@ import io.legado.app.utils.NetworkUtils
import io.legado.app.utils.activityPendingIntent import io.legado.app.utils.activityPendingIntent
import io.legado.app.utils.cnCompare import io.legado.app.utils.cnCompare
import io.legado.app.utils.createFolderIfNotExist import io.legado.app.utils.createFolderIfNotExist
import io.legado.app.utils.find
import io.legado.app.utils.isContentScheme import io.legado.app.utils.isContentScheme
import io.legado.app.utils.list
import io.legado.app.utils.mapAsync import io.legado.app.utils.mapAsync
import io.legado.app.utils.mapAsyncIndexed import io.legado.app.utils.mapAsyncIndexed
import io.legado.app.utils.outputStream import io.legado.app.utils.outputStream
import io.legado.app.utils.postEvent import io.legado.app.utils.postEvent
import io.legado.app.utils.readBytes
import io.legado.app.utils.readText
import io.legado.app.utils.servicePendingIntent import io.legado.app.utils.servicePendingIntent
import io.legado.app.utils.toastOnUi import io.legado.app.utils.toastOnUi
import io.legado.app.utils.writeBytes import io.legado.app.utils.writeBytes
@ -461,7 +462,7 @@ class ExportBookService : BaseService() {
//set cover //set cover
setCover(book, epubBook) setCover(book, epubBook)
//set css //set css
val contentModel = setAssets(book, epubBook) val contentModel = setAssets(file, book, epubBook)
val bookPath = FileUtils.getPath(file, filename) val bookPath = FileUtils.getPath(file, filename)
val bookFile = FileUtils.createFileWithReplace(bookPath) val bookFile = FileUtils.createFileWithReplace(bookPath)
@ -477,84 +478,91 @@ class ExportBookService : BaseService() {
} }
private fun setAssets(doc: DocumentFile, book: Book, epubBook: EpubBook): String { private fun setAssets(doc: DocumentFile, book: Book, epubBook: EpubBook): String {
var contentModel = "" return setAssets(FileDoc.fromDocumentFile(doc), book, epubBook)
DocumentUtils.getDirDocument(doc, "Asset").let { customPath -> }
if (customPath == null) {//使用内置模板
contentModel = setAssets(book, epubBook)
} else {//外部模板
customPath.listFiles().forEach { folder ->
if (folder.isDirectory && folder.name == "Text") {
folder.listFiles().sortedWith { o1, o2 ->
val name1 = o1.name ?: ""
val name2 = o2.name ?: ""
name1.cnCompare(name2)
}.forEach { file ->
if (file.isFile) {
when {
//正文模板
file.name.equals("chapter.html", true)
|| file.name.equals("chapter.xhtml", true) -> {
contentModel = file.readText(this)
}
//封面等其他模板
true == file.name?.endsWith("html", true) -> {
epubBook.addSection(
FileUtils.getNameExcludeExtension(
file.name ?: "Cover.html"
),
ResourceUtil.createPublicResource(
book.name,
book.getRealAuthor(),
book.getDisplayIntro(),
book.kind,
book.wordCount,
file.readText(this),
"${folder.name}/${file.name}"
)
)
}
else -> { private fun setAssets(file: File, book: Book, epubBook: EpubBook): String {
//其他格式文件当做资源文件 return setAssets(FileDoc.fromFile(file), book, epubBook)
folder.listFiles().forEach { }
if (it.isFile)
epubBook.resources.add( private fun setAssets(doc: FileDoc, book: Book, epubBook: EpubBook): String {
Resource( val customPath = doc.find("Asset")
it.readBytes(this), val contentModel = if (customPath == null) {//使用内置模板
"${folder.name}/${it.name}" setAssets(book, epubBook)
) } else {//外部模板
) setAssetsExternal(customPath, book, epubBook)
}
}
}
}
}
} else if (folder.isDirectory) {
//资源文件
folder.listFiles().forEach {
if (it.isFile)
epubBook.resources.add(
Resource(
it.readBytes(this),
"${folder.name}/${it.name}"
)
)
}
} else {//Asset下面的资源文件
epubBook.resources.add(
Resource(
folder.readBytes(this),
"${folder.name}"
)
)
}
}
}
} }
return contentModel return contentModel
} }
private fun setAssetsExternal(doc: FileDoc, book: Book, epubBook: EpubBook): String {
var contentModel = ""
doc.list()!!.forEach { folder ->
if (folder.isDir && folder.name == "Text") {
folder.list()!!.sortedWith { o1, o2 ->
o1.name.cnCompare(o2.name)
}.forEach loop@{ file ->
if (file.isDir) {
return@loop
}
when {
//正文模板
file.name.equals("chapter.html", true)
|| file.name.equals("chapter.xhtml", true) -> {
contentModel = file.readText()
}
//封面等其他模板
file.name.endsWith("html", true) -> {
epubBook.addSection(
FileUtils.getNameExcludeExtension(file.name),
ResourceUtil.createPublicResource(
book.name,
book.getRealAuthor(),
book.getDisplayIntro(),
book.kind,
book.wordCount,
file.readText(),
"${folder.name}/${file.name}"
)
)
}
//其他格式文件当做资源文件
else -> {
epubBook.resources.add(
Resource(
file.readBytes(),
"${folder.name}/${file.name}"
)
)
}
}
}
} else if (folder.isDir) {
//资源文件
folder.list()!!.forEach loop2@{
if (it.isDir) {
return@loop2
}
epubBook.resources.add(
Resource(
it.readBytes(),
"${folder.name}/${it.name}"
)
)
}
} else {//Asset下面的资源文件
epubBook.resources.add(
Resource(
folder.readBytes(),
folder.name
)
)
}
}
return contentModel
}
private fun setAssets(book: Book, epubBook: EpubBook): String { private fun setAssets(book: Book, epubBook: EpubBook): String {
epubBook.resources.add( epubBook.resources.add(
Resource( Resource(

View File

@ -36,6 +36,10 @@ data class FileDoc(
return uri.readBytes(appCtx) return uri.readBytes(appCtx)
} }
fun readText(): String {
return uri.readText(appCtx)
}
fun asDocumentFile(): DocumentFile? { fun asDocumentFile(): DocumentFile? {
if (isContentScheme) { if (isContentScheme) {
return if (isDir) { return if (isDir) {