This commit is contained in:
HapeLee committed 2025-06-04 13:33:51 +08:00
1 parent b86d90021c
commit 6c0757c91b
972 files changed
+6885 -7321

No files matched your search

@@ -0,0 +1,31 @@
package io.legato.kazusa.model.remote
import androidx.annotation.Keep
import io.legato.kazusa.lib.webdav.WebDavFile
import io.legato.kazusa.model.localBook.LocalBook
@Keep
data class RemoteBook(
val filename: String,
val path: String,
val size: Long,
val lastModify: Long,
var contentType: String = "folder",
var isOnBookShelf: Boolean = false
) {
val isDir get() = contentType == "folder"
constructor(webDavFile: WebDavFile) : this(
webDavFile.displayName,
webDavFile.path,
webDavFile.size,
webDavFile.lastModify
) {
if (!webDavFile.isDir) {
contentType = webDavFile.displayName.substringAfterLast(".")
isOnBookShelf = LocalBook.isOnBookShelf(webDavFile.displayName)
}
}
}
@@ -0,0 +1,38 @@
package io.legato.kazusa.model.remote
import android.net.Uri
import io.legato.kazusa.data.entities.Book
abstract class RemoteBookManager {
/**
* 获取书籍列表
*/
@Throws(Exception::class)
abstract suspend fun getRemoteBookList(path: String): MutableList<RemoteBook>
/**
* 根据书籍地址获取书籍信息
*/
@Throws(Exception::class)
abstract suspend fun getRemoteBook(path: String): RemoteBook?
/**
* @return Uri:下载到本地的路径
*/
@Throws(Exception::class)
abstract suspend fun downloadRemoteBook(remoteBook: RemoteBook): Uri
/**
* 上传书籍
*/
@Throws(Exception::class)
abstract suspend fun upload(book: Book)
/**
* 删除书籍
*/
@Throws(Exception::class)
abstract suspend fun delete(remoteBookUrl: String)
}
@@ -0,0 +1,90 @@
package io.legato.kazusa.model.remote
import android.net.Uri
import io.legato.kazusa.constant.AppPattern.archiveFileRegex
import io.legato.kazusa.constant.AppPattern.bookFileRegex
import io.legato.kazusa.constant.BookType
import io.legato.kazusa.data.entities.Book
import io.legato.kazusa.exception.NoStackTraceException
import io.legato.kazusa.help.book.update
import io.legato.kazusa.help.config.AppConfig
import io.legato.kazusa.lib.webdav.Authorization
import io.legato.kazusa.lib.webdav.WebDav
import io.legato.kazusa.lib.webdav.WebDavFile
import io.legato.kazusa.model.analyzeRule.CustomUrl
import io.legato.kazusa.model.localBook.LocalBook
import io.legato.kazusa.utils.NetworkUtils
import io.legato.kazusa.utils.isContentScheme
import kotlinx.coroutines.runBlocking
class RemoteBookWebDav(
val rootBookUrl: String,
val authorization: Authorization,
val serverID: Long? = null
) : RemoteBookManager() {
init {
runBlocking {
WebDav(rootBookUrl, authorization).makeAsDir()
}
}
@Throws(Exception::class)
override suspend fun getRemoteBookList(path: String): MutableList<RemoteBook> {
if (!NetworkUtils.isAvailable()) throw NoStackTraceException("网络不可用")
val remoteBooks = mutableListOf<RemoteBook>()
//读取文件列表
val remoteWebDavFileList: List<WebDavFile> = WebDav(path, authorization).listFiles()
//转化远程文件信息到本地对象
remoteWebDavFileList.forEach { webDavFile ->
if (webDavFile.isDir
|| bookFileRegex.matches(webDavFile.displayName)
|| archiveFileRegex.matches(webDavFile.displayName)
) {
//扩展名符合阅读的格式则认为是书籍
remoteBooks.add(RemoteBook(webDavFile))
}
}
return remoteBooks
}
override suspend fun getRemoteBook(path: String): RemoteBook? {
if (!NetworkUtils.isAvailable()) throw NoStackTraceException("网络不可用")
val webDavFile = WebDav(path, authorization).getWebDavFile()
?: return null
return RemoteBook(webDavFile)
}
override suspend fun downloadRemoteBook(remoteBook: RemoteBook): Uri {
AppConfig.defaultBookTreeUri
?: throw NoStackTraceException("没有设置书籍保存位置!")
if (!NetworkUtils.isAvailable()) throw NoStackTraceException("网络不可用")
val webdav = WebDav(remoteBook.path, authorization)
return webdav.downloadInputStream().let { inputStream ->
LocalBook.saveBookFile(inputStream, remoteBook.filename)
}
}
override suspend fun upload(book: Book) {
if (!NetworkUtils.isAvailable()) throw NoStackTraceException("网络不可用")
val localBookUri = Uri.parse(book.bookUrl)
val putUrl = "$rootBookUrl${book.originName}"
val webDav = WebDav(putUrl, authorization)
if (localBookUri.isContentScheme()) {
webDav.upload(localBookUri)
} else {
webDav.upload(localBookUri.path!!)
}
book.origin = BookType.webDavTag + CustomUrl(putUrl)
.putAttribute("serverID", serverID)
.toString()
book.update()
}
override suspend fun delete(remoteBookUrl: String) {
if (!NetworkUtils.isAvailable()) throw NoStackTraceException("网络不可用")
WebDav(remoteBookUrl, authorization).delete()
}
}