背景同步

This commit is contained in:
kunfei 2023-08-02 22:34:20 +08:00
parent c3bea1cb0f
commit 389cd81ede
2 changed files with 20 additions and 3 deletions

View File

@ -72,6 +72,7 @@ object AppWebDav {
WebDav(rootWebDavUrl, mAuthorization).makeAsDir()
WebDav(bookProgressUrl, mAuthorization).makeAsDir()
WebDav(exportsWebDavUrl, mAuthorization).makeAsDir()
WebDav(bgWebDavUrl, mAuthorization).makeAsDir()
val rootBooksUrl = "${rootWebDavUrl}books"
defaultBookWebDav = RemoteBookWebDav(rootBooksUrl, mAuthorization)
authorization = mAuthorization
@ -159,7 +160,7 @@ object AppWebDav {
/**
* 获取云端所有背景名称
*/
suspend fun getAllBgWebDavFiles(): Result<List<WebDavFile>> {
private suspend fun getAllBgWebDavFiles(): Result<List<WebDavFile>> {
return kotlin.runCatching {
if (!NetworkUtils.isAvailable())
throw NoStackTraceException("网络未连接")
@ -173,9 +174,18 @@ object AppWebDav {
/**
* 上传背景图片
*/
suspend fun upBgs(files: List<File>) {
suspend fun upBgs(files: Array<File>) {
val authorization = authorization ?: return
if (!NetworkUtils.isAvailable()) return
val bgWebDavFiles = getAllBgWebDavFiles().getOrThrow()
.map { it.displayName }
.toSet()
files.forEach {
if (!bgWebDavFiles.contains(it.name)) {
WebDav("$bgWebDavUrl${it.name}", authorization)
.upload(it)
}
}
}
/**

View File

@ -304,10 +304,17 @@ open class WebDav(
suspend fun upload(
localPath: String,
contentType: String = "application/octet-stream"
) {
upload(File(localPath), contentType)
}
@Throws(WebDavException::class)
suspend fun upload(
file: File,
contentType: String = "application/octet-stream"
) {
kotlin.runCatching {
withContext(IO) {
val file = File(localPath)
if (!file.exists()) throw WebDavException("文件不存在")
// 务必注意RequestBody不要嵌套不然上传时内容可能会被追加多余的文件信息
val fileBody = file.asRequestBody(contentType.toMediaType())