|
|
|
@ -3,11 +3,13 @@ package io.legado.app.ui.book.remote.manager |
|
|
|
|
|
|
|
|
|
import android.net.Uri |
|
|
|
|
import io.legado.app.constant.PreferKey |
|
|
|
|
import io.legado.app.constant.AppPattern.bookFileRegex |
|
|
|
|
import io.legado.app.exception.NoStackTraceException |
|
|
|
|
import io.legado.app.help.AppWebDav |
|
|
|
|
import io.legado.app.help.config.AppConfig |
|
|
|
|
import io.legado.app.lib.webdav.WebDav |
|
|
|
|
import io.legado.app.lib.webdav.WebDavFile |
|
|
|
|
import io.legado.app.model.localBook.LocalBook |
|
|
|
|
import io.legado.app.ui.book.remote.RemoteBook |
|
|
|
|
import io.legado.app.ui.book.remote.RemoteBookManager |
|
|
|
|
import io.legado.app.utils.* |
|
|
|
@ -17,7 +19,6 @@ import java.io.File |
|
|
|
|
|
|
|
|
|
object RemoteBookWebDav : RemoteBookManager() { |
|
|
|
|
private val remoteBookUrl get() = "${rootWebDavUrl}${remoteBookFolder}" |
|
|
|
|
private val localSaveFolder get() = "${appCtx.externalFiles.absolutePath}${File.separator}${remoteBookFolder}" |
|
|
|
|
|
|
|
|
|
init { |
|
|
|
|
runBlocking { |
|
|
|
@ -68,31 +69,35 @@ object RemoteBookWebDav : RemoteBookManager() { |
|
|
|
|
val fileExtension = webDavFileName.substringAfterLast(".") |
|
|
|
|
|
|
|
|
|
//扩展名符合阅读的格式则认为是书籍 |
|
|
|
|
if (contentTypeList.contains(fileExtension)) { |
|
|
|
|
remoteBooks.add(RemoteBook(webDavFileName,webDavUrlName,webDavFile.size,fileExtension,webDavFile.lastModify)) |
|
|
|
|
if (bookFileRegex.matches(webDavFileName)) { |
|
|
|
|
val isOnBookShelf = LocalBook.isOnBookShelf(webDavFileName) |
|
|
|
|
remoteBooks.add( |
|
|
|
|
RemoteBook( |
|
|
|
|
webDavFileName, webDavUrlName, webDavFile.size, |
|
|
|
|
fileExtension, webDavFile.lastModify, isOnBookShelf |
|
|
|
|
) |
|
|
|
|
) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} ?: throw NoStackTraceException("webDav没有配置") |
|
|
|
|
return remoteBooks |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
override suspend fun getRemoteBook(remoteBook: RemoteBook): String? { |
|
|
|
|
val saveFilePath= "${localSaveFolder}${File.separator}${remoteBook.filename}" |
|
|
|
|
kotlin.runCatching { |
|
|
|
|
override suspend fun getRemoteBook(remoteBook: RemoteBook): Uri? { |
|
|
|
|
return kotlin.runCatching { |
|
|
|
|
AppWebDav.authorization?.let { |
|
|
|
|
FileUtils.createFolderIfNotExist(localSaveFolder).run { |
|
|
|
|
val webdav = WebDav( |
|
|
|
|
remoteBook.urlName, |
|
|
|
|
it |
|
|
|
|
) |
|
|
|
|
webdav.downloadTo(saveFilePath, true) |
|
|
|
|
val webdav = WebDav( |
|
|
|
|
remoteBook.urlName, |
|
|
|
|
it |
|
|
|
|
) |
|
|
|
|
webdav.download().let { bytes -> |
|
|
|
|
LocalBook.saveBookFile(bytes, remoteBook.filename) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
}.onFailure { |
|
|
|
|
it.printStackTrace() |
|
|
|
|
return null |
|
|
|
|
} |
|
|
|
|
return saveFilePath |
|
|
|
|
null |
|
|
|
|
}.getOrNull() |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|