Added exceptions in Google Sync process so that it fails correctly and moved the redirect url to a constant

This commit is contained in:
undefiened 2023-08-27 00:35:37 +02:00
parent 1fcfd27977
commit 1993928bab

View File

@ -18,6 +18,7 @@ import com.google.api.client.json.jackson2.JacksonFactory
import com.google.api.services.drive.Drive
import com.google.api.services.drive.DriveScopes
import com.google.api.services.drive.model.File
import eu.kanade.tachiyomi.R
import eu.kanade.tachiyomi.data.sync.models.SyncData
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
@ -57,7 +58,7 @@ class GoogleDriveSyncService(context: Context, json: Json, syncPreferences: Sync
// Check if the Google Drive service is initialized
if (drive == null) {
logcat(LogPriority.ERROR) { "Google Drive service not initialized" }
return null
throw Exception(context.getString(R.string.google_drive_not_signed_in))
}
val fileList = getFileList(drive)
@ -85,7 +86,7 @@ class GoogleDriveSyncService(context: Context, json: Json, syncPreferences: Sync
// Check if the Google Drive service is initialized
if (drive == null) {
logcat(LogPriority.ERROR) { "Google Drive service not initialized" }
return
throw Exception(context.getString(R.string.google_drive_not_signed_in))
}
// delete file if exists
@ -151,6 +152,7 @@ class GoogleDriveSyncService(context: Context, json: Json, syncPreferences: Sync
class GoogleDriveService(private val context: Context) {
var googleDriveService: Drive? = null
private val redirectUri = "eu.kanade.google.oauth:/oauth2redirect"
private val syncPreferences = Injekt.get<SyncPreferences>()
init {
@ -208,7 +210,7 @@ class GoogleDriveService(private val context: Context) {
).setAccessType("offline").build()
return flow.newAuthorizationUrl()
.setRedirectUri("eu.kanade.google.oauth:/oauth2redirect")
.setRedirectUri(redirectUri)
.setApprovalPrompt("force")
.build()
}
@ -225,6 +227,10 @@ class GoogleDriveService(private val context: Context) {
.setClientSecrets(secrets)
.build()
if (syncPreferences.getGoogleDriveRefreshToken() == "") {
throw Exception(context.getString(R.string.google_drive_not_signed_in))
}
credential.refreshToken = syncPreferences.getGoogleDriveRefreshToken()
logcat(LogPriority.DEBUG) { "Refreshing access token with: ${syncPreferences.getGoogleDriveRefreshToken()}" }
@ -246,11 +252,13 @@ class GoogleDriveService(private val context: Context) {
// Token refresh failed; handle this situation
logcat(LogPriority.ERROR) { "Failed to refresh access token ${e.message}" }
logcat(LogPriority.ERROR) { "Google Drive sync will be disabled" }
throw e.message?.let { Exception(it) } ?: Exception("Unknown error")
}
} catch (e: IOException) {
// Token refresh failed; handle this situation
logcat(LogPriority.ERROR) { "Failed to refresh access token ${e.message}" }
logcat(LogPriority.ERROR) { "Google Drive sync will be disabled" }
throw e.message?.let { Exception(it) } ?: Exception("Unknown error")
}
}
@ -305,7 +313,7 @@ class GoogleDriveService(private val context: Context) {
secrets.installed.clientId,
secrets.installed.clientSecret,
authorizationCode,
"eu.kanade.google.oauth:/oauth2redirect",
redirectUri,
).setGrantType("authorization_code").execute()
try {