@@ -5,6 +5,7 @@ import com.google.gson.reflect.TypeToken
55import {{ sdk .namespace | caseDot }}.exceptions.{{ spec .title | caseUcfirst }}Exception
66import {{ sdk .namespace | caseDot }}.extensions.fromJson
77import {{ sdk .namespace | caseDot }}.json.PreciseNumberAdapter
8+ import {{ sdk .namespace | caseDot }}.models.InputFile
89import {{ sdk .namespace | caseDot }}.models.UploadProgress
910import kotlinx.coroutines.CoroutineScope
1011import kotlinx.coroutines.Dispatchers
@@ -281,14 +282,29 @@ class Client @JvmOverloads constructor(
281282 idParamName: String? = null,
282283 onProgress: ((UploadProgress) -> Unit)? = null,
283284 ): T {
284- val file = params[paramName] as File
285- val size = file.length()
285+ var file: RandomAccessFile? = null
286+ val input = params[paramName] as InputFile
287+ val size: Long = when(input.sourceType) {
288+ "path", "file" -> {
289+ file = RandomAccessFile(input.path, "r")
290+ file.length()
291+ }
292+ "bytes" -> {
293+ (input.data as ByteArray).size.toLong()
294+ }
295+ else -> throw UnsupportedOperationException()
296+ }
286297
287298 if (size < CHUNK_SIZE) {
299+ val data = when(input.sourceType) {
300+ "file", "path" -> File(input.path).asRequestBody()
301+ "bytes" -> (input.data as ByteArray).toRequestBody(input.mimeType.toMediaType())
302+ else -> throw UnsupportedOperationException()
303+ }
288304 params[paramName] = MultipartBody.Part.createFormData(
289305 paramName,
290- file.name ,
291- file.asRequestBody()
306+ input.filename ,
307+ data
292308 )
293309 return call(
294310 method = "POST",
@@ -300,7 +316,6 @@ class Client @JvmOverloads constructor(
300316 )
301317 }
302318
303- val input = RandomAccessFile(file, "r")
304319 val buffer = ByteArray(CHUNK_SIZE)
305320 var offset = 0L
306321 var result: Map< *, *>? = null
@@ -319,12 +334,29 @@ class Client @JvmOverloads constructor(
319334 }
320335
321336 while (offset < size) {
322- input.seek(offset)
323- input.read(buffer)
337+ when(input.sourceType) {
338+ "file", "path" -> {
339+ file!!.seek(offset)
340+ file!!.read(buffer)
341+ }
342+ "bytes" -> {
343+ val end = if (offset + CHUNK_SIZE < size) {
344+ offset + CHUNK_SIZE
345+ } else {
346+ size - 1
347+ }
348+ (input.data as ByteArray).copyInto(
349+ buffer,
350+ startIndex = offset.toInt(),
351+ endIndex = end.toInt()
352+ )
353+ }
354+ else -> throw UnsupportedOperationException()
355+ }
324356
325357 params[paramName] = MultipartBody.Part.createFormData(
326358 paramName,
327- file.name ,
359+ input.filename ,
328360 buffer.toRequestBody()
329361 )
330362
0 commit comments