-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathClient.kt
More file actions
762 lines (684 loc) Β· 24.7 KB
/
Copy pathClient.kt
File metadata and controls
762 lines (684 loc) Β· 24.7 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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
package io.appwrite
import android.content.Context
import android.content.Intent
import android.content.pm.PackageManager
import io.appwrite.cookies.ListenableCookieJar
import io.appwrite.cookies.stores.SharedPreferencesCookieStore
import io.appwrite.exceptions.AppwriteException
import io.appwrite.extensions.fromJson
import io.appwrite.extensions.toJson
import io.appwrite.models.InputFile
import io.appwrite.models.UploadProgress
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.async
import kotlinx.coroutines.awaitAll
import kotlinx.coroutines.coroutineScope
import kotlinx.coroutines.suspendCancellableCoroutine
import okhttp3.*
import okhttp3.Headers.Companion.toHeaders
import okhttp3.HttpUrl.Companion.toHttpUrl
import okhttp3.MediaType.Companion.toMediaType
import okhttp3.RequestBody.Companion.asRequestBody
import okhttp3.RequestBody.Companion.toRequestBody
import java.io.BufferedInputStream
import java.io.BufferedReader
import java.io.File
import java.io.RandomAccessFile
import java.io.IOException
import java.lang.IllegalArgumentException
import java.net.CookieManager
import java.net.CookiePolicy
import java.security.SecureRandom
import java.security.cert.X509Certificate
import java.util.concurrent.atomic.AtomicInteger
import java.util.concurrent.atomic.AtomicLong
import javax.net.ssl.SSLContext
import javax.net.ssl.SSLSocketFactory
import javax.net.ssl.TrustManager
import javax.net.ssl.X509TrustManager
import kotlin.coroutines.CoroutineContext
import kotlin.coroutines.resume
class Client @JvmOverloads constructor(
context: Context,
var endpoint: String = "https://cloud.appwrite.io/v1",
var endpointRealtime: String? = null,
private var selfSigned: Boolean = false
) : CoroutineScope {
companion object {
/**
* The size for chunked uploads in bytes.
*/
internal const val CHUNK_SIZE = 5*1024*1024; // 5MB
internal const val MAX_CONCURRENT_UPLOADS = 8
internal const val GLOBAL_PREFS = "io.appwrite"
internal const val COOKIE_PREFS = "myCookie"
}
override val coroutineContext: CoroutineContext
get() = Dispatchers.Main + job
private val job = Job()
internal lateinit var http: OkHttpClient
internal val headers: MutableMap<String, String>
val config: MutableMap<String, String>
internal val cookieJar = ListenableCookieJar(CookieManager(
SharedPreferencesCookieStore(context.getSharedPreferences(COOKIE_PREFS, Context.MODE_PRIVATE)),
CookiePolicy.ACCEPT_ALL
))
private val appVersion by lazy {
try {
val pInfo = context.packageManager.getPackageInfo(context.packageName, 0)
return@lazy pInfo.versionName
} catch (e: PackageManager.NameNotFoundException) {
e.printStackTrace()
return@lazy ""
}
}
init {
headers = mutableMapOf(
"content-type" to "application/json",
"origin" to "appwrite-android://${context.packageName}",
"user-agent" to "${context.packageName}/${appVersion}, ${System.getProperty("http.agent")}",
"x-sdk-name" to "Android",
"x-sdk-platform" to "client",
"x-sdk-language" to "android",
"x-sdk-version" to "24.2.0",
"x-appwrite-response-format" to "1.9.5"
)
config = mutableMapOf()
setSelfSigned(selfSigned)
}
/**
* Set Project
*
* Your project ID
*
* @param {string} project
*
* @return this
*/
fun setProject(value: String): Client {
config["project"] = value
addHeader("x-appwrite-project", value)
return this
}
/**
* Set JWT
*
* Your secret JSON Web Token
*
* @param {string} jwt
*
* @return this
*/
fun setJWT(value: String): Client {
config["jWT"] = value
addHeader("x-appwrite-jwt", value)
return this
}
/**
* Set Locale
*
* @param {string} locale
*
* @return this
*/
fun setLocale(value: String): Client {
config["locale"] = value
addHeader("x-appwrite-locale", value)
return this
}
/**
* Set Session
*
* The user session to authenticate with
*
* @param {string} session
*
* @return this
*/
fun setSession(value: String): Client {
config["session"] = value
addHeader("x-appwrite-session", value)
return this
}
/**
* Set DevKey
*
* Your secret dev API key
*
* @param {string} devkey
*
* @return this
*/
fun setDevKey(value: String): Client {
config["devKey"] = value
addHeader("x-appwrite-dev-key", value)
return this
}
/**
* Set Cookie
*
* The user cookie to authenticate with. Used by SDKs that forward an incoming Cookie header in server-side runtimes.
*
* @param {string} cookie
*
* @return this
*/
fun setCookie(value: String): Client {
config["cookie"] = value
addHeader("cookie", value)
return this
}
/**
* Set ImpersonateUserId
*
* Impersonate a user by ID on an already user-authenticated request. Requires the current request to be authenticated as a user with impersonator capability; X-Appwrite-Key alone is not sufficient. Impersonator users are intentionally granted users.read so they can discover a target before impersonation begins. Internal audit logs still attribute actions to the original impersonator and record the impersonated target only in internal audit payload data.
*
* @param {string} impersonateuserid
*
* @return this
*/
fun setImpersonateUserId(value: String): Client {
config["impersonateUserId"] = value
addHeader("x-appwrite-impersonate-user-id", value)
return this
}
/**
* Set ImpersonateUserEmail
*
* Impersonate a user by email on an already user-authenticated request. Requires the current request to be authenticated as a user with impersonator capability; X-Appwrite-Key alone is not sufficient. Impersonator users are intentionally granted users.read so they can discover a target before impersonation begins. Internal audit logs still attribute actions to the original impersonator and record the impersonated target only in internal audit payload data.
*
* @param {string} impersonateuseremail
*
* @return this
*/
fun setImpersonateUserEmail(value: String): Client {
config["impersonateUserEmail"] = value
addHeader("x-appwrite-impersonate-user-email", value)
return this
}
/**
* Set ImpersonateUserPhone
*
* Impersonate a user by phone on an already user-authenticated request. Requires the current request to be authenticated as a user with impersonator capability; X-Appwrite-Key alone is not sufficient. Impersonator users are intentionally granted users.read so they can discover a target before impersonation begins. Internal audit logs still attribute actions to the original impersonator and record the impersonated target only in internal audit payload data.
*
* @param {string} impersonateuserphone
*
* @return this
*/
fun setImpersonateUserPhone(value: String): Client {
config["impersonateUserPhone"] = value
addHeader("x-appwrite-impersonate-user-phone", value)
return this
}
/**
* Set self Signed
*
* @param status
*
* @return this
*/
fun setSelfSigned(status: Boolean): Client {
selfSigned = status
val builder = OkHttpClient()
.newBuilder()
.cookieJar(cookieJar)
if (!selfSigned) {
http = builder.build()
return this
}
try {
// Create a trust manager that does not validate certificate chains
val trustAllCerts = arrayOf<TrustManager>(
@Suppress("CustomX509TrustManager")
object : X509TrustManager {
@Suppress("TrustAllX509TrustManager")
override fun checkClientTrusted(chain: Array<X509Certificate>, authType: String) {
}
@Suppress("TrustAllX509TrustManager")
override fun checkServerTrusted(chain: Array<X509Certificate>, authType: String) {
}
override fun getAcceptedIssuers(): Array<X509Certificate> {
return arrayOf()
}
}
)
// Install the all-trusting trust manager
val sslContext = SSLContext.getInstance("SSL")
sslContext.init(null, trustAllCerts, SecureRandom())
// Create an ssl socket factory with our all-trusting manager
val sslSocketFactory: SSLSocketFactory = sslContext.socketFactory
builder.sslSocketFactory(sslSocketFactory, trustAllCerts[0] as X509TrustManager)
builder.hostnameVerifier { _, _ -> true }
http = builder.build()
} catch (e: Exception) {
throw RuntimeException(e)
}
return this
}
/**
* Set endpoint and realtime endpoint.
*
* @param endpoint
*
* @return this
*/
@Throws(IllegalArgumentException::class)
fun setEndpoint(endpoint: String): Client {
require(endpoint.startsWith("http://") || endpoint.startsWith("https://")) {
"Invalid endpoint URL: $endpoint"
}
this.endpoint = endpoint
this.endpointRealtime = endpoint.replaceFirst("http", "ws")
return this
}
/**
* Set realtime endpoint
*
* @param endpoint
*
* @return this
*/
@Throws(IllegalArgumentException::class)
fun setEndpointRealtime(endpoint: String): Client {
require(endpoint.startsWith("ws://") || endpoint.startsWith("wss://")) {
"Invalid realtime endpoint URL: $endpoint"
}
this.endpointRealtime = endpoint
return this
}
/**
* Add Header
*
* @param key
* @param value
*
* @return this
*/
fun addHeader(key: String, value: String): Client {
headers[key] = value
return this
}
/**
* Get the current request headers used for Appwrite API calls.
*
* @return a copy of the current request headers
*/
fun getHeaders(): Map<String, String> = headers.toMap()
/**
* Get the cookies for a given URL from the SDK's cookie store.
*
* @param url the URL to retrieve cookies for
* @return a list of cookies for the given URL
*/
fun getCookies(url: String): List<Cookie> = cookieJar.loadForRequest(url.toHttpUrl())
/**
* Get the OkHttpClient instance used by this SDK.
*
* @return the OkHttpClient instance used by this client
*/
fun getHttpClient(): OkHttpClient = http
/**
* Sends a "ping" request to Appwrite to verify connectivity.
*
* @return String
*/
suspend fun ping(): String {
val apiPath = "/ping"
val apiParams = mutableMapOf<String, Any?>()
val apiHeaders = mutableMapOf("content-type" to "application/json")
return call(
"GET",
apiPath,
apiHeaders,
apiParams,
responseType = String::class.java
)
}
/**
* Send the HTTP request
*
* @param method
* @param path
* @param headers
* @param params
*
* @return [T]
*/
@Throws(AppwriteException::class)
suspend fun <T> call(
method: String,
path: String,
headers: Map<String, String> = mapOf(),
params: Map<String, Any?> = mapOf(),
responseType: Class<T>,
converter: ((Any) -> T)? = null
): T {
val filteredParams = params.filterValues { it != null }
val requestHeaders = this.headers.toHeaders().newBuilder()
.addAll(headers.toHeaders())
.build()
val httpBuilder = (endpoint + path).toHttpUrl().newBuilder()
if ("GET" == method) {
filteredParams.forEach {
when (it.value) {
null -> {
return@forEach
}
is List<*> -> {
val list = it.value as List<*>
for (index in list.indices) {
httpBuilder.addQueryParameter(
"${it.key}[]",
list[index].toString()
)
}
}
else -> {
httpBuilder.addQueryParameter(it.key, it.value.toString())
}
}
}
val request = Request.Builder()
.url(httpBuilder.build())
.headers(requestHeaders)
.get()
.build()
return awaitResponse(request, responseType, converter)
}
val body = if (MultipartBody.FORM.toString() == headers["content-type"]) {
val builder = MultipartBody.Builder().setType(MultipartBody.FORM)
filteredParams.forEach {
when {
it.key == "file" -> {
builder.addPart(it.value as MultipartBody.Part)
}
it.value is List<*> -> {
val list = it.value as List<*>
for (index in list.indices) {
builder.addFormDataPart(
"${it.key}[]",
list[index].toString()
)
}
}
else -> {
builder.addFormDataPart(it.key, it.value.toString())
}
}
}
builder.build()
} else {
filteredParams
.toJson()
.toRequestBody("application/json".toMediaType())
}
val request = Request.Builder()
.url(httpBuilder.build())
.headers(requestHeaders)
.method(method, body)
.build()
return awaitResponse(request, responseType, converter)
}
/**
* Upload a file in chunks
*
* @param path
* @param headers
* @param params
*
* @return [T]
*/
@Throws(AppwriteException::class)
suspend fun <T> chunkedUpload(
path: String,
headers: MutableMap<String, String>,
params: MutableMap<String, Any?>,
responseType: Class<T>,
converter: ((Any) -> T),
paramName: String,
idParamName: String? = null,
onProgress: ((UploadProgress) -> Unit)? = null,
): T {
val input = params[paramName] as InputFile
val size: Long = when(input.sourceType) {
"path", "file" -> {
File(input.path).length()
}
"bytes" -> {
(input.data as ByteArray).size.toLong()
}
else -> throw UnsupportedOperationException()
}
if (size < CHUNK_SIZE) {
val data = when(input.sourceType) {
"file", "path" -> File(input.path).asRequestBody()
"bytes" -> (input.data as ByteArray).toRequestBody(input.mimeType.toMediaType())
else -> throw UnsupportedOperationException()
}
params[paramName] = MultipartBody.Part.createFormData(
paramName,
input.filename,
data
)
return call(
method = "POST",
path,
headers,
params,
responseType,
converter
)
}
var offset = 0L
var result: Map<*, *>? = null
var uploadId: String? = null
if (idParamName?.isNotEmpty() == true) {
// Make a request to check if a file already exists
val current = call(
method = "GET",
path = "$path/${params[idParamName]}",
headers = headers,
params = emptyMap(),
responseType = Map::class.java,
)
val chunksUploaded = current["chunksUploaded"] as Long
offset = chunksUploaded * CHUNK_SIZE
uploadId = params[idParamName]?.toString()
result = current
}
fun readChunk(start: Long, end: Long): ByteArray {
val length = (end - start).toInt()
return when(input.sourceType) {
"file", "path" -> {
RandomAccessFile(input.path, "r").use { chunkFile ->
val chunk = ByteArray(length)
chunkFile.seek(start)
chunkFile.readFully(chunk)
chunk
}
}
"bytes" -> {
(input.data as ByteArray).copyOfRange(start.toInt(), end.toInt())
}
else -> throw UnsupportedOperationException()
}
}
val totalChunks = (size + CHUNK_SIZE - 1) / CHUNK_SIZE
fun isUploadComplete(chunkResult: Map<*, *>): Boolean {
val chunksUploaded = chunkResult["chunksUploaded"]?.toString()?.toLongOrNull() ?: return false
val chunksTotal = chunkResult["chunksTotal"]?.toString()?.toLongOrNull() ?: totalChunks
return chunksUploaded >= chunksTotal
}
suspend fun uploadChunk(index: Int, start: Long, end: Long, includeUploadId: Boolean): Map<*, *> {
val chunkParams = params.toMutableMap()
val chunkHeaders = headers.toMutableMap()
if (includeUploadId && uploadId != null) {
chunkHeaders["x-appwrite-id"] = uploadId!!
}
chunkHeaders["Content-Range"] = "bytes $start-${end - 1}/$size"
chunkParams[paramName] = MultipartBody.Part.createFormData(
paramName,
input.filename,
readChunk(start, end).toRequestBody()
)
val chunkResult = call(
method = "POST",
path,
chunkHeaders,
chunkParams,
responseType = Map::class.java
)
if (index == 0 || uploadId == null) {
uploadId = chunkResult["\$id"].toString()
}
return chunkResult
}
if (offset == 0L) {
val firstChunkEnd = CHUNK_SIZE.toLong().coerceAtMost(size)
result = uploadChunk(0, 0, firstChunkEnd, false)
offset = firstChunkEnd
onProgress?.invoke(
UploadProgress(
id = uploadId ?: result!!["\$id"].toString(),
progress = offset.coerceAtMost(size).toDouble() / size * 100,
sizeUploaded = offset.coerceAtMost(size),
chunksTotal = result!!["chunksTotal"].toString().toInt(),
chunksUploaded = result!!["chunksUploaded"].toString().toInt(),
)
)
}
val chunks = mutableListOf<Triple<Int, Long, Long>>()
var chunkOffset = offset
while (chunkOffset < size) {
val end = (chunkOffset + CHUNK_SIZE).coerceAtMost(size)
chunks.add(Triple((chunkOffset / CHUNK_SIZE).toInt(), chunkOffset, end))
chunkOffset = end
}
if (chunks.isNotEmpty()) {
val nextChunk = AtomicInteger(0)
val completedChunks = AtomicInteger((offset / CHUNK_SIZE).toInt())
val uploadedBytes = AtomicLong(offset.coerceAtMost(size))
coroutineScope {
List(MAX_CONCURRENT_UPLOADS.coerceAtMost(chunks.size)) {
async {
while (true) {
val chunkIndex = nextChunk.getAndIncrement()
if (chunkIndex >= chunks.size) {
break
}
val (index, start, end) = chunks[chunkIndex]
val chunkResult = uploadChunk(index, start, end, true)
val chunksUploaded = completedChunks.incrementAndGet()
val sizeUploaded = uploadedBytes.addAndGet(end - start)
if (isUploadComplete(chunkResult)) {
result = chunkResult
}
onProgress?.invoke(
UploadProgress(
id = uploadId ?: chunkResult["\$id"].toString(),
progress = sizeUploaded.coerceAtMost(size).toDouble() / size * 100,
sizeUploaded = sizeUploaded.coerceAtMost(size),
chunksTotal = chunkResult["chunksTotal"].toString().toInt(),
chunksUploaded = chunksUploaded,
)
)
}
}
}.awaitAll()
}
}
return converter(result as Map<String, Any>)
}
/**
* Await Response
*
* @param request
* @param responseType
* @param converter
*
* @return [T]
*/
@Throws(AppwriteException::class)
private suspend fun <T> awaitResponse(
request: Request,
responseType: Class<T>,
converter: ((Any) -> T)? = null
) = suspendCancellableCoroutine<T> {
http.newCall(request).enqueue(object : Callback {
override fun onFailure(call: Call, e: IOException) {
if (it.isCancelled) {
return
}
it.cancel(e)
}
@Suppress("UNCHECKED_CAST")
override fun onResponse(call: Call, response: Response) {
if (!response.isSuccessful) {
val body = response.body!!
.charStream()
.buffered()
.use(BufferedReader::readText)
val error = if (response.headers["content-type"]?.contains("application/json") == true) {
val map = body.fromJson<Map<String, Any>>()
AppwriteException(
map["message"] as? String ?: "",
(map["code"] as Number).toInt(),
map["type"] as? String ?: "",
body
)
} else {
AppwriteException(body, response.code, "", body)
}
it.cancel(error)
return
}
val warnings = response.headers["x-appwrite-warning"]
if (warnings != null) {
warnings.split(";").forEach { warning ->
System.err.println("Warning: $warning")
}
}
when {
responseType == Boolean::class.java -> {
it.resume(true as T)
return
}
responseType == String::class.java -> {
val body = response.body!!
.charStream()
.buffered()
.use(BufferedReader::readText)
it.resume(body as T)
return
}
responseType == ByteArray::class.java -> {
it.resume(response.body!!
.byteStream()
.buffered()
.use(BufferedInputStream::readBytes) as T
)
return
}
response.body == null -> {
it.resume(true as T)
return
}
}
val body = response.body!!
.charStream()
.buffered()
.use(BufferedReader::readText)
if (body.isEmpty()) {
it.resume(true as T)
return
}
val map = body.fromJson<Any>()
it.resume(
converter?.invoke(map) ?: map as T
)
}
})
}
}