|
|
@ -22,9 +22,9 @@ import io.legado.app.help.AppWebDav |
|
|
|
import io.legado.app.help.BookHelp |
|
|
|
import io.legado.app.help.BookHelp |
|
|
|
import io.legado.app.help.ContentProcessor |
|
|
|
import io.legado.app.help.ContentProcessor |
|
|
|
import io.legado.app.help.config.AppConfig |
|
|
|
import io.legado.app.help.config.AppConfig |
|
|
|
|
|
|
|
import io.legado.app.help.coroutine.OrderCoroutine |
|
|
|
import io.legado.app.utils.* |
|
|
|
import io.legado.app.utils.* |
|
|
|
import kotlinx.coroutines.delay |
|
|
|
import kotlinx.coroutines.* |
|
|
|
import kotlinx.coroutines.ensureActive |
|
|
|
|
|
|
|
import kotlinx.coroutines.sync.Mutex |
|
|
|
import kotlinx.coroutines.sync.Mutex |
|
|
|
import kotlinx.coroutines.sync.withLock |
|
|
|
import kotlinx.coroutines.sync.withLock |
|
|
|
import me.ag2s.epublib.domain.* |
|
|
|
import me.ag2s.epublib.domain.* |
|
|
@ -104,10 +104,13 @@ class CacheViewModel(application: Application) : BaseViewModel(application) { |
|
|
|
val bookDoc = DocumentUtils.createFileIfNotExist(doc, filename) |
|
|
|
val bookDoc = DocumentUtils.createFileIfNotExist(doc, filename) |
|
|
|
?: throw NoStackTraceException("创建文档失败,请尝试重新设置导出文件夹") |
|
|
|
?: throw NoStackTraceException("创建文档失败,请尝试重新设置导出文件夹") |
|
|
|
val stringBuilder = StringBuilder() |
|
|
|
val stringBuilder = StringBuilder() |
|
|
|
|
|
|
|
val exportToWebDav = AppConfig.exportToWebDav |
|
|
|
context.contentResolver.openOutputStream(bookDoc.uri, "wa")?.use { bookOs -> |
|
|
|
context.contentResolver.openOutputStream(bookDoc.uri, "wa")?.use { bookOs -> |
|
|
|
getAllContents(book) { text, srcList -> |
|
|
|
getAllContents(book) { text, srcList -> |
|
|
|
bookOs.write(text.toByteArray(Charset.forName(AppConfig.exportCharset))) |
|
|
|
bookOs.write(text.toByteArray(Charset.forName(AppConfig.exportCharset))) |
|
|
|
|
|
|
|
if (exportToWebDav) { |
|
|
|
stringBuilder.append(text) |
|
|
|
stringBuilder.append(text) |
|
|
|
|
|
|
|
} |
|
|
|
srcList?.forEach { |
|
|
|
srcList?.forEach { |
|
|
|
val vFile = BookHelp.getImage(book, it.third) |
|
|
|
val vFile = BookHelp.getImage(book, it.third) |
|
|
|
if (vFile.exists()) { |
|
|
|
if (vFile.exists()) { |
|
|
@ -133,9 +136,12 @@ 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() |
|
|
|
|
|
|
|
val exportToWebDav = AppConfig.exportToWebDav |
|
|
|
getAllContents(book) { text, srcList -> |
|
|
|
getAllContents(book) { text, srcList -> |
|
|
|
bookFile.appendText(text, Charset.forName(AppConfig.exportCharset)) |
|
|
|
bookFile.appendText(text, Charset.forName(AppConfig.exportCharset)) |
|
|
|
|
|
|
|
if (exportToWebDav) { |
|
|
|
stringBuilder.append(text) |
|
|
|
stringBuilder.append(text) |
|
|
|
|
|
|
|
} |
|
|
|
srcList?.forEach { |
|
|
|
srcList?.forEach { |
|
|
|
val vFile = BookHelp.getImage(book, it.third) |
|
|
|
val vFile = BookHelp.getImage(book, it.third) |
|
|
|
if (vFile.exists()) { |
|
|
|
if (vFile.exists()) { |
|
|
@ -171,10 +177,35 @@ class CacheViewModel(application: Application) : BaseViewModel(application) { |
|
|
|
) |
|
|
|
) |
|
|
|
}" |
|
|
|
}" |
|
|
|
append(qy, null) |
|
|
|
append(qy, null) |
|
|
|
|
|
|
|
if (AppConfig.parallelExportBook) { |
|
|
|
|
|
|
|
val oc = |
|
|
|
|
|
|
|
OrderCoroutine<Pair<String, ArrayList<Triple<String, Int, String>>?>>(AppConfig.threadCount) |
|
|
|
|
|
|
|
appDb.bookChapterDao.getChapterList(book.bookUrl).forEach { chapter -> |
|
|
|
|
|
|
|
oc.submit { getExportData(book, chapter, contentProcessor, useReplace) } |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
oc.collect { index, result -> |
|
|
|
|
|
|
|
upAdapterLiveData.postValue(book.bookUrl) |
|
|
|
|
|
|
|
exportProgress[book.bookUrl] = index |
|
|
|
|
|
|
|
append.invoke(result.first, result.second) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} else { |
|
|
|
appDb.bookChapterDao.getChapterList(book.bookUrl).forEachIndexed { index, chapter -> |
|
|
|
appDb.bookChapterDao.getChapterList(book.bookUrl).forEachIndexed { index, chapter -> |
|
|
|
coroutineContext.ensureActive() |
|
|
|
coroutineContext.ensureActive() |
|
|
|
upAdapterLiveData.postValue(book.bookUrl) |
|
|
|
upAdapterLiveData.postValue(book.bookUrl) |
|
|
|
exportProgress[book.bookUrl] = index |
|
|
|
exportProgress[book.bookUrl] = index |
|
|
|
|
|
|
|
val result = getExportData(book, chapter, contentProcessor, useReplace) |
|
|
|
|
|
|
|
append.invoke(result.first, result.second) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private suspend fun getExportData( |
|
|
|
|
|
|
|
book: Book, |
|
|
|
|
|
|
|
chapter: BookChapter, |
|
|
|
|
|
|
|
contentProcessor: ContentProcessor, |
|
|
|
|
|
|
|
useReplace: Boolean |
|
|
|
|
|
|
|
): Pair<String, ArrayList<Triple<String, Int, String>>?> { |
|
|
|
BookHelp.getContent(book, chapter).let { content -> |
|
|
|
BookHelp.getContent(book, chapter).let { content -> |
|
|
|
val content1 = contentProcessor |
|
|
|
val content1 = contentProcessor |
|
|
|
.getContent( |
|
|
|
.getContent( |
|
|
@ -198,10 +229,9 @@ class CacheViewModel(application: Application) : BaseViewModel(application) { |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
append.invoke("\n\n$content1", srcList) |
|
|
|
return Pair("\n\n$content1", srcList) |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
append.invoke("\n\n$content1", null) |
|
|
|
return Pair("\n\n$content1", null) |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
@ -422,7 +452,10 @@ class CacheViewModel(application: Application) : BaseViewModel(application) { |
|
|
|
.asBitmap() |
|
|
|
.asBitmap() |
|
|
|
.load(book.getDisplayCover()) |
|
|
|
.load(book.getDisplayCover()) |
|
|
|
.into(object : CustomTarget<Bitmap>() { |
|
|
|
.into(object : CustomTarget<Bitmap>() { |
|
|
|
override fun onResourceReady(resource: Bitmap, transition: Transition<in Bitmap>?) { |
|
|
|
override fun onResourceReady( |
|
|
|
|
|
|
|
resource: Bitmap, |
|
|
|
|
|
|
|
transition: Transition<in Bitmap>? |
|
|
|
|
|
|
|
) { |
|
|
|
val stream = ByteArrayOutputStream() |
|
|
|
val stream = ByteArrayOutputStream() |
|
|
|
resource.compress(Bitmap.CompressFormat.JPEG, 100, stream) |
|
|
|
resource.compress(Bitmap.CompressFormat.JPEG, 100, stream) |
|
|
|
val byteArray: ByteArray = stream.toByteArray() |
|
|
|
val byteArray: ByteArray = stream.toByteArray() |
|
|
@ -491,8 +524,10 @@ 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 = |
|
|
|
val href = "Images/${MD5Utils.md5Encode16(src)}.${BookHelp.getImageSuffix(src)}" |
|
|
|
"${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) |
|
|
|
if (vFile.exists()) { |
|
|
|
if (vFile.exists()) { |
|
|
|