@@ -20,6 +20,7 @@ import okhttp3.logging.HttpLoggingInterceptor
20
20
import java.io.BufferedInputStream
21
21
import java.io.BufferedReader
22
22
import java.io.File
23
+ import java.io.RandomAccessFile
23
24
import java.io.IOException
24
25
import java.security.SecureRandom
25
26
import java.security.cert.X509Certificate
@@ -299,40 +300,32 @@ class Client @JvmOverloads constructor(
299
300
)
300
301
}
301
302
302
- val input = file.inputStream().buffered( )
303
+ val input = RandomAccessFile(file, "r" )
303
304
val buffer = ByteArray(CHUNK_SIZE)
304
305
var offset = 0L
305
306
var result: Map< *, *>? = null
306
307
307
308
if (idParamName?.isNotEmpty() == true && params[idParamName] != "unique()") {
308
309
// Make a request to check if a file already exists
309
- try {
310
- val current = call(
311
- method = "GET",
312
- path = "$path/$params[idParamName]",
313
- headers = headers,
314
- params = emptyMap(),
315
- responseType = Map::class.java,
316
- )
317
- val chunksUploaded = current["chunksUploaded"] as Long
318
- offset = size.coerceAtMost(chunksUploaded * CHUNK_SIZE)
319
- } catch (ex: {{spec .title | caseUcfirst }}Exception) {
320
- }
310
+ val current = call(
311
+ method = "GET",
312
+ path = "$path/${params[idParamName]}",
313
+ headers = headers,
314
+ params = emptyMap(),
315
+ responseType = Map::class.java,
316
+ )
317
+ val chunksUploaded = current["chunksUploaded"] as Long
318
+ offset = (chunksUploaded * CHUNK_SIZE).coerceAtMost(size)
321
319
}
322
320
323
- generateSequence {
324
- val readBytes = input.read(buffer, offset.toInt(), CHUNK_SIZE)
325
- if (readBytes >= 0) {
326
- buffer.copyOf(readBytes)
327
- } else {
328
- input.close()
329
- null
330
- }
331
- }.forEach {
321
+ while (offset < size) {
322
+ input.seek(offset)
323
+ input.read(buffer)
324
+
332
325
params[paramName] = MultipartBody.Part.createFormData(
333
326
paramName,
334
327
file.name,
335
- it .toRequestBody()
328
+ buffer .toRequestBody()
336
329
)
337
330
338
331
headers["Content-Range"] =
@@ -347,14 +340,16 @@ class Client @JvmOverloads constructor(
347
340
)
348
341
349
342
offset += CHUNK_SIZE
350
- headers["x-{{ spec .title | caseLower }}-id"] = result!!["\$id"].toString()
351
- onProgress?.invoke(UploadProgress(
352
- id = result!!["\$id"].toString(),
353
- progress = offset.coerceAtMost(size).toDouble()/size * 100,
354
- sizeUploaded = offset.coerceAtMost(size),
355
- chunksTotal = result!!["chunkTotal"].toString().toInt(),
356
- chunksUploaded = result!!["chunksUploaded"].toString().toInt(),
357
- ))
343
+ headers["x-appwrite-id"] = result!!["\$id"].toString()
344
+ onProgress?.invoke(
345
+ UploadProgress(
346
+ id = result!!["\$id"].toString(),
347
+ progress = offset.coerceAtMost(size).toDouble() / size * 100,
348
+ sizeUploaded = offset.coerceAtMost(size),
349
+ chunksTotal = result!!["chunksTotal"].toString().toInt(),
350
+ chunksUploaded = result!!["chunksUploaded"].toString().toInt(),
351
+ )
352
+ )
358
353
}
359
354
360
355
return converter(result as Map<String , Any >)
0 commit comments