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