Skip to content

Commit

Permalink
resumable upload support
Browse files Browse the repository at this point in the history
  • Loading branch information
lohanidamodar committed Apr 13, 2022
1 parent 5ceecf1 commit 77d6edb
Show file tree
Hide file tree
Showing 9 changed files with 125 additions and 393 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ Add the package to your `Package.swift` dependencies:

```swift
dependencies: [
.package(url: "[email protected]:appwrite/sdk-for-apple.git", from: "0.3.1"),
.package(url: "[email protected]:appwrite/sdk-for-apple.git", from: "0.4.0"),
],
```

Expand Down
42 changes: 28 additions & 14 deletions Sources/Appwrite/Client.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ open class Client {

open var headers: [String: String] = [
"content-type": "",
"x-sdk-version": "appwrite:swiftclient:0.3.1",
"x-sdk-version": "appwrite:swiftclient:0.4.0",
"X-Appwrite-Response-Format": "0.13.0"
]

Expand Down Expand Up @@ -249,7 +249,7 @@ open class Client {
headers: [String: String] = [:],
params: [String: Any?] = [:],
sink: ((ByteBuffer) -> Void)? = nil,
convert: (([String: Any]) -> T)? = nil
converter: (([String: Any]) -> T)? = nil
) async throws -> T {
let validParams = params.filter { $0.value != nil }

Expand All @@ -268,12 +268,12 @@ open class Client {
request.addDomainCookies()

if "GET" == method {
return try await execute(request, convert: convert)
return try await execute(request, converter: converter)
}

try buildBody(for: &request, with: validParams)

return try await execute(request, withSink: sink, convert: convert)
return try await execute(request, withSink: sink, converter: converter)
}

private func buildBody(
Expand All @@ -290,7 +290,7 @@ open class Client {
private func execute<T>(
_ request: HTTPClientRequest,
withSink bufferSink: ((ByteBuffer) -> Void)? = nil,
convert: (([String: Any]) -> T)? = nil
converter: (([String: Any]) -> T)? = nil
) async throws -> T {
func complete(with response: HTTPClientResponse) async throws -> T {
switch response.status.code {
Expand All @@ -310,7 +310,7 @@ open class Client {
let data = try await response.body.collect(upTo: Int.max)
let dict = try JSONSerialization.jsonObject(with: data) as? [String: Any]

return convert?(dict!) ?? dict! as! T
return converter?(dict!) ?? dict! as! T
}
default:
var message = ""
Expand Down Expand Up @@ -351,7 +351,8 @@ open class Client {
headers: inout [String: String],
params: inout [String: Any?],
paramName: String,
convert: (([String: Any]) -> T)? = nil,
idParamName: String? = nil,
converter: (([String: Any]) -> T)? = nil,
onProgress: ((UploadProgress) -> Void)? = nil
) async throws -> T {
let file = params[paramName] as! File
Expand All @@ -363,18 +364,31 @@ open class Client {
path: path,
headers: headers,
params: params,
convert: convert
converter: converter
)
}

var input = file.buffer
let input = file.buffer
var offset = 0
var result = [String:Any]()

while offset < size {
let slice = input.readSlice(length: Client.chunkSize)
?? input.readSlice(length: Int(size - offset))
if idParamName != nil && params[idParamName!] as! String != "unique()" {
// Make a request to check if a file already exists
let map = try! await call(
method: "GET",
path: path + "/" + (params[idParamName!] as! String),
headers: headers,
params: [:],
converter: { return $0 }
)
let chunksUploaded = map["chunksUploaded"] as! Int
offset = min(size, (chunksUploaded * Client.chunkSize))
}

while offset < size {
let slice = input.getSlice(at: offset, length: Client.chunkSize)
?? input.getSlice(at: offset, length: Int(size - offset))

params[paramName] = File(
name: file.name,
buffer: slice!
Expand All @@ -387,7 +401,7 @@ open class Client {
path: path,
headers: headers,
params: params,
convert: { return $0 }
converter: { return $0 }
)

offset += Client.chunkSize
Expand All @@ -401,7 +415,7 @@ open class Client {
))
}

return convert!(result)
return converter!(result)
}

private static func randomBoundary() -> String {
Expand Down
Loading

0 comments on commit 77d6edb

Please sign in to comment.