Skip to content

Commit

Permalink
Merge pull request mihonapp#5 from undefiened/sync-part-final
Browse files Browse the repository at this point in the history
Added exceptions in Google Drive sync process so that it fails correctly and moved the redirect url to a constant
  • Loading branch information
kaiserbh authored Aug 27, 2023
2 parents d229010 + 2289fb9 commit 30af850
Showing 1 changed file with 14 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -151,6 +152,9 @@ class GoogleDriveSyncService(context: Context, json: Json, syncPreferences: Sync

class GoogleDriveService(private val context: Context) {
var googleDriveService: Drive? = null
companion object {
const val REDIRECT_URI = "eu.kanade.google.oauth:/oauth2redirect"
}
private val syncPreferences = Injekt.get<SyncPreferences>()

init {
Expand Down Expand Up @@ -208,7 +212,7 @@ class GoogleDriveService(private val context: Context) {
).setAccessType("offline").build()

return flow.newAuthorizationUrl()
.setRedirectUri("eu.kanade.google.oauth:/oauth2redirect")
.setRedirectUri(REDIRECT_URI)
.setApprovalPrompt("force")
.build()
}
Expand All @@ -225,6 +229,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()}" }
Expand All @@ -246,11 +254,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")
}
}

Expand Down Expand Up @@ -305,7 +315,7 @@ class GoogleDriveService(private val context: Context) {
secrets.installed.clientId,
secrets.installed.clientSecret,
authorizationCode,
"eu.kanade.google.oauth:/oauth2redirect",
REDIRECT_URI,
).setGrantType("authorization_code").execute()

try {
Expand Down

0 comments on commit 30af850

Please sign in to comment.