We are aware of the issue with the badge emails resending to everyone, we apologise for the inconvenience - learn more here.

Forum Discussion

Michael-jamf's avatar
Michael-jamf
Helpful | Level 6
2 years ago

Better understanding of offset with appendV2 and finish

Attempting to upload large files. The regular upload method works without an issue. When I try to use appendV2 and Finish I get that the offset was incorrect and the correct offset. This post does di...
  • Michael-jamf's avatar
    Michael-jamf
    2 years ago

    That makes sense. Thanks for clearing that up. I was thinking you provide the full file, then the offset was used to tell which part to upload. I also ran into an issue when the upload would not finish before attempting the next part so I made it recursive if the file part finished.

     

    Anyone that find this. Here is my upload function for small and large files. It could be improved but it works.

     

    func upload(file filePath: String, to dbxLocation: String) {
            if FileManager.default.fileExists(atPath: filePath) {
                print("File Exists")
            } else {
                return
            }
            let fileURL: URL = URL(filePath: filePath)
            var dbFilePath: String = ""
            if dbxLocation.last == "/" {
                dbFilePath = dbxLocation + fileURL.lastPathComponent
            } else {
                dbFilePath = dbxLocation + "/" + fileURL.lastPathComponent
            }
            print(dbFilePath)
            let fileSize = try? FileManager.default.attributesOfItem(atPath: filePath)[.size] as? UInt64
            let chunkSize: UInt64 = 4 * 1024 * 1024
            //        Check to see if file is smaller than chunksize
            if fileSize! < chunkSize {
                dbxClient.files.upload(path: dbFilePath, input: fileURL).response(completionHandler: { response, error in
                    if let response = response {
                        print("File uploaded: \(response)")
                    } else {
                        print("Error upload session: \(error!)")
                    }
                })
                .progress { progressData in print(progressData) }
                print("small file")
            } else {
                let data: [Data] = try! self.split(file: fileURL, into: chunkSize)
                //start the upload session
                var offset: UInt64 = UInt64(data[0].count)
                dbxClient.files.uploadSessionStart(input: data[0])
                    .response(completionHandler: { [self] response, error in
                        if let result = response {
                            print(result)
                            append(data: data, part: 1, sessionId: result.sessionId, offset: offset, chunkSize: chunkSize, fileSize: fileSize!, dbFilePath: dbFilePath)
                        } else {
                            // the call failed
                            print(error!)
                        }
                    })
            }
        }
        
        func append(data: [Data], part: Int, sessionId: String, offset: UInt64, chunkSize: UInt64, fileSize: UInt64, dbFilePath: String) {
            let chunk = data[part]
            if ((fileSize - offset) <= chunkSize) {
                dbxClient.files.uploadSessionFinish(cursor: Files.UploadSessionCursor(sessionId: sessionId, offset: offset), commit: Files.CommitInfo(path: dbFilePath), input: chunk)
                    .response { response, error in
                        if let result = response {
                            print("File Uploaded: \(result)")
                        } else {
                            print(fileSize)
                            print("Finish Error: \(error!)")
                        }
                    }
            } else {
                dbxClient.files.uploadSessionAppendV2(cursor: Files.UploadSessionCursor(sessionId: sessionId, offset: offset), input: chunk)
                    .response {response , error in
                        if let response = response {
                            self.append(data: data, part: part + 1, sessionId: sessionId, offset: offset + UInt64(data[part].count), chunkSize: chunkSize, fileSize: fileSize, dbFilePath: dbFilePath)
                        } else {
                            print("Error appending data \(part) to file upload session: \(error!)")
                        }
                    }
            }
        }
            
        func split(file fileURL: URL, into chunkSize: UInt64) throws -> [Data] {
            let data = try Data(contentsOf: fileURL)
            let chunks = stride(from: 0, to: data.count, by: Int(chunkSize)).map {
                data.subdata(in: $0 ..< Swift.min($0 + Int(chunkSize), data.count))
            }
            return chunks
        }

About Dropbox API Support & Feedback

Node avatar for Dropbox API Support & Feedback

Find help with the Dropbox API from other developers.

5,877 PostsLatest Activity: 7 hours ago
325 Following

If you need more help you can view your support options (expected response time for an email or ticket is 24 hours), or contact us on X or Facebook.

For more info on available support options for your Dropbox plan, see this article.

If you found the answer to your question in this Community thread, please 'like' the post to say thanks and to let us know it was useful!