|
|
|
@ -5,13 +5,11 @@ import android.net.Uri |
|
|
|
|
import androidx.documentfile.provider.DocumentFile |
|
|
|
|
import io.legado.app.App |
|
|
|
|
import io.legado.app.R |
|
|
|
|
import io.legado.app.constant.AppPattern |
|
|
|
|
import io.legado.app.base.BaseViewModel |
|
|
|
|
import io.legado.app.data.entities.Book |
|
|
|
|
import io.legado.app.help.BookHelp |
|
|
|
|
import io.legado.app.utils.DocumentUtils |
|
|
|
|
import io.legado.app.utils.FileUtils |
|
|
|
|
import io.legado.app.utils.isContentPath |
|
|
|
|
import io.legado.app.utils.writeText |
|
|
|
|
import io.legado.app.utils.* |
|
|
|
|
import java.io.File |
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -38,11 +36,47 @@ class DownloadViewModel(application: Application) : BaseViewModel(application) { |
|
|
|
|
private fun export(doc: DocumentFile, book: Book) { |
|
|
|
|
DocumentUtils.createFileIfNotExist(doc, "${book.name} 作者:${book.author}.txt") |
|
|
|
|
?.writeText(context, getAllContents(book)) |
|
|
|
|
App.db.bookChapterDao().getChapterList(book.bookUrl).forEach { chapter -> |
|
|
|
|
BookHelp.getContent(book, chapter).let { content -> |
|
|
|
|
content?.split("\n")?.forEachIndexed { index, text -> |
|
|
|
|
val matcher = AppPattern.imgPattern.matcher(text) |
|
|
|
|
if (matcher.find()) { |
|
|
|
|
var src = matcher.group(1) |
|
|
|
|
src = NetworkUtils.getAbsoluteURL(chapter.url, src) |
|
|
|
|
src?.let { |
|
|
|
|
val vfile = BookHelp.getImage(book, src) |
|
|
|
|
if(vfile.exists()) { |
|
|
|
|
DocumentUtils.createFileIfNotExist(doc, "${index}-${MD5Utils.md5Encode16(src)}.jpg", subDirs = arrayOf("${book.name}_${book.author}", "images", chapter.title)) |
|
|
|
|
?.writeBytes(context, vfile.readBytes()) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private fun export(file: File, book: Book) { |
|
|
|
|
FileUtils.createFileIfNotExist(file, "${book.name} 作者:${book.author}.txt") |
|
|
|
|
.writeText(getAllContents(book)) |
|
|
|
|
App.db.bookChapterDao().getChapterList(book.bookUrl).forEach { chapter -> |
|
|
|
|
BookHelp.getContent(book, chapter).let { content -> |
|
|
|
|
content?.split("\n")?.forEachIndexed { index, text -> |
|
|
|
|
val matcher = AppPattern.imgPattern.matcher(text) |
|
|
|
|
if (matcher.find()) { |
|
|
|
|
var src = matcher.group(1) |
|
|
|
|
src = NetworkUtils.getAbsoluteURL(chapter.url, src) |
|
|
|
|
src?.let { |
|
|
|
|
val vfile = BookHelp.getImage(book, src) |
|
|
|
|
if(vfile.exists()) { |
|
|
|
|
FileUtils.createFileIfNotExist(file, "${book.name}_${book.author}", "images", chapter.title, "${index}-${MD5Utils.md5Encode16(src)}.jpg") |
|
|
|
|
.writeBytes(vfile.readBytes()) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private fun getAllContents(book: Book): String { |
|
|
|
|