From c622ed579d4b47102dba77261a87df6016297a02 Mon Sep 17 00:00:00 2001 From: 10bits Date: Wed, 16 Sep 2020 17:58:11 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=AF=E6=8C=81=E7=A6=BB=E7=BA=BF=E4=B8=8B?= =?UTF-8?q?=E8=BD=BD=E4=B9=A6=E7=B1=8D=E5=AF=BC=E5=87=BA=E5=88=B0webdav?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../io/legado/app/help/storage/WebDavHelp.kt | 25 ++++++++++++++++--- .../app/ui/book/cache/CacheViewModel.kt | 20 ++++++++++++--- 2 files changed, 38 insertions(+), 7 deletions(-) diff --git a/app/src/main/java/io/legado/app/help/storage/WebDavHelp.kt b/app/src/main/java/io/legado/app/help/storage/WebDavHelp.kt index e728c28de..327ed5fda 100644 --- a/app/src/main/java/io/legado/app/help/storage/WebDavHelp.kt +++ b/app/src/main/java/io/legado/app/help/storage/WebDavHelp.kt @@ -10,10 +10,7 @@ import io.legado.app.help.coroutine.Coroutine import io.legado.app.lib.dialogs.selector import io.legado.app.lib.webdav.WebDav import io.legado.app.lib.webdav.http.HttpAuth -import io.legado.app.utils.FileUtils -import io.legado.app.utils.ZipUtils -import io.legado.app.utils.getPrefBoolean -import io.legado.app.utils.getPrefString +import io.legado.app.utils.* import kotlinx.coroutines.Dispatchers.IO import kotlinx.coroutines.Dispatchers.Main import kotlinx.coroutines.withContext @@ -125,4 +122,24 @@ object WebDavHelp { } } } + fun exportWebDav(path: String, fileName: String) { + try { + if (initWebDav()) { + //默认导出到legado文件夹下exports目录 + val exportsWebDavUrl = rootWebDavUrl + EncoderUtils.escape("exports") + "/" + //在legado文件夹创建exports目录,如果不存在的话 + WebDav(exportsWebDavUrl).makeAsDir() + val file = File("${path}${File.separator}${fileName}") + //如果导出的本地文件存在,开始上传 + if(file.exists()){ + val putUrl = exportsWebDavUrl + fileName + WebDav(putUrl).upload("${path}${File.separator}${fileName}") + } + } + } catch (e: Exception) { + Handler(Looper.getMainLooper()).post { + App.INSTANCE.toast("WebDav导出\n${e.localizedMessage}") + } + } + } } \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/ui/book/cache/CacheViewModel.kt b/app/src/main/java/io/legado/app/ui/book/cache/CacheViewModel.kt index 0673aaaab..e0c67bd6b 100644 --- a/app/src/main/java/io/legado/app/ui/book/cache/CacheViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/book/cache/CacheViewModel.kt @@ -9,6 +9,7 @@ import io.legado.app.base.BaseViewModel import io.legado.app.constant.AppPattern import io.legado.app.data.entities.Book import io.legado.app.help.BookHelp +import io.legado.app.help.storage.WebDavHelp import io.legado.app.utils.* import java.io.File @@ -34,8 +35,19 @@ class CacheViewModel(application: Application) : BaseViewModel(application) { } private fun export(doc: DocumentFile, book: Book) { - DocumentUtils.createFileIfNotExist(doc, "${book.name} 作者:${book.author}.txt") - ?.writeText(context, getAllContents(book)) + val filename = "${book.name} 作者:${book.author}.txt" + val content = getAllContents(book) + DocumentUtils.createFileIfNotExist(doc, filename) + ?.writeText(context, content) + //写出文件到cache目录 + FileUtils.createFileIfNotExist( + File(FileUtils.getCachePath()), + filename + ).writeText(content) + //导出到webdav + WebDavHelp.exportWebDav(FileUtils.getCachePath(), filename) + //上传完删除cache文件 + FileUtils.deleteFile("${FileUtils.getCachePath()}${File.separator}${filename}") App.db.bookChapterDao().getChapterList(book.bookUrl).forEach { chapter -> BookHelp.getContent(book, chapter).let { content -> content?.split("\n")?.forEachIndexed { index, text -> @@ -61,8 +73,10 @@ class CacheViewModel(application: Application) : BaseViewModel(application) { } private fun export(file: File, book: Book) { - FileUtils.createFileIfNotExist(file, "${book.name} 作者:${book.author}.txt") + val filename = "${book.name} 作者:${book.author}.txt" + FileUtils.createFileIfNotExist(file, filename) .writeText(getAllContents(book)) + WebDavHelp.exportWebDav(file.absolutePath, filename)//导出到webdav App.db.bookChapterDao().getChapterList(book.bookUrl).forEach { chapter -> BookHelp.getContent(book, chapter).let { content -> content?.split("\n")?.forEachIndexed { index, text ->