Merge pull request #5 from undefiened/sync-part-final

Added exceptions in Google Drive sync process so that it fails correctly and moved the redirect url to a constant
This commit is contained in:
KaiserBh 2023-08-28 00:29:02 +10:00 committed by GitHub
commit 30af850211
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

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