Skip to content

Commit f24a7e2

Browse files
committed
Update android input file handling
1 parent ec238b0 commit f24a7e2

File tree

1 file changed

+40
-8
lines changed

1 file changed

+40
-8
lines changed

templates/android/library/src/main/java/io/appwrite/Client.kt.twig

Lines changed: 40 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {{ sdk.namespace | caseDot }}.cookies.stores.SharedPreferencesCookieStore
99
import {{ sdk.namespace | caseDot }}.exceptions.{{ spec.title | caseUcfirst }}Exception
1010
import {{ sdk.namespace | caseDot }}.extensions.fromJson
1111
import {{ sdk.namespace | caseDot }}.json.PreciseNumberAdapter
12+
import {{ sdk.namespace | caseDot }}.models.InputFile
1213
import {{ sdk.namespace | caseDot }}.models.UploadProgress
1314
import kotlinx.coroutines.CoroutineScope
1415
import kotlinx.coroutines.Dispatchers
@@ -322,14 +323,29 @@ class Client @JvmOverloads constructor(
322323
idParamName: String? = null,
323324
onProgress: ((UploadProgress) -> Unit)? = null,
324325
): T {
325-
val file = params[paramName] as File
326-
val size = file.length()
326+
var file: RandomAccessFile? = null
327+
val input = params[paramName] as InputFile
328+
val size: Long = when(input.sourceType) {
329+
"path", "file" -> {
330+
file = RandomAccessFile(input.path, "r")
331+
file.length()
332+
}
333+
"bytes" -> {
334+
(input.data as ByteArray).size.toLong()
335+
}
336+
else -> throw UnsupportedOperationException()
337+
}
327338

328339
if (size < CHUNK_SIZE) {
340+
val data = when(input.sourceType) {
341+
"file", "path" -> File(input.path).asRequestBody()
342+
"bytes" -> (input.data as ByteArray).toRequestBody(input.mimeType.toMediaType())
343+
else -> throw UnsupportedOperationException()
344+
}
329345
params[paramName] = MultipartBody.Part.createFormData(
330346
paramName,
331-
file.name,
332-
file.asRequestBody()
347+
input.filename,
348+
data
333349
)
334350
return call(
335351
method = "POST",
@@ -341,7 +357,6 @@ class Client @JvmOverloads constructor(
341357
)
342358
}
343359

344-
val input = RandomAccessFile(file, "r")
345360
val buffer = ByteArray(CHUNK_SIZE)
346361
var offset = 0L
347362
var result: Map<*, *>? = null
@@ -360,12 +375,29 @@ class Client @JvmOverloads constructor(
360375
}
361376

362377
while (offset < size) {
363-
input.seek(offset)
364-
input.read(buffer)
378+
when(input.sourceType) {
379+
"file", "path" -> {
380+
file!!.seek(offset)
381+
file!!.read(buffer)
382+
}
383+
"bytes" -> {
384+
val end = if (offset + CHUNK_SIZE < size) {
385+
offset + CHUNK_SIZE
386+
} else {
387+
size - 1
388+
}
389+
(input.data as ByteArray).copyInto(
390+
buffer,
391+
startIndex = offset.toInt(),
392+
endIndex = end.toInt()
393+
)
394+
}
395+
else -> throw UnsupportedOperationException()
396+
}
365397

366398
params[paramName] = MultipartBody.Part.createFormData(
367399
paramName,
368-
file.name,
400+
input.filename,
369401
buffer.toRequestBody()
370402
)
371403

0 commit comments

Comments
 (0)