-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGoogleDriveClient.kt
More file actions
256 lines (222 loc) · 9.77 KB
/
GoogleDriveClient.kt
File metadata and controls
256 lines (222 loc) · 9.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
package io.github.smiling_pixel.client
import android.content.Context
import com.google.android.gms.auth.api.signin.GoogleSignIn
import com.google.android.gms.auth.api.signin.GoogleSignInOptions
import com.google.android.gms.common.api.ApiException
import com.google.android.gms.common.api.Scope
import com.google.api.client.googleapis.extensions.android.gms.auth.GoogleAccountCredential
import com.google.api.client.extensions.android.http.AndroidHttp
import com.google.api.client.http.ByteArrayContent
import com.google.api.client.json.gson.GsonFactory
import com.google.api.services.drive.Drive
import com.google.api.services.drive.DriveScopes
import com.google.api.services.drive.model.File
import io.github.smiling_pixel.util.Logger
import io.github.smiling_pixel.util.e
import io.github.smiling_pixel.preference.AndroidContextProvider
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import kotlinx.coroutines.sync.Mutex
import kotlinx.coroutines.sync.withLock
import java.io.ByteArrayOutputStream
import java.util.Collections
class GoogleDriveClient : CloudDriveClient {
/**
* Retrieves the Android Application Context.
*
* This property accesses [AndroidContextProvider.context]. Ensure that [AndroidContextProvider.context]
* is initialized (typically in `MainActivity.onCreate`) before any methods of this client are called.
*
* @throws IllegalStateException if [AndroidContextProvider.context] has not been initialized yet.
*/
private val context: Context
get() = try {
AndroidContextProvider.context
} catch (e: UninitializedPropertyAccessException) {
throw IllegalStateException(
"AndroidContextProvider.context is not initialized. " +
"Ensure it is set before using GoogleDriveClient.",
e
)
}
private val jsonFactory = GsonFactory.getDefaultInstance()
private val appName = "MarkDay Diary"
private val serviceMutex = Mutex()
@Volatile
private var driveService: Drive? = null
private fun getService(): Drive {
return driveService ?: throw IllegalStateException("Google Drive not authorized")
}
// Checking auth state and initializing service if possible
private suspend fun checkAndInitService(): Boolean {
if (driveService != null) return true
return serviceMutex.withLock {
if (driveService != null) return@withLock true
val account = GoogleSignIn.getLastSignedInAccount(context)
val driveScope = Scope(DriveScopes.DRIVE_FILE)
if (account != null && GoogleSignIn.hasPermissions(account, driveScope)) {
val email = account.email
if (email != null) {
initService(email)
return@withLock true
}
}
false
}
}
private fun initService(email: String) {
val credential = GoogleAccountCredential.usingOAuth2(
context, Collections.singleton(DriveScopes.DRIVE_FILE)
)
credential.selectedAccountName = email
driveService = Drive.Builder(
AndroidHttp.newCompatibleTransport(),
jsonFactory,
credential
).setApplicationName(appName).build()
}
override suspend fun isAuthorized(): Boolean = withContext(Dispatchers.IO) {
checkAndInitService()
}
override suspend fun authorize(): Boolean {
// First, try to initialize the service on IO without switching to Main unnecessarily
if (withContext(Dispatchers.IO) { checkAndInitService() }) return true
// If not yet authorized, perform the sign-in flow on the main thread
return withContext(Dispatchers.Main) {
val driveScope = Scope(DriveScopes.DRIVE_FILE)
val gso = GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestEmail()
.requestScopes(driveScope)
.build()
val client = GoogleSignIn.getClient(context, gso)
val signInIntent = client.signInIntent
val result = GoogleSignInHelper.launchSignIn(signInIntent)
if (result != null && result.resultCode == android.app.Activity.RESULT_OK) {
val task = GoogleSignIn.getSignedInAccountFromIntent(result.data)
try {
val account = task.getResult(ApiException::class.java)
if (account != null) {
val email = account.email
if (email != null) {
serviceMutex.withLock {
initService(email)
}
return@withContext true
}
}
} catch (e: Exception) {
Logger.e("GoogleDriveClient", "Authorization failed: ${e.message}")
e.printStackTrace()
}
}
false
}
}
override suspend fun signOut() = withContext(Dispatchers.Main) {
val gso = GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN).build()
val client = GoogleSignIn.getClient(context, gso)
val deferred = kotlinx.coroutines.CompletableDeferred<Unit>()
client.signOut().addOnCompleteListener { task ->
if (task.isSuccessful) {
driveService = null
deferred.complete(Unit)
} else {
val e = task.exception ?: RuntimeException("Sign out failed")
Logger.e("GoogleDriveClient", "Sign out failed: ${e.message}")
deferred.completeExceptionally(e)
}
}
deferred.await()
}
override suspend fun getUserInfo(): UserInfo? = withContext(Dispatchers.IO) {
if (!checkAndInitService()) return@withContext null
val account = GoogleSignIn.getLastSignedInAccount(context) ?: return@withContext null
val photoUrl = account.photoUrl?.toString()
UserInfo(
name = account.displayName ?: "",
email = account.email ?: "",
photoUrl = photoUrl
)
}
override suspend fun listFiles(parentId: String?): List<DriveFile> = withContext(Dispatchers.IO) {
val folderId = parentId ?: "root"
// Sanitize folderId to prevent injection
val sanitizedFolderId = folderId.replace("'", "\\'")
val query = "'$sanitizedFolderId' in parents and trashed = false"
val result = getService().files().list()
.setQ(query)
.setFields("nextPageToken, files(id, name, mimeType)")
.execute()
result.files?.map { file ->
DriveFile(
id = file.id,
name = file.name,
mimeType = file.mimeType,
isFolder = file.mimeType == CloudDriveClient.MIME_TYPE_FOLDER
)
} ?: emptyList()
}
override suspend fun createFile(name: String, content: ByteArray, mimeType: String, parentId: String?): DriveFile = withContext(Dispatchers.IO) {
val fileMetadata = File().apply {
this.name = name
this.mimeType = mimeType
if (parentId != null) {
this.parents = listOf(parentId)
}
}
val mediaContent = ByteArrayContent(mimeType, content)
val file = getService().files().create(fileMetadata, mediaContent)
.setFields("id, name, mimeType, parents")
.execute()
DriveFile(
id = file.id,
name = file.name,
mimeType = file.mimeType,
isFolder = file.mimeType == CloudDriveClient.MIME_TYPE_FOLDER
)
}
override suspend fun createFolder(name: String, parentId: String?): DriveFile = withContext(Dispatchers.IO) {
val fileMetadata = File().apply {
this.name = name
this.mimeType = CloudDriveClient.MIME_TYPE_FOLDER
if (parentId != null) {
this.parents = listOf(parentId)
}
}
val file = getService().files().create(fileMetadata)
.setFields("id, name, mimeType")
.execute()
DriveFile(
id = file.id,
name = file.name,
mimeType = CloudDriveClient.MIME_TYPE_FOLDER,
isFolder = true
)
}
override suspend fun deleteFile(fileId: String): Unit = withContext(Dispatchers.IO) {
getService().files().delete(fileId).execute()
}
override suspend fun downloadFile(fileId: String): ByteArray = withContext(Dispatchers.IO) {
val outputStream = ByteArrayOutputStream()
getService().files().get(fileId)
.executeMediaAndDownloadTo(outputStream)
outputStream.toByteArray()
}
override suspend fun updateFile(fileId: String, content: ByteArray): DriveFile = withContext(Dispatchers.IO) {
val fileMetadata = File()
val existingFile = getService().files().get(fileId).setFields("mimeType").execute()
val mimeType = existingFile.mimeType
val mediaContent = ByteArrayContent(mimeType, content)
val updatedFile = getService().files().update(fileId, fileMetadata, mediaContent)
.setFields("id, name, mimeType")
.execute()
DriveFile(
id = updatedFile.id,
name = updatedFile.name,
mimeType = updatedFile.mimeType,
isFolder = updatedFile.mimeType == CloudDriveClient.MIME_TYPE_FOLDER
)
}
}
private val googleDriveClientInstance by lazy { GoogleDriveClient() }
actual fun getCloudDriveClient(): CloudDriveClient = googleDriveClientInstance