Last active
February 25, 2020 15:52
-
-
Save cnharris10/3d744ca13abd13d4d5bd3a363be16dff to your computer and use it in GitHub Desktop.
This file contains hidden or 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
import SwiftyDropbox | |
class DropboxManager { | |
var delegate: Dropboxable? | |
private var cursor: UInt64 = 0 | |
init() { | |
// Completed assumptions: | |
// - setupWithAppKey | |
// - authorizeFromController | |
} | |
func sendFile(localFileUrl: NSURL, remoteFileUrl: NSURL) { | |
cursor = 0 | |
let client = Dropbox.authorizedClient | |
guard let _ = client else { | |
print("Failed to authorize Dropbox user") | |
return | |
} | |
guard case let localFile = NSFileManager.defaultManager().fileExistsAtPath(localFileUrl.path!) where localFile == true else { | |
print("File does not exist at localPath: \(localFileUrl)") | |
return | |
} | |
guard let fileData = NSData(contentsOfFile: localFileUrl.path!) else { | |
print("File does not exist at localPath: \(localFileUrl)") | |
return | |
} | |
// Partition into 1MB chunks | |
let dataChunks = fileData.partitionBy(DataPartition.oneMB) | |
client!.files.uploadSessionStart(body: NSData()).response() { (responseData, error) in | |
if let sessionId = responseData?.sessionId { | |
self.appendChunksToFile(dataChunks, index: 0, remoteFileUrlPath: remoteFileUrl.path!, sessionId: sessionId, totalLength: UInt64(fileData.length)) | |
} | |
} | |
} | |
func appendChunksToFile(dataChunks: [(data: NSData, offset: UInt64)], | |
index: Int, | |
remoteFileUrlPath: String, | |
sessionId: String, | |
totalLength: UInt64) { | |
let client = Dropbox.authorizedClient | |
if index < dataChunks.count { | |
let chunk = dataChunks[index] | |
client!.files.uploadSessionAppend(sessionId: sessionId, offset: UInt64(chunk.offset), body: chunk.data).response({ (responseData, error) in | |
print("Cursor Offset: \(self.cursor)") | |
print("Chunk Offset: \(chunk.offset)") | |
print("Error: \(error)") | |
if error == nil { | |
// Chunk successfully uploaded, load next one | |
self.cursor += UInt64(chunk.data.length) | |
// Recurse to end | |
self.appendChunksToFile(dataChunks, index: index + 1, remoteFileUrlPath: remoteFileUrlPath, sessionId: sessionId, totalLength: totalLength) | |
print("Successfully uploaded chunk at offset: \(chunk.offset) with chunk size: \(chunk.data.length)") | |
} else { | |
// Chunk failed to upload, throw error | |
print("Failed to upload file chunk") | |
} | |
}) | |
} else { | |
let sessionCursor = Files.UploadSessionCursor(sessionId: sessionId, offset: cursor) | |
let commitInfo = Files.CommitInfo(path: remoteFileUrlPath, mode: .Overwrite, autorename: true, clientModified: nil, mute: false) | |
client!.files.uploadSessionFinish(cursor: sessionCursor, commit: commitInfo, body: NSData()).response({ (responseData, error) in | |
print("Finished Cursor Offset: \(self.cursor)") | |
print("Finished Data: \(responseData?.clientModified)") | |
print("Finished Error: \(error)") | |
if error == nil { | |
print("Successfully created file at path: \(remoteFileUrlPath)") | |
} else { | |
print("Failed to finish file upload") | |
} | |
}) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment