pull/1966/head
kunfei 3 years ago
parent 304510505d
commit ea06eb5d85
  1. 2
      app/src/main/java/io/legado/app/ui/book/remote/RemoteBookActivity.kt
  2. 182
      app/src/main/java/io/legado/app/ui/book/remote/manager/RemoteBookWebDav.kt

@ -27,6 +27,7 @@ class RemoteBookActivity : VMBaseActivity<ActivityRemoteBookBinding,RemoteBookVi
override val binding by viewBinding(ActivityRemoteBookBinding::inflate)
override val viewModel by viewModels<RemoteBookViewModel>()
private val adapter by lazy { RemoteBookAdapter(this, this) }
private val waitDialog by lazy { WaitDialog(this) }
override fun onActivityCreated(savedInstanceState: Bundle?) {
initView()
@ -51,7 +52,6 @@ class RemoteBookActivity : VMBaseActivity<ActivityRemoteBookBinding,RemoteBookVi
@SuppressLint("NotifyDataSetChanged")
override fun addToBookshelf(remoteBook: RemoteBook) {
val waitDialog = WaitDialog(this)
waitDialog.show()
viewModel.addToBookshelf(remoteBook, success = {
toastOnUi(getString(R.string.download_book_success))

@ -95,19 +95,12 @@ object RemoteBookWebDav : RemoteBookManager() {
* 下载指定的远程书籍到本地
*/
override suspend fun getRemoteBook(remoteBook: RemoteBook): Uri? {
return kotlin.runCatching {
AppWebDav.authorization?.let {
val webdav = WebDav(
remoteBook.urlName,
it
)
webdav.download().let { bytes ->
LocalBook.saveBookFile(bytes, remoteBook.filename)
}
return AppWebDav.authorization?.let {
val webdav = WebDav(remoteBook.urlName, it)
webdav.download().let { bytes ->
LocalBook.saveBookFile(bytes, remoteBook.filename)
}
}.onFailure {
throw it
}.getOrNull()
}
}
/**
@ -118,19 +111,15 @@ object RemoteBookWebDav : RemoteBookManager() {
val localBookName = localBookUri.path?.substringAfterLast(File.separator)
val putUrl = "${remoteBookUrl}${File.separator}${localBookName}"
kotlin.runCatching {
AppWebDav.authorization?.let {
if (localBookUri.isContentScheme()) {
WebDav(putUrl, it).upload(
byteArray = localBookUri.readBytes(appCtx),
contentType = "application/octet-stream"
)
} else {
WebDav(putUrl, it).upload(localBookUri.path!!)
}
AppWebDav.authorization?.let {
if (localBookUri.isContentScheme()) {
WebDav(putUrl, it).upload(
byteArray = localBookUri.readBytes(appCtx),
contentType = "application/octet-stream"
)
} else {
WebDav(putUrl, it).upload(localBookUri.path!!)
}
}.onFailure {
throw it
}
return true
}
@ -139,149 +128,4 @@ object RemoteBookWebDav : RemoteBookManager() {
TODO("Not yet implemented")
}
// suspend fun showRestoreDialog(context: Context) {
// val names = withContext(Dispatchers.IO) { getBackupNames() }
// if (names.isNotEmpty()) {
// withContext(Dispatchers.Main) {
// context.selector(
// title = context.getString(R.string.select_restore_file),
// items = names
// ) { _, index ->
// if (index in 0 until names.size) {
// Coroutine.async {
// restoreWebDav(names[index])
// }.onError {
// appCtx.toastOnUi("WebDav恢复出错\n${it.localizedMessage}")
// }
// }
// }
// }
// } else {
// throw NoStackTraceException("Web dav no back up file")
// }
// }
//
// @Throws(WebDavException::class)
// suspend fun restoreWebDav(name: String) {
// authorization?.let {
// val webDav = WebDav(rootWebDavUrl + name, it)
// webDav.downloadTo(zipFilePath, true)
// @Suppress("BlockingMethodInNonBlockingContext")
// ZipUtils.unzipFile(zipFilePath, Backup.backupPath)
// Restore.restoreDatabase()
// Restore.restoreConfig()
// }
// }
//
// suspend fun hasBackUp(): Boolean {
// authorization?.let {
// val url = "${rootWebDavUrl}${backupFileName}"
// return WebDav(url, it).exists()
// }
// return false
// }
//
// suspend fun lastBackUp(): Result<WebDavFile?> {
// return kotlin.runCatching {
// authorization?.let {
// var lastBackupFile: WebDavFile? = null
// WebDav(rootWebDavUrl, it).listFiles().reversed().forEach { webDavFile ->
// if (webDavFile.displayName.startsWith("backup")) {
// if (lastBackupFile == null
// || webDavFile.lastModify > lastBackupFile!!.lastModify
// ) {
// lastBackupFile = webDavFile
// }
// }
// }
// lastBackupFile
// }
// }
// }
//
// @Throws(Exception::class)
// suspend fun backUpWebDav(path: String) {
// if (!NetworkUtils.isAvailable()) return
// authorization?.let {
// val paths = arrayListOf(*Backup.backupFileNames)
// for (i in 0 until paths.size) {
// paths[i] = path + File.separator + paths[i]
// }
// FileUtils.delete(zipFilePath)
// if (ZipUtils.zipFiles(paths, zipFilePath)) {
// val putUrl = "${rootWebDavUrl}${backupFileName}"
// WebDav(putUrl, it).upload(zipFilePath)
// }
// }
// }
//
// suspend fun exportWebDav(byteArray: ByteArray, fileName: String) {
// if (!NetworkUtils.isAvailable()) return
// try {
// authorization?.let {
// // 如果导出的本地文件存在,开始上传
// val putUrl = exportsWebDavUrl + fileName
// WebDav(putUrl, it).upload(byteArray, "text/plain")
// }
// } catch (e: Exception) {
// val msg = "WebDav导出\n${e.localizedMessage}"
// AppLog.put(msg)
// appCtx.toastOnUi(msg)
// }
// }
//
// fun uploadBookProgress(book: Book) {
// val authorization = authorization ?: return
// if (!syncBookProgress) return
// if (!NetworkUtils.isAvailable()) return
// Coroutine.async {
// val bookProgress = BookProgress(book)
// val json = GSON.toJson(bookProgress)
// val url = getProgressUrl(book)
// WebDav(url, authorization).upload(json.toByteArray(), "application/json")
// }.onError {
// AppLog.put("上传进度失败\n${it.localizedMessage}")
// }
// }
//
// private fun getProgressUrl(book: Book): String {
// return bookProgressUrl + book.name + "_" + book.author + ".json"
// }
//
// /**
// * 获取书籍进度
// */
// suspend fun getBookProgress(book: Book): BookProgress? {
// authorization?.let {
// val url = getProgressUrl(book)
// kotlin.runCatching {
// WebDav(url, it).download().let { byteArray ->
// val json = String(byteArray)
// if (json.isJson()) {
// return GSON.fromJsonObject<BookProgress>(json).getOrNull()
// }
// }
// }
// }
// return null
// }
//
// suspend fun downloadAllBookProgress() {
// authorization ?: return
// if (!NetworkUtils.isAvailable()) return
// appDb.bookDao.all.forEach { book ->
// getBookProgress(book)?.let { bookProgress ->
// if (bookProgress.durChapterIndex > book.durChapterIndex
// || (bookProgress.durChapterIndex == book.durChapterIndex
// && bookProgress.durChapterPos > book.durChapterPos)
// ) {
// book.durChapterIndex = bookProgress.durChapterIndex
// book.durChapterPos = bookProgress.durChapterPos
// book.durChapterTitle = bookProgress.durChapterTitle
// book.durChapterTime = bookProgress.durChapterTime
// appDb.bookDao.update(book)
// }
// }
// }
// }
}
Loading…
Cancel
Save