Fix Google Drive wipe files

This commit is contained in:
Rafał Kobyłko 2023-10-01 14:42:57 +02:00
parent 993a83db02
commit 8f9a980431
3 changed files with 12 additions and 8 deletions

View File

@ -3,5 +3,5 @@ package com.twofasapp.data.cloud.googledrive
interface GoogleDrive { interface GoogleDrive {
suspend fun getBackupFile(): GoogleDriveFileResult suspend fun getBackupFile(): GoogleDriveFileResult
suspend fun updateBackupFile(backupContent: String): GoogleDriveResult suspend fun updateBackupFile(backupContent: String): GoogleDriveResult
suspend fun deleteBackupFile(): GoogleDriveResult suspend fun deleteBackupFiles(): GoogleDriveResult
} }

View File

@ -118,7 +118,7 @@ internal class GoogleDriveImpl(
} }
} }
override suspend fun deleteBackupFile(): GoogleDriveResult { override suspend fun deleteBackupFiles(): GoogleDriveResult {
return withContext(dispatchers.io) { return withContext(dispatchers.io) {
try { try {
Timber.d("DeleteFile -> Starting...") Timber.d("DeleteFile -> Starting...")
@ -127,9 +127,12 @@ internal class GoogleDriveImpl(
?: return@withContext GoogleDriveResult.Failure(error = GoogleDriveError.CredentialsNotFound) ?: return@withContext GoogleDriveResult.Failure(error = GoogleDriveError.CredentialsNotFound)
val drive = getDrive(credentials) val drive = getDrive(credentials)
val backupFileId = getFiles(drive)?.firstOrNull { it.name == backupVersions.first() }?.id
if (backupFileId.isNullOrBlank().not()) { getFiles(drive)?.forEach { file ->
drive.files().delete(backupFileId).execute() if (backupVersions.contains(file.name)) {
Timber.d("DeleteFile -> ${file.name}")
drive.files().delete(file.id).execute()
}
} }
Timber.d("DeleteFile <- Success") Timber.d("DeleteFile <- Success")
@ -154,12 +157,13 @@ internal class GoogleDriveImpl(
} }
} }
private fun getFiles(drive: Drive) = private fun getFiles(drive: Drive): List<File>? {
drive.files().list() return drive.files().list()
.setSpaces("appDataFolder") .setSpaces("appDataFolder")
.execute() .execute()
?.files ?.files
?.sortedByDescending { it.name.replace("2fas-backup-v", "").replace(".json", "").toIntOrNull() } ?.sortedByDescending { it.name.replace("2fas-backup-v", "").replace(".json", "").toIntOrNull() }
}
private fun getDrive(accountCredentials: AccountCredentials): Drive { private fun getDrive(accountCredentials: AccountCredentials): Drive {
val googleAccountCredential = GoogleAccountCredential val googleAccountCredential = GoogleAccountCredential

View File

@ -17,7 +17,7 @@ class WipeGoogleDriveWork(
private val googleAuth: GoogleAuth by inject() private val googleAuth: GoogleAuth by inject()
override suspend fun doWork(): Result { override suspend fun doWork(): Result {
googleDrive.deleteBackupFile() googleDrive.deleteBackupFiles()
googleAuth.revokeAccess() googleAuth.revokeAccess()
return Result.success() return Result.success()