公式はサンプルを載せろ、と言いたい。
func uploadLargeFile(srcPath a_sSrcPath:String, dstPath a_sDstPath:String){
if let client=Dropbox.authorizedClient{
let inputStrm = NSInputStream(fileAtPath: a_sSrcPath)
inputStrm!.open()
let sendBlockByte = 1024 * 1024 * 50//50MB毎で
var transferedCnt:UInt64 = 0
var buffer = Array<UInt8>(count:sendBlockByte, repeatedValue:0)
var data = NSData.init(bytesNoCopy: &buffer, length:sendBlockByte, freeWhenDone:false)
var sessionID = ""
var retryCnt = 0
var appendNextBlock:() -> Void = { () -> Void in }
appendNextBlock = { () in
let readCnt = inputStrm!.read(&buffer, maxLength: sendBlockByte)
if (readCnt > 0) {
if (readCnt != sendBlockByte) {
data = NSData.init(bytesNoCopy: &buffer, length:(readCnt >= 0 ? readCnt: 0), freeWhenDone:false)
}
var l_fncCompletion:((Void)?, CallError<(Files.UploadSessionLookupError)>?) -> Void = { (_, _) -> Void in }
l_fncCompletion = { (response, error) in
debugPrint(response)
if error != nil {
// Lets Rety
retryCnt++
if (retryCnt < 4) {
client.files.uploadSessionAppend(sessionId:sessionID, offset:transferedCnt, body:data).response(l_fncCompletion)
}else{
debugPrint("ERROR")
}
}else{
transferedCnt = transferedCnt + UInt64(readCnt)
retryCnt = 0
appendNextBlock()
}
}
client.files.uploadSessionAppend(sessionId:sessionID, offset:transferedCnt, body:data).response(l_fncCompletion)
}else{
//全ブロック送信できたとき、finishする
if (transferedCnt > 0) {
let sessionCursor = Files.UploadSessionCursor(sessionId: sessionID, offset: transferedCnt)
let commitInfo = Files.CommitInfo(path: a_sDstPath, mode: .Overwrite, autorename: false, clientModified: nil, mute: false)
client.files.uploadSessionFinish(cursor: sessionCursor, commit: commitInfo, body: NSData()).response( { (response, error) in
if let _ = response {
debugPrint("FINISH")
}else{
debugPrint("ERROR")
}
})
}else{
debugPrint("ERROR")
}
}
}
//空のNSDataで始める
let tmp = NSData()
client.files.uploadSessionStart(body:tmp).response( {
(response, errorType) -> Void in
if let (startResult) = response {
sessionID = startResult.sessionId
appendNextBlock()
}else{
debugPrint("ERROR")
}
})
}
}