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 {
suspend fun getBackupFile(): GoogleDriveFileResult
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) {
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<File>? {
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

View File

@ -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()