From 8f9a98043107d4e8ab1465e2daed98997c442c39 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafa=C5=82=C2=A0Koby=C5=82ko?= Date: Sun, 1 Oct 2023 14:42:57 +0200 Subject: [PATCH] Fix Google Drive wipe files --- .../data/cloud/googledrive/GoogleDrive.kt | 2 +- .../data/cloud/googledrive/GoogleDriveImpl.kt | 16 ++++++++++------ .../data/services/remote/WipeGoogleDriveWork.kt | 2 +- 3 files changed, 12 insertions(+), 8 deletions(-) diff --git a/data/cloud/src/main/java/com/twofasapp/data/cloud/googledrive/GoogleDrive.kt b/data/cloud/src/main/java/com/twofasapp/data/cloud/googledrive/GoogleDrive.kt index 6776d4f4..32f4a921 100644 --- a/data/cloud/src/main/java/com/twofasapp/data/cloud/googledrive/GoogleDrive.kt +++ b/data/cloud/src/main/java/com/twofasapp/data/cloud/googledrive/GoogleDrive.kt @@ -3,5 +3,5 @@ package com.twofasapp.data.cloud.googledrive interface GoogleDrive { suspend fun getBackupFile(): GoogleDriveFileResult suspend fun updateBackupFile(backupContent: String): GoogleDriveResult - suspend fun deleteBackupFile(): GoogleDriveResult + suspend fun deleteBackupFiles(): GoogleDriveResult } \ No newline at end of file diff --git a/data/cloud/src/main/java/com/twofasapp/data/cloud/googledrive/GoogleDriveImpl.kt b/data/cloud/src/main/java/com/twofasapp/data/cloud/googledrive/GoogleDriveImpl.kt index 47b40e24..4a4061b8 100644 --- a/data/cloud/src/main/java/com/twofasapp/data/cloud/googledrive/GoogleDriveImpl.kt +++ b/data/cloud/src/main/java/com/twofasapp/data/cloud/googledrive/GoogleDriveImpl.kt @@ -118,7 +118,7 @@ internal class GoogleDriveImpl( } } - override suspend fun deleteBackupFile(): GoogleDriveResult { + override suspend fun deleteBackupFiles(): GoogleDriveResult { return withContext(dispatchers.io) { try { Timber.d("DeleteFile -> Starting...") @@ -127,9 +127,12 @@ internal class GoogleDriveImpl( ?: return@withContext GoogleDriveResult.Failure(error = GoogleDriveError.CredentialsNotFound) val drive = getDrive(credentials) - val backupFileId = getFiles(drive)?.firstOrNull { it.name == backupVersions.first() }?.id - if (backupFileId.isNullOrBlank().not()) { - drive.files().delete(backupFileId).execute() + + getFiles(drive)?.forEach { file -> + if (backupVersions.contains(file.name)) { + Timber.d("DeleteFile -> ${file.name}") + drive.files().delete(file.id).execute() + } } Timber.d("DeleteFile <- Success") @@ -154,12 +157,13 @@ internal class GoogleDriveImpl( } } - private fun getFiles(drive: Drive) = - drive.files().list() + private fun getFiles(drive: Drive): List? { + return drive.files().list() .setSpaces("appDataFolder") .execute() ?.files ?.sortedByDescending { it.name.replace("2fas-backup-v", "").replace(".json", "").toIntOrNull() } + } private fun getDrive(accountCredentials: AccountCredentials): Drive { val googleAccountCredential = GoogleAccountCredential diff --git a/data/services/src/main/java/com/twofasapp/data/services/remote/WipeGoogleDriveWork.kt b/data/services/src/main/java/com/twofasapp/data/services/remote/WipeGoogleDriveWork.kt index cfcf4bf6..3ee200c9 100644 --- a/data/services/src/main/java/com/twofasapp/data/services/remote/WipeGoogleDriveWork.kt +++ b/data/services/src/main/java/com/twofasapp/data/services/remote/WipeGoogleDriveWork.kt @@ -17,7 +17,7 @@ class WipeGoogleDriveWork( private val googleAuth: GoogleAuth by inject() override suspend fun doWork(): Result { - googleDrive.deleteBackupFile() + googleDrive.deleteBackupFiles() googleAuth.revokeAccess() return Result.success()