Skip to content

Commit

Permalink
refactor: add more error guard for gdrive.
Browse files Browse the repository at this point in the history
Also changed it to be app specific, we don't want them to use sync data from SY or other forks as some of the model and backup is different. So if people using other forks they should use the same data and not mismatch.
  • Loading branch information
kaiserbh committed Jan 25, 2024
1 parent 8680565 commit 624d65d
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -400,12 +400,19 @@ object SettingsDataScreen : SearchableSettings {
when (result) {
GoogleDriveSyncService.DeleteSyncDataStatus.NOT_INITIALIZED -> context.toast(
MR.strings.google_drive_not_signed_in,
duration = 5000,
)
GoogleDriveSyncService.DeleteSyncDataStatus.NO_FILES -> context.toast(
MR.strings.google_drive_sync_data_not_found,
duration = 5000,
)
GoogleDriveSyncService.DeleteSyncDataStatus.SUCCESS -> context.toast(
MR.strings.google_drive_sync_data_purged,
duration = 5000,
)
GoogleDriveSyncService.DeleteSyncDataStatus.ERROR -> context.toast(
MR.strings.google_drive_sync_data_purge_error,
duration = 10000,
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,18 +56,22 @@ class GoogleDriveSyncService(context: Context, json: Json, syncPreferences: Sync
NOT_INITIALIZED,
NO_FILES,
SUCCESS,
ERROR,
}

private val remoteFileName = "tachiyomi_sync_data.gz"
private val appName = context.stringResource(MR.strings.app_name)

private val lockFileName = "tachiyomi_sync.lock"
private val remoteFileName = "${appName}_sync_data.gz"

private val lockFileName = "${appName}_sync.lock"

private val googleDriveService = GoogleDriveService(context)

override suspend fun beforeSync() {
try {
googleDriveService.refreshToken()
val drive = googleDriveService.driveService ?: throw Exception("Google Drive service not initialized")
val drive = googleDriveService.driveService
?: throw Exception(context.stringResource(MR.strings.google_drive_not_signed_in))

var backoff = 1000L

Expand Down Expand Up @@ -105,6 +109,7 @@ class GoogleDriveSyncService(context: Context, json: Json, syncPreferences: Sync
}
} catch (e: Exception) {
logcat(LogPriority.ERROR) { "Error in GoogleDrive beforeSync: ${e.message}" }
throw Exception(context.stringResource(MR.strings.error_before_sync_gdrive) + ": ${e.message}")
}
}

Expand Down Expand Up @@ -234,6 +239,7 @@ class GoogleDriveSyncService(context: Context, json: Json, syncPreferences: Sync
Log.d("GoogleDrive", "Created lock file with ID: ${file.id}")
} catch (e: Exception) {
Log.e("GoogleDrive", "Error creating lock file: ${e.message}")
throw Exception(e.message)
}
}

Expand Down Expand Up @@ -282,19 +288,24 @@ class GoogleDriveSyncService(context: Context, json: Json, syncPreferences: Sync
googleDriveService.refreshToken()

return withContext(Dispatchers.IO) {
val appDataFileList = getAppDataFileList(drive)

if (appDataFileList.isEmpty()) {
logcat(LogPriority.DEBUG) { "No sync data file found in appData folder of Google Drive" }
DeleteSyncDataStatus.NO_FILES
} else {
for (file in appDataFileList) {
drive.files().delete(file.id).execute()
logcat(
LogPriority.DEBUG,
) { "Deleted sync data file in appData folder of Google Drive with file ID: ${file.id}" }
try {
val appDataFileList = getAppDataFileList(drive)

if (appDataFileList.isEmpty()) {
logcat(LogPriority.DEBUG) { "No sync data file found in appData folder of Google Drive" }
DeleteSyncDataStatus.NO_FILES
} else {
for (file in appDataFileList) {
drive.files().delete(file.id).execute()
logcat(
LogPriority.DEBUG,
) { "Deleted sync data file in appData folder of Google Drive with file ID: ${file.id}" }
}
DeleteSyncDataStatus.SUCCESS
}
DeleteSyncDataStatus.SUCCESS
} catch (e: Exception) {
logcat(LogPriority.ERROR) { "Error occurred while interacting with Google Drive: ${e.message}" }
DeleteSyncDataStatus.ERROR
}
}
}
Expand Down Expand Up @@ -441,7 +452,7 @@ class GoogleDriveService(private val context: Context) {
NetHttpTransport(),
jsonFactory,
credential,
).setApplicationName("Tachiyomi")
).setApplicationName(context.stringResource(MR.strings.app_name))
.build()
}

Expand Down
2 changes: 2 additions & 0 deletions i18n/src/commonMain/resources/MR/base/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -577,11 +577,13 @@
<string name="pref_google_drive_purge_sync_data">Clear Sync Data from Google Drive</string>
<string name="google_drive_sync_data_purged">Sync data purged from Google Drive</string>
<string name="google_drive_sync_data_not_found">No sync data found in Google Drive</string>
<string name="google_drive_sync_data_purge_error">Error purging sync data from Google Drive, Try to sign in again.</string>
<string name="google_drive_login_success">Logged in to Google Drive</string>
<string name="google_drive_login_failed">Failed to log in to Google Drive: %s</string>
<string name="google_drive_not_signed_in">Not signed in to Google Drive</string>
<string name="error_uploading_sync_data">Error uploading sync data to Google Drive</string>
<string name="error_deleting_google_drive_lock_file">Error Deleting Google Drive Lock File</string>
<string name="error_before_sync_gdrive">Error before sync: %s</string>
<string name="pref_purge_confirmation_title">Purge confirmation</string>
<string name="pref_purge_confirmation_message">Purging sync data will delete all your sync data from Google Drive. Are you sure you want to continue?</string>
<string name="pref_sync_options">Create sync triggers</string>
Expand Down

0 comments on commit 624d65d

Please sign in to comment.