Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationEventAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
6
Help us understand the problem. What are the problem?

More than 5 years have passed since last update.

@nkmrtkhd

SwiftyDropboxで150MB以上のファイルをUPLOADする

公式はサンプルを載せろ、と言いたい。

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")
            }
        })

    }

}
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
6
Help us understand the problem. What are the problem?