Compare commits
1 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
1f3e1ceea8 |
@@ -26,8 +26,8 @@ android {
|
||||
applicationId "com.example.androidbackupgui"
|
||||
minSdk 24
|
||||
targetSdk 34
|
||||
versionCode 12
|
||||
versionName "1.11"
|
||||
versionCode 13
|
||||
versionName "1.12"
|
||||
}
|
||||
buildFeatures {
|
||||
viewBinding true
|
||||
|
||||
@@ -42,6 +42,8 @@ interface RemoteTransport {
|
||||
|
||||
suspend fun delete(remotePath: String): AppResult<Unit>
|
||||
suspend fun exists(remotePath: String): AppResult<Boolean>
|
||||
/** Get the size of a remote file in bytes. Returns [AppResult.Failure] if not found. */
|
||||
suspend fun fileSize(remotePath: String): AppResult<Long>
|
||||
|
||||
companion object {
|
||||
|
||||
|
||||
@@ -4,6 +4,7 @@ import android.util.Log
|
||||
import fi.iki.elonen.NanoHTTPD
|
||||
import fi.iki.elonen.NanoHTTPD.IHTTPSession
|
||||
import kotlinx.coroutines.runBlocking
|
||||
import java.io.ByteArrayInputStream
|
||||
import java.io.File
|
||||
import java.util.UUID
|
||||
/**
|
||||
@@ -168,10 +169,15 @@ class ResticRestBridge(
|
||||
val remotePath = "$remoteBase/config"
|
||||
when (method) {
|
||||
NanoHTTPD.Method.HEAD -> {
|
||||
when (val result = transport.exists(remotePath)) {
|
||||
when (val exists = transport.exists(remotePath)) {
|
||||
is AppResult.Success -> {
|
||||
if (result.data) {
|
||||
newFixedLengthResponse(Response.Status.OK, "application/octet-stream", "")
|
||||
if (exists.data) {
|
||||
val sizeResult = transport.fileSize(remotePath)
|
||||
val fileSize = if (sizeResult is AppResult.Success) sizeResult.data else 0L
|
||||
newFixedLengthResponse(
|
||||
Response.Status.OK, "application/octet-stream",
|
||||
ByteArrayInputStream(ByteArray(0)), fileSize
|
||||
)
|
||||
} else {
|
||||
newFixedLengthResponse(Response.Status.NOT_FOUND, "text/plain", "")
|
||||
}
|
||||
|
||||
@@ -253,4 +253,17 @@ class SmbTransport(
|
||||
err(AppError.Remote("SMB 检查失败", "exists", cause = e))
|
||||
}
|
||||
}
|
||||
|
||||
override suspend fun fileSize(remotePath: String): AppResult<Long> =
|
||||
withContext(Dispatchers.IO) {
|
||||
try {
|
||||
val file = smbFile(remotePath)
|
||||
if (!file.exists()) return@withContext err(AppError.Remote("文件不存在", "fileSize"))
|
||||
AppResult.Success(file.length())
|
||||
} catch (e: CancellationException) {
|
||||
throw e
|
||||
} catch (e: Exception) {
|
||||
err(AppError.Remote("SMB 获取文件大小失败", "fileSize", cause = e))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -180,4 +180,19 @@ class WebdavTransport(
|
||||
err(AppError.Remote("WebDAV 检查失败", "exists", cause = e))
|
||||
}
|
||||
}
|
||||
|
||||
override suspend fun fileSize(remotePath: String): AppResult<Long> =
|
||||
withContext(Dispatchers.IO) {
|
||||
try {
|
||||
val url = buildUrl(remotePath)
|
||||
if (!sardine.exists(url)) return@withContext err(AppError.Remote("文件不存在", "fileSize"))
|
||||
val resources = sardine.list(url)
|
||||
val size = resources.firstOrNull()?.contentLength ?: 0L
|
||||
AppResult.Success(size)
|
||||
} catch (e: CancellationException) {
|
||||
throw e
|
||||
} catch (e: Exception) {
|
||||
err(AppError.Remote("WebDAV 获取文件大小失败", "fileSize", cause = e))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user