Fix handling Google Drive 404 errors

pull/1244/head
Alex Baker 5 years ago
parent d74f041ef6
commit 7bfd339021

@ -9,6 +9,7 @@ import com.google.api.services.drive.Drive
import com.google.api.services.drive.model.File import com.google.api.services.drive.model.File
import com.todoroo.astrid.backup.BackupConstants import com.todoroo.astrid.backup.BackupConstants
import com.todoroo.astrid.gtasks.api.HttpCredentialsAdapter import com.todoroo.astrid.gtasks.api.HttpCredentialsAdapter
import com.todoroo.astrid.gtasks.api.HttpNotFoundException
import dagger.hilt.android.qualifiers.ApplicationContext import dagger.hilt.android.qualifiers.ApplicationContext
import org.tasks.DebugNetworkInterceptor import org.tasks.DebugNetworkInterceptor
import org.tasks.files.FileHelper import org.tasks.files.FileHelper
@ -28,13 +29,18 @@ class DriveInvoker(
.build() .build()
@Throws(IOException::class) @Throws(IOException::class)
suspend fun getFile(folderId: String?): File? { suspend fun getFile(folderId: String?): File? = try {
return execute(service.files()[folderId].setFields("id, trashed")) execute(service.files()[folderId].setFields("id, trashed"))
} catch (ignored: HttpNotFoundException) {
null
} }
@Throws(IOException::class) @Throws(IOException::class)
suspend fun delete(file: File) { suspend fun delete(file: File) {
execute(service.files().delete(file.id)) try {
execute(service.files().delete(file.id))
} catch (ignored: HttpNotFoundException) {
}
} }
@Throws(IOException::class) @Throws(IOException::class)

@ -48,17 +48,7 @@ class DriveUploader @WorkerInject constructor(
drive drive
.getFilesByPrefix(folder.id, "auto.") .getFilesByPrefix(folder.id, "auto.")
.drop(BackupWork.DAYS_TO_KEEP_BACKUP) .drop(BackupWork.DAYS_TO_KEEP_BACKUP)
.forEach { .forEach { drive.delete(it) }
try {
drive.delete(it)
} catch (e: GoogleJsonResponseException) {
if (e.statusCode == 404) {
Timber.e(e)
} else {
throw e
}
}
}
} }
Result.success() Result.success()
} catch (e: SocketTimeoutException) { } catch (e: SocketTimeoutException) {
@ -102,13 +92,7 @@ class DriveUploader @WorkerInject constructor(
val folderId = preferences.getStringValue(R.string.p_google_drive_backup_folder) val folderId = preferences.getStringValue(R.string.p_google_drive_backup_folder)
var file: File? = null var file: File? = null
if (!isNullOrEmpty(folderId)) { if (!isNullOrEmpty(folderId)) {
try { file = drive.getFile(folderId)
file = drive.getFile(folderId)
} catch (e: GoogleJsonResponseException) {
if (e.statusCode != 404) {
throw e
}
}
} }
return if (file == null || file.trashed) drive.createFolder(FOLDER_NAME) else file return if (file == null || file.trashed) drive.createFolder(FOLDER_NAME) else file
} }

Loading…
Cancel
Save