@@ -20,7 +20,7 @@ open class Client {
2020
2121 open var headers : [ String : String ] = [
2222 " content-type " : " " ,
23- " x-sdk-version " : " appwrite:swift:0.3.1 " ,
23+ " x-sdk-version " : " appwrite:swift:0.4.0 " ,
2424 " X-Appwrite-Response-Format " : " 0.13.0 "
2525 ]
2626
@@ -264,7 +264,7 @@ open class Client {
264264 headers: [ String : String ] = [ : ] ,
265265 params: [ String : Any ? ] = [ : ] ,
266266 sink: ( ( ByteBuffer ) -> Void ) ? = nil ,
267- convert : ( ( [ String : Any ] ) -> T ) ? = nil
267+ converter : ( ( [ String : Any ] ) -> T ) ? = nil
268268 ) async throws -> T {
269269 let validParams = params. filter { $0. value != nil }
270270
@@ -283,12 +283,12 @@ open class Client {
283283 request. addDomainCookies ( )
284284
285285 if " GET " == method {
286- return try await execute ( request, convert : convert )
286+ return try await execute ( request, converter : converter )
287287 }
288288
289289 try buildBody ( for: & request, with: validParams)
290290
291- return try await execute ( request, withSink: sink, convert : convert )
291+ return try await execute ( request, withSink: sink, converter : converter )
292292 }
293293
294294 private func buildBody(
@@ -305,7 +305,7 @@ open class Client {
305305 private func execute< T> (
306306 _ request: HTTPClientRequest ,
307307 withSink bufferSink: ( ( ByteBuffer ) -> Void ) ? = nil ,
308- convert : ( ( [ String : Any ] ) -> T ) ? = nil
308+ converter : ( ( [ String : Any ] ) -> T ) ? = nil
309309 ) async throws -> T {
310310 func complete( with response: HTTPClientResponse ) async throws -> T {
311311 switch response. status. code {
@@ -325,7 +325,7 @@ open class Client {
325325 let data = try await response. body. collect ( upTo: Int . max)
326326 let dict = try JSONSerialization . jsonObject ( with: data) as? [ String : Any ]
327327
328- return convert ? ( dict!) ?? dict! as! T
328+ return converter ? ( dict!) ?? dict! as! T
329329 }
330330 default :
331331 var message = " "
@@ -366,7 +366,8 @@ open class Client {
366366 headers: inout [ String : String ] ,
367367 params: inout [ String : Any ? ] ,
368368 paramName: String ,
369- convert: ( ( [ String : Any ] ) -> T ) ? = nil ,
369+ idParamName: String ? = nil ,
370+ converter: ( ( [ String : Any ] ) -> T ) ? = nil ,
370371 onProgress: ( ( UploadProgress ) -> Void ) ? = nil
371372 ) async throws -> T {
372373 let file = params [ paramName] as! File
@@ -378,18 +379,31 @@ open class Client {
378379 path: path,
379380 headers: headers,
380381 params: params,
381- convert : convert
382+ converter : converter
382383 )
383384 }
384385
385- var input = file. buffer
386+ let input = file. buffer
386387 var offset = 0
387388 var result = [ String: Any] ( )
388389
389- while offset < size {
390- let slice = input. readSlice ( length: Client . chunkSize)
391- ?? input. readSlice ( length: Int ( size - offset) )
390+ if idParamName != nil && params [ idParamName!] as! String != " unique() " {
391+ // Make a request to check if a file already exists
392+ let map = try ! await call (
393+ method: " GET " ,
394+ path: path + " / " + ( params [ idParamName!] as! String ) ,
395+ headers: headers,
396+ params: [ : ] ,
397+ converter: { return $0 }
398+ )
399+ let chunksUploaded = map [ " chunksUploaded " ] as! Int
400+ offset = min ( size, ( chunksUploaded * Client. chunkSize) )
401+ }
392402
403+ while offset < size {
404+ let slice = input. getSlice ( at: offset, length: Client . chunkSize)
405+ ?? input. getSlice ( at: offset, length: Int ( size - offset) )
406+
393407 params [ paramName] = File (
394408 name: file. name,
395409 buffer: slice!
@@ -402,7 +416,7 @@ open class Client {
402416 path: path,
403417 headers: headers,
404418 params: params,
405- convert : { return $0 }
419+ converter : { return $0 }
406420 )
407421
408422 offset += Client . chunkSize
@@ -416,7 +430,7 @@ open class Client {
416430 ) )
417431 }
418432
419- return convert !( result)
433+ return converter !( result)
420434 }
421435
422436 private static func randomBoundary( ) -> String {
0 commit comments