Add a method for testing if storage works

This is especially useful for WebDAV storage where the user can supply whatever URL and before proceeding, we need to know whether that URL and the provided credentials are actually working.
This commit is contained in:
Torsten Grote 2024-04-09 17:14:10 -03:00
parent 4f2ead66a5
commit ec8190755e
No known key found for this signature in database
GPG key ID: 3E5F77D92CF891FF
5 changed files with 39 additions and 0 deletions

View file

@ -9,6 +9,12 @@ import java.io.OutputStream
interface StoragePlugin {
/**
* Returns true if the plugin is working, or false if it isn't.
* @throws Exception any kind of exception to provide more info on the error
*/
suspend fun test(): Boolean
/**
* Start a new [RestoreSet] with the given token.
*

View file

@ -33,6 +33,11 @@ internal class DocumentsProviderStoragePlugin(
private val packageManager: PackageManager = appContext.packageManager
override suspend fun test(): Boolean {
val dir = storage.rootBackupDir
return dir != null && dir.exists()
}
@Throws(IOException::class)
override suspend fun startNewRestoreSet(token: Long) {
// reset current storage

View file

@ -62,6 +62,7 @@ internal abstract class WebDavStorage(
.retryOnConnectionFailure(true)
.build()
protected val baseUrl = webDavConfig.url
protected val url = "${webDavConfig.url}/$root"
@Throws(IOException::class)

View file

@ -27,6 +27,22 @@ internal class WebDavStoragePlugin(
root: String = DIRECTORY_ROOT,
) : WebDavStorage(webDavConfig, root), StoragePlugin {
override suspend fun test(): Boolean {
val location = baseUrl.toHttpUrl()
val davCollection = DavCollection(okHttpClient, location)
val webDavSupported = suspendCoroutine { cont ->
davCollection.options { davCapabilities, response ->
debugLog { "test() = $davCapabilities $response" }
if (davCapabilities.contains("1")) cont.resume(true)
else if (davCapabilities.contains("2")) cont.resume(true)
else if (davCapabilities.contains("3")) cont.resume(true)
else cont.resume(false)
}
}
return webDavSupported
}
@Throws(IOException::class)
override suspend fun startNewRestoreSet(token: Long) {
val location = "$url/$token".toHttpUrl()

View file

@ -30,6 +30,17 @@ internal class WebDavStoragePluginTest : TransportTest() {
private val plugin = WebDavStoragePlugin(context, WebDavTestConfig.getConfig())
@Test
fun `test self-test`() = runBlocking {
assertTrue(plugin.test())
val plugin2 = WebDavStoragePlugin(context, WebDavConfig("https://github.com/", "", ""))
val e = assertThrows<Exception> {
assertFalse(plugin2.test())
}
println(e)
}
@Test
fun `test restore sets and reading+writing`() = runBlocking {
val token = System.currentTimeMillis()