function _uploadChunk(fileName, filePath, oAuth2TwoLegged, credentials, progressCallback) {
return new Promise((resolve, reject) => {
let objectApi = new ForgeSDK.ObjectsApi()
let fileState = fs.statSync(filePath)
let fileSize = fileState.size
let chunkSize = 2 * 1024 * 1024
let nbChunks = Math.round(0.5 + fileSize / chunkSize)
let sessionID = 'testviotv4054irt'// + Math.floor(Date.now() / 1000)
for (let index = 0; index < nbChunks; index++) {
let start = index * chunkSize
let end = Math.min(fileSize, (index + 1) * chunkSize) - 1
let contentRange = 'bytes ' + start + '-' + end + '/' + fileSize
let readStream = fs.createReadStream(filePath, { start: start, end: end })
objectApi.uploadChunk(config.defaultBucketKey, fileName, fileSize, contentRange, sessionID, readStream, {}, oAuth2TwoLegged, credentials)
.then(object => {
if (object.statusCode == 202) {
progressCallback(object.body)
} else if (object.statusCode == 202) {
resolve(object.body)
}
}).catch(err => {
resolve(err)
})
}
})
}
First of all, your code uploadChunk uses fileSize, which is the size of the entire file, not the size of a certain chunk. In addition, there needs to be a status of object.statusCode == 200, which is the status after the last block is successful.
I am not very familiar with ES6, so I wrote a function according to regular JS. Based on the test script in Forge Node.js SDK, I can replace the uploadFile in line 155 for testing.