Skip to content

Commit 1fc682c

Browse files
committed
Fix random access read
1 parent 23e351a commit 1fc682c

File tree

1 file changed

+26
-31
lines changed

1 file changed

+26
-31
lines changed

templates/kotlin/src/main/kotlin/io/appwrite/Client.kt.twig

Lines changed: 26 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import okhttp3.logging.HttpLoggingInterceptor
2020
import java.io.BufferedInputStream
2121
import java.io.BufferedReader
2222
import java.io.File
23+
import java.io.RandomAccessFile
2324
import java.io.IOException
2425
import java.security.SecureRandom
2526
import java.security.cert.X509Certificate
@@ -299,40 +300,32 @@ class Client @JvmOverloads constructor(
299300
)
300301
}
301302

302-
val input = file.inputStream().buffered()
303+
val input = RandomAccessFile(file, "r")
303304
val buffer = ByteArray(CHUNK_SIZE)
304305
var offset = 0L
305306
var result: Map<*, *>? = null
306307

307308
if (idParamName?.isNotEmpty() == true && params[idParamName] != "unique()") {
308309
// 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)
321319
}
322320

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+
332325
params[paramName] = MultipartBody.Part.createFormData(
333326
paramName,
334327
file.name,
335-
it.toRequestBody()
328+
buffer.toRequestBody()
336329
)
337330

338331
headers["Content-Range"] =
@@ -347,14 +340,16 @@ class Client @JvmOverloads constructor(
347340
)
348341

349342
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+
)
358353
}
359354

360355
return converter(result as Map<String, Any>)

0 commit comments

Comments
 (0)