@@ -20,7 +20,7 @@ open class Client {
2020
2121 open var headers : [ String : String ] = [
2222 " content-type " : " " ,
23- " x-sdk-version " : " appwrite:swiftclient:0.3.1 " ,
23+ " x-sdk-version " : " appwrite:swiftclient:0.4.0 " ,
2424 " X-Appwrite-Response-Format " : " 0.13.0 "
2525 ]
2626
@@ -249,7 +249,7 @@ open class Client {
249249 headers: [ String : String ] = [ : ] ,
250250 params: [ String : Any ? ] = [ : ] ,
251251 sink: ( ( ByteBuffer ) -> Void ) ? = nil ,
252- convert : ( ( [ String : Any ] ) -> T ) ? = nil
252+ converter : ( ( [ String : Any ] ) -> T ) ? = nil
253253 ) async throws -> T {
254254 let validParams = params. filter { $0. value != nil }
255255
@@ -268,12 +268,12 @@ open class Client {
268268 request. addDomainCookies ( )
269269
270270 if " GET " == method {
271- return try await execute ( request, convert : convert )
271+ return try await execute ( request, converter : converter )
272272 }
273273
274274 try buildBody ( for: & request, with: validParams)
275275
276- return try await execute ( request, withSink: sink, convert : convert )
276+ return try await execute ( request, withSink: sink, converter : converter )
277277 }
278278
279279 private func buildBody(
@@ -290,7 +290,7 @@ open class Client {
290290 private func execute< T> (
291291 _ request: HTTPClientRequest ,
292292 withSink bufferSink: ( ( ByteBuffer ) -> Void ) ? = nil ,
293- convert : ( ( [ String : Any ] ) -> T ) ? = nil
293+ converter : ( ( [ String : Any ] ) -> T ) ? = nil
294294 ) async throws -> T {
295295 func complete( with response: HTTPClientResponse ) async throws -> T {
296296 switch response. status. code {
@@ -310,7 +310,7 @@ open class Client {
310310 let data = try await response. body. collect ( upTo: Int . max)
311311 let dict = try JSONSerialization . jsonObject ( with: data) as? [ String : Any ]
312312
313- return convert ? ( dict!) ?? dict! as! T
313+ return converter ? ( dict!) ?? dict! as! T
314314 }
315315 default :
316316 var message = " "
@@ -351,7 +351,8 @@ open class Client {
351351 headers: inout [ String : String ] ,
352352 params: inout [ String : Any ? ] ,
353353 paramName: String ,
354- convert: ( ( [ String : Any ] ) -> T ) ? = nil ,
354+ idParamName: String ? = nil ,
355+ converter: ( ( [ String : Any ] ) -> T ) ? = nil ,
355356 onProgress: ( ( UploadProgress ) -> Void ) ? = nil
356357 ) async throws -> T {
357358 let file = params [ paramName] as! File
@@ -363,18 +364,31 @@ open class Client {
363364 path: path,
364365 headers: headers,
365366 params: params,
366- convert : convert
367+ converter : converter
367368 )
368369 }
369370
370- var input = file. buffer
371+ let input = file. buffer
371372 var offset = 0
372373 var result = [ String: Any] ( )
373374
374- while offset < size {
375- let slice = input. readSlice ( length: Client . chunkSize)
376- ?? input. readSlice ( length: Int ( size - offset) )
375+ if idParamName != nil && params [ idParamName!] as! String != " unique() " {
376+ // Make a request to check if a file already exists
377+ let map = try ! await call (
378+ method: " GET " ,
379+ path: path + " / " + ( params [ idParamName!] as! String ) ,
380+ headers: headers,
381+ params: [ : ] ,
382+ converter: { return $0 }
383+ )
384+ let chunksUploaded = map [ " chunksUploaded " ] as! Int
385+ offset = min ( size, ( chunksUploaded * Client. chunkSize) )
386+ }
377387
388+ while offset < size {
389+ let slice = input. getSlice ( at: offset, length: Client . chunkSize)
390+ ?? input. getSlice ( at: offset, length: Int ( size - offset) )
391+
378392 params [ paramName] = File (
379393 name: file. name,
380394 buffer: slice!
@@ -387,7 +401,7 @@ open class Client {
387401 path: path,
388402 headers: headers,
389403 params: params,
390- convert : { return $0 }
404+ converter : { return $0 }
391405 )
392406
393407 offset += Client . chunkSize
@@ -401,7 +415,7 @@ open class Client {
401415 ) )
402416 }
403417
404- return convert !( result)
418+ return converter !( result)
405419 }
406420
407421 private static func randomBoundary( ) -> String {
0 commit comments