pull/1990/head
kunfei 2 years ago
parent 94c6be7202
commit 61aee76c53
  1. 34
      app/src/main/java/io/legado/app/lib/webdav/WebDav.kt
  2. 3
      app/src/main/java/io/legado/app/model/ReadBook.kt
  3. 8
      app/src/main/java/io/legado/app/ui/book/remote/RemoteBook.kt
  4. 11
      app/src/main/java/io/legado/app/ui/book/remote/RemoteBookAdapter.kt
  5. 11
      app/src/main/java/io/legado/app/ui/book/remote/manager/RemoteBookWebDav.kt

@ -6,6 +6,7 @@ import io.legado.app.exception.NoStackTraceException
import io.legado.app.help.http.newCallResponse
import io.legado.app.help.http.okHttpClient
import io.legado.app.help.http.text
import io.legado.app.utils.NetworkUtils
import io.legado.app.utils.printOnDebug
import okhttp3.MediaType.Companion.toMediaType
import okhttp3.RequestBody.Companion.asRequestBody
@ -61,11 +62,14 @@ open class WebDav(urlStr: String, val authorization: Authorization) {
/**
* 填充文件信息实例化WebDAVFile对象时并没有将远程文件的信息填充到实例中需要手动填充
* @return 远程文件是否存在
* 获取当前url文件信息
*/
suspend fun indexFileInfo(): Boolean {
return !propFindResponse(ArrayList()).isNullOrEmpty()
suspend fun getWebDavFile(): WebDavFile? {
return kotlin.runCatching {
propFindResponse(depth = 0)?.let {
return parseBody(it).firstOrNull()
}
}.getOrNull()
}
/**
@ -76,9 +80,11 @@ open class WebDav(urlStr: String, val authorization: Authorization) {
@Throws(WebDavException::class)
suspend fun listFiles(): List<WebDavFile> {
propFindResponse()?.let { body ->
return parseDir(body)
return parseBody(body).filter {
it.path != path
}
return ArrayList()
}
return emptyList()
}
/**
@ -112,12 +118,12 @@ open class WebDav(urlStr: String, val authorization: Authorization) {
}.body?.text()
}
private fun parseDir(s: String): List<WebDavFile> {
private fun parseBody(s: String): List<WebDavFile> {
val list = ArrayList<WebDavFile>()
val document = Jsoup.parse(s)
val elements = document.getElementsByTag("d:response")
httpUrl?.let { urlStr ->
val baseUrl = if (urlStr.endsWith("/")) urlStr else "$urlStr/"
val baseUrl = NetworkUtils.getBaseUrl(urlStr)
for (element in elements) {
val href = element.getElementsByTag("d:href")[0].text()
if (!href.endsWith("/")) {
@ -137,11 +143,12 @@ open class WebDav(urlStr: String, val authorization: Authorization) {
val lastModify: Long = kotlin.runCatching {
element.getElementsByTag("d:getlastmodified")
.firstOrNull()?.text()?.let {
LocalDateTime.parse(it, dateTimeFormatter).toInstant(ZoneOffset.of("+8")).toEpochMilli()
LocalDateTime.parse(it, dateTimeFormatter)
.toInstant(ZoneOffset.of("+8")).toEpochMilli()
}
}.getOrNull() ?: 0
webDavFile = WebDavFile(
baseUrl + fileName,
"$baseUrl$href",
authorization,
displayName = fileName,
urlName = urlName,
@ -163,12 +170,7 @@ open class WebDav(urlStr: String, val authorization: Authorization) {
* 文件是否存在
*/
suspend fun exists(): Boolean {
return kotlin.runCatching {
val response = propFindResponse(depth = 0) ?: return false
val document = Jsoup.parse(response)
val elements = document.getElementsByTag("d:response")
return elements.isNotEmpty()
}.getOrDefault(false)
return getWebDavFile() != null
}
/**

@ -21,6 +21,7 @@ import kotlinx.coroutines.Dispatchers.IO
import kotlinx.coroutines.MainScope
import kotlinx.coroutines.delay
import splitties.init.appCtx
import kotlin.math.min
@Suppress("MemberVisibilityCanBePrivate")
@ -437,7 +438,7 @@ object ReadBook : CoroutineScope by MainScope() {
delay(1000)
download(i)
}
val minChapterIndex = durChapterIndex - 5
val minChapterIndex = durChapterIndex - min(5, AppConfig.preDownloadNum)
for (i in durChapterIndex.minus(2) downTo minChapterIndex) {
delay(1000)
download(i)

@ -7,4 +7,10 @@ data class RemoteBook(
val contentType: String,
val lastModify: Long,
val isOnBookShelf: Boolean
)
) {
val isDir by lazy {
contentType == "folder"
}
}

@ -2,6 +2,7 @@ package io.legado.app.ui.book.remote
import android.content.Context
import android.view.ViewGroup
import androidx.core.view.isGone
import cn.hutool.core.date.LocalDateTimeUtil
import io.legado.app.base.adapter.ItemViewHolder
import io.legado.app.base.adapter.RecyclerAdapter
@ -37,7 +38,11 @@ class RemoteBookAdapter (context: Context, val callBack: CallBack) :
tvName.text = item.filename.substringBeforeLast(".")
tvContentType.text = item.contentType
tvSize.text = ConvertUtils.formatFileSize(item.size)
tvDate.text = LocalDateTimeUtil.format(LocalDateTimeUtil.of(item.lastModify), "yyyy-MM-dd")
tvDate.text =
LocalDateTimeUtil.format(LocalDateTimeUtil.of(item.lastModify), "yyyy-MM-dd")
llInfo.isGone = item.isDir
tvContentType.isGone = item.isDir
btnDownload.isGone = item.isDir
}
}
@ -45,9 +50,13 @@ class RemoteBookAdapter (context: Context, val callBack: CallBack) :
binding.btnDownload.setOnClickListener {
getItem(holder.layoutPosition)?.let {
if (it.isDir) {
} else {
callBack.addToBookshelf(it)
}
}
}
}

@ -49,8 +49,14 @@ object RemoteBookWebDav : RemoteBookManager() {
webDavFileName = URLDecoder.decode(webDavFileName, "utf-8")
webDavUrlName = URLDecoder.decode(webDavUrlName, "utf-8")
webDavFile.isDir
if (webDavFile.isDir) {
remoteBooks.add(
RemoteBook(
webDavFileName, webDavUrlName, webDavFile.size,
"folder", webDavFile.lastModify, false
)
)
} else {
//分割后缀
val fileExtension = webDavFileName.substringAfterLast(".")
@ -65,6 +71,7 @@ object RemoteBookWebDav : RemoteBookManager() {
)
}
}
}
} ?: throw NoStackTraceException("webDav没有配置")
return remoteBooks
}

Loading…
Cancel
Save