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:
parent
4f2ead66a5
commit
ec8190755e
5 changed files with 39 additions and 0 deletions
|
@ -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.
|
||||
*
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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()
|
||||
|
|
|
@ -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()
|
||||
|
|
Loading…
Reference in a new issue