Skip to content
Snippets Groups Projects
Commit bc5bf155 authored by Kamal Bramwell's avatar Kamal Bramwell
Browse files

Added Crust backup/restore data source

parent 54052b62
No related branches found
No related tags found
No related merge requests found
package io.xxlabs.messenger.backup.cloud.crust
import io.xxlabs.messenger.R
import io.xxlabs.messenger.backup.bindings.AccountArchive
import io.xxlabs.messenger.backup.bindings.BackupService
import io.xxlabs.messenger.backup.cloud.CloudStorage
import io.xxlabs.messenger.backup.data.backup.BackupPreferencesRepository
import io.xxlabs.messenger.backup.data.restore.RestoreEnvironment
import io.xxlabs.messenger.backup.model.BackupLocation
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
/**
* Encapsulates Crust storage API.
*/
class Crust private constructor(
private val backupService: BackupService,
private val preferences: BackupPreferencesRepository,
private val crustApi: CrustDataSource
) : CloudStorage(backupService) {
private var cachedBackupData: AccountArchive? = null
override val location: BackupLocation = BackupLocationData(
R.drawable.ic_sftp,
"Crust",
::signInRequired,
::authBackgroundsApp,
::signOut,
::isEnabled
) {
null
}
private fun signInRequired(): Boolean = false
private fun authBackgroundsApp() = false
private fun signOut() {}
override fun onAuthResultSuccess() {
scope.launch {
fetchData()
withContext(Dispatchers.Main) {
_authResultCallback?.onSuccess()
}
}
}
private suspend fun fetchData() {
cachedBackupData = crustApi.recoverBackup("").getOrNull()?.let {
AccountArchive(it)
}
}
override suspend fun onRestore(environment: RestoreEnvironment) {
updateProgress(25)
cachedBackupData?.restoreUsing(environment)
}
override fun isEnabled(): Boolean {
return preferences.isCrustEnabled
}
override fun backupNow() {
if (isEnabled()) backup()
}
private fun backup() {
scope.launch {
updateProgress()
crustApi.uploadBackup(backupService.backupFilePath)
updateProgress(25)
}
}
companion object {
@Volatile
private var instance: Crust? = null
fun getInstance(
backupService: BackupService,
preferences: BackupPreferencesRepository,
crustApi: CrustDataSource
): Crust = instance ?: Crust(backupService, preferences, crustApi)
}
}
\ No newline at end of file
package io.xxlabs.messenger.backup.cloud.crust
import bindings.Bindings
import bindings.UserDiscovery
interface CrustDataSource {
suspend fun uploadBackup(path: String): ByteArray
suspend fun recoverBackup(username: String): Result<ByteArray>
}
class BindingsCrustMediator(
var udManager: UserDiscovery? = null,
var receptionRsaPrivateKey: ByteArray = byteArrayOf()
) : CrustDataSource {
override suspend fun uploadBackup(path: String): ByteArray {
return udManager?.let {
Bindings.uploadBackup(path, udManager, receptionRsaPrivateKey)
} ?: byteArrayOf()
}
override suspend fun recoverBackup(username: String): Result<ByteArray> {
return try {
val backupData = Bindings.recoverBackup(username)
Result.success(backupData)
} catch (e: Exception) {
Result.failure(e)
}
}
}
\ No newline at end of file
package io.xxlabs.messenger.backup.data package io.xxlabs.messenger.backup.data
import io.xxlabs.messenger.backup.bindings.BackupService import io.xxlabs.messenger.backup.bindings.BackupService
import io.xxlabs.messenger.backup.cloud.crust.BindingsCrustMediator
import io.xxlabs.messenger.backup.cloud.crust.Crust
import io.xxlabs.messenger.backup.cloud.drive.GoogleDrive import io.xxlabs.messenger.backup.cloud.drive.GoogleDrive
import io.xxlabs.messenger.backup.cloud.dropbox.Dropbox import io.xxlabs.messenger.backup.cloud.dropbox.Dropbox
import io.xxlabs.messenger.backup.cloud.sftp.transfer.Sftp import io.xxlabs.messenger.backup.cloud.sftp.transfer.Sftp
...@@ -12,14 +14,18 @@ abstract class BackupLocationRepository( ...@@ -12,14 +14,18 @@ abstract class BackupLocationRepository(
backupService: BackupService, backupService: BackupService,
) : AccountBackupDataSource { ) : AccountBackupDataSource {
private val crustApi = BindingsCrustMediator()
protected val googleDrive = GoogleDrive.getInstance(backupService, preferences) protected val googleDrive = GoogleDrive.getInstance(backupService, preferences)
protected val dropbox = Dropbox.getInstance(backupService, preferences) protected val dropbox = Dropbox.getInstance(backupService, preferences)
protected val sftp = Sftp.getInstance(backupService, preferences) protected val sftp = Sftp.getInstance(backupService, preferences)
protected val crust = Crust.getInstance(backupService, preferences, crustApi)
override val locations: List<AccountBackup> = listOf( override val locations: List<AccountBackup> = listOf(
googleDrive, googleDrive,
dropbox, dropbox,
sftp sftp,
crust
) )
override fun getBackupFrom(source: BackupSource): AccountBackup = override fun getBackupFrom(source: BackupSource): AccountBackup =
...@@ -27,6 +33,7 @@ abstract class BackupLocationRepository( ...@@ -27,6 +33,7 @@ abstract class BackupLocationRepository(
BackupSource.DRIVE -> googleDrive BackupSource.DRIVE -> googleDrive
BackupSource.DROPBOX -> dropbox BackupSource.DROPBOX -> dropbox
BackupSource.SFTP -> sftp BackupSource.SFTP -> sftp
BackupSource.CRUST -> crust
} }
override fun getSourceFor(backup: AccountBackup): BackupSource? = override fun getSourceFor(backup: AccountBackup): BackupSource? =
...@@ -34,6 +41,7 @@ abstract class BackupLocationRepository( ...@@ -34,6 +41,7 @@ abstract class BackupLocationRepository(
is GoogleDrive -> BackupSource.DRIVE is GoogleDrive -> BackupSource.DRIVE
is Dropbox -> BackupSource.DROPBOX is Dropbox -> BackupSource.DROPBOX
is Sftp -> BackupSource.SFTP is Sftp -> BackupSource.SFTP
is Crust -> BackupSource.CRUST
else -> null else -> null
} }
} }
\ No newline at end of file
...@@ -2,4 +2,4 @@ package io.xxlabs.messenger.backup.data ...@@ -2,4 +2,4 @@ package io.xxlabs.messenger.backup.data
import java.io.Serializable import java.io.Serializable
enum class BackupSource : Serializable { DRIVE, DROPBOX, SFTP } enum class BackupSource : Serializable { DRIVE, DROPBOX, SFTP, CRUST }
\ No newline at end of file \ No newline at end of file
...@@ -24,6 +24,7 @@ class BackupMediator @Inject constructor( ...@@ -24,6 +24,7 @@ class BackupMediator @Inject constructor(
googleDrive.location.name -> googleDrive googleDrive.location.name -> googleDrive
dropbox.location.name -> dropbox dropbox.location.name -> dropbox
sftp.location.name -> sftp sftp.location.name -> sftp
crust.location.name -> crust
else -> null else -> null
} }
......
...@@ -5,6 +5,7 @@ interface BackupPreferencesRepository { ...@@ -5,6 +5,7 @@ interface BackupPreferencesRepository {
var isGoogleDriveEnabled: Boolean var isGoogleDriveEnabled: Boolean
var isDropboxEnabled: Boolean var isDropboxEnabled: Boolean
var isSftpEnabled: Boolean var isSftpEnabled: Boolean
var isCrustEnabled: Boolean
var backupPassword: String? var backupPassword: String?
var autoBackup: Boolean var autoBackup: Boolean
var wiFiOnlyBackup: Boolean var wiFiOnlyBackup: Boolean
......
...@@ -245,6 +245,13 @@ class PreferencesRepository @Inject constructor( ...@@ -245,6 +245,13 @@ class PreferencesRepository @Inject constructor(
preferences.edit().putBoolean("sftp_enabled", value).apply() preferences.edit().putBoolean("sftp_enabled", value).apply()
} }
override var isCrustEnabled: Boolean = preferences.getBoolean("crust_enabled", false)
get() = preferences.getBoolean("crust_enabled", false)
set(value) {
field = value
preferences.edit().putBoolean("crust_enabled", value).apply()
}
override var backupPassword: String? = preferences.getString("backup_pw", null) override var backupPassword: String? = preferences.getString("backup_pw", null)
get() = preferences.getString("backup_pw", null) get() = preferences.getString("backup_pw", null)
set(value) { set(value) {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment