Created
March 23, 2016 03:16
-
-
Save smarx/4bd20487ddf37a5942bf to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
... | |
let data = String(count: 100*1024*1024, repeatedValue: Character("A")).dataUsingEncoding(NSUTF8StringEncoding)! | |
let chunkSize = 5 * 1024 * 1024 // 5MB | |
var offset = 0 | |
var sessionId = "" | |
func uploadFirstChunk() { | |
let size = min(chunkSize, data.length) | |
self.client.files.uploadSessionStart(body: | |
data.subdataWithRange(NSMakeRange(0, size))) | |
.response { response, error in | |
if let result = response { | |
self.sessionId = result.sessionId | |
self.offset += size | |
print("So far \(self.offset) bytes have been uploaded.") | |
self.uploadNextChunk() | |
} else { | |
print("uploadSessionStart failed") | |
} | |
} | |
} | |
func uploadNextChunk() { | |
if data.length - offset <= chunkSize { | |
let size = data.length - offset | |
client.files.uploadSessionFinish( | |
cursor: Files.UploadSessionCursor( | |
sessionId: self.sessionId, offset: UInt64(self.offset)), | |
commit: Files.CommitInfo(path: "/foo"), | |
body: data.subdataWithRange(NSMakeRange(self.offset, size))) | |
.response { response, error in | |
if error != nil { | |
print("uploadSessionFinish failed: \(error)") | |
} else { | |
print("Done!") | |
} | |
} | |
} else { | |
client.files.uploadSessionAppend( | |
sessionId: self.sessionId, | |
offset: UInt64(self.offset), | |
body: data.subdataWithRange(NSMakeRange(self.offset, chunkSize))) | |
.response { response, error in | |
if error != nil { | |
print("uploadSessionAppend failed: \(error)") | |
} else { | |
self.offset += self.chunkSize | |
print("So far \(self.offset) bytes have been uploaded.") | |
self.uploadNextChunk() | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment