@@ -184,7 +184,7 @@ class Client @JvmOverloads constructor(
184
184
headers: Map<String , String > = mapOf(),
185
185
params: Map<String , Any ?> = mapOf(),
186
186
responseType: Class<T >,
187
- convert : ((Map<String , Any ,>) -> T)? = null
187
+ converter : ((Map<String , Any ,>) -> T)? = null
188
188
): T {
189
189
val filteredParams = params.filterValues { it != null }
190
190
@@ -220,7 +220,7 @@ class Client @JvmOverloads constructor(
220
220
.get()
221
221
.build()
222
222
223
- return awaitResponse(request, responseType, convert )
223
+ return awaitResponse(request, responseType, converter )
224
224
}
225
225
226
226
val body = if (MultipartBody.FORM.toString() == headers["content-type"]) {
@@ -257,7 +257,7 @@ class Client @JvmOverloads constructor(
257
257
.method(method, body)
258
258
.build()
259
259
260
- return awaitResponse(request, responseType, convert )
260
+ return awaitResponse(request, responseType, converter )
261
261
}
262
262
263
263
/**
@@ -275,8 +275,9 @@ class Client @JvmOverloads constructor(
275
275
headers: MutableMap<String , String >,
276
276
params: MutableMap<String , Any ?>,
277
277
responseType: Class<T >,
278
- convert : ((Map<String , Any ,>) -> T),
278
+ converter : ((Map<String , Any ,>) -> T),
279
279
paramName: String,
280
+ idParamName: String? = null,
280
281
onProgress: ((UploadProgress) -> Unit)? = null,
281
282
): T {
282
283
val file = params[paramName] as File
@@ -289,12 +290,12 @@ class Client @JvmOverloads constructor(
289
290
file.asRequestBody()
290
291
)
291
292
return call(
292
- "POST",
293
+ method = "POST",
293
294
path,
294
295
headers,
295
296
params,
296
297
responseType,
297
- convert
298
+ converter
298
299
)
299
300
}
300
301
@@ -303,6 +304,22 @@ class Client @JvmOverloads constructor(
303
304
var offset = 0L
304
305
var result: Map< *, *>? = null
305
306
307
+ if (idParamName?.isNotEmpty() == true && params[idParamName] != "unique()") {
308
+ // 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
+ }
321
+ }
322
+
306
323
generateSequence {
307
324
val readBytes = input.read(buffer)
308
325
if (readBytes >= 0) {
@@ -322,11 +339,11 @@ class Client @JvmOverloads constructor(
322
339
"bytes $offset-${((offset + CHUNK_SIZE) - 1).coerceAtMost(size)}/$size"
323
340
324
341
result = call(
325
- "POST",
342
+ method = "POST",
326
343
path,
327
344
headers,
328
345
params,
329
- Map::class.java
346
+ responseType = Map::class.java
330
347
)
331
348
332
349
offset += CHUNK_SIZE
@@ -336,27 +353,27 @@ class Client @JvmOverloads constructor(
336
353
progress = offset.coerceAtMost(size).toDouble()/size * 100,
337
354
sizeUploaded = offset.coerceAtMost(size),
338
355
chunksTotal = result!!["chunkTotal"].toString().toInt(),
339
- chunksUploaded = result!!["chunkUploaded "].toString().toInt(),
356
+ chunksUploaded = result!!["chunksUploaded "].toString().toInt(),
340
357
))
341
358
}
342
359
343
- return convert (result as Map<String , Any >)
360
+ return converter (result as Map<String , Any >)
344
361
}
345
362
346
363
/**
347
364
* Await Response
348
365
*
349
366
* @param request
350
367
* @param responseType
351
- * @param convert
368
+ * @param converter
352
369
*
353
370
* @return [T]
354
371
*/
355
372
@Throws({{ spec .title | caseUcfirst }}Exception::class)
356
373
private suspend fun <T > awaitResponse(
357
374
request: Request,
358
375
responseType: Class<T >,
359
- convert : ((Map<String , Any ,>) -> T)? = null
376
+ converter : ((Map<String , Any ,>) -> T)? = null
360
377
) = suspendCancellableCoroutine<T > {
361
378
http.newCall(request).enqueue(object : Callback {
362
379
override fun onFailure(call: Call, e: IOException) {
@@ -422,7 +439,7 @@ class Client @JvmOverloads constructor(
422
439
object : TypeToken<Map <String , Any >>(){}.type
423
440
)
424
441
it.resume(
425
- convert ?.invoke(map) ?: map as T
442
+ converter ?.invoke(map) ?: map as T
426
443
)
427
444
}
428
445
})
0 commit comments