We Want to Hear From You! What Do You Want to See on the Community? Tell us here!

Forum Discussion

zainulabd786's avatar
zainulabd786
Explorer | Level 3
7 years ago

How to know the offset for filesUploadSessionFinishBatch()

Hi, I am trying to upload many files using the JavaScript SDK's 

filesUploadSessionFinishBatch(),
 
For that, I am looping through all the files and calling 
filesUploadSessionStart() for each file.
 
Things are fine up to this point, but I am wondering How am I supposed to know the value of cursor.offset before getting the lookup_failed error.
 
Here's my code
const uploadImagesToDropbox = async (team_member_id, dbxUser, clientFolder) => {
	try{
		const queueData = await uploadQueue.find({team_member_id, status: 2})
		const appDir = path.dirname(require.main.filename);
		const printResPath = `${clientFolder}/Print Resolution`;
		const webResPath = `${clientFolder}/Web Resolution`;
		const isPrintResPathExists = await dbxFolderExists(dbxUser, printResPath);
		const isWebResPathExists = await dbxFolderExists(dbxUser, webResPath);
		!isPrintResPathExists && await dbxUser.filesCreateFolderV2({path: printResPath})
		!isWebResPathExists && await dbxUser.filesCreateFolderV2({path: webResPath});
		const batchData= [];
		for(let data of queueData){
			let PrintResFilePath = `${appDir}${data.destination.split("/").join("\\")}\\${data.filename}`;
			let WebResFilePath = `${appDir}${data.destination.split("/").join("\\")}\\Web_Resolution\\${data.filename}`;
			let PrintResFile = fs.readFileSync(PrintResFilePath)
			let WebResFile = fs.readFileSync(WebResFilePath)
			const printResStart = await dbxUser.filesUploadSessionStart({ contents: PrintResFile, close: true})
			const webResStart = await dbxUser.filesUploadSessionStart({ contents: WebResFile, close: true})
			batchData.push({
				cursor:{
					session_id: printResStart.session_id,
					offset: 0 //<--- What should I pass Here?
				},
				commit: {
					path: printResPath
				}
			})

			batchData.push({
				cursor:{
					session_id: webResStart.session_id,
					offset: 0 //<--- What should I pass Here?
				},
				commit: {
					path: webResPath
				}
			})
			console.log(data.filename, "Uploaded")
		}console.log(batchData)
		const res = await dbxUser.filesUploadSessionFinishBatch({ entries: batchData });
		console.log(res)
		const status = await dbxUser.filesUploadSessionFinishBatchCheck({async_job_id: res.async_job_id})
		console.log(JSON.stringify(status))
		let interval = setInterval(async () => {
			const status = await dbxUser.filesUploadSessionFinishBatchCheck({async_job_id: res.async_job_id})
			status['.tag'] === "complete" && clearInterval(interval)
			console.log(JSON.stringify(status))
		}, 10000);
		
		await uploadQueue.deleteMany({team_member_id})
		rimraf.sync(`${appDir}\\uploads\\${team_member_id.replace(":", "_")}`)
	} catch(error){
		console.log(error)
		return error;
	}
}
Currently, this code throws the following error
{
  ".tag": "complete",
  "entries": [
    {
      ".tag": "failure",
      "failure": {
        ".tag": "lookup_failed",
        "lookup_failed": {
          ".tag": "incorrect_offset",
          "correct_offset": 11721555
        }
      }
    },
    {
      ".tag": "failure",
      "failure": {
        ".tag": "lookup_failed",
        "lookup_failed": {
          ".tag": "incorrect_offset",
          "correct_offset": 3529761
        }
      }
    },
    {
      ".tag": "failure",
      "failure": {
        ".tag": "lookup_failed",
        "lookup_failed": {
          ".tag": "incorrect_offset",
          "correct_offset": 8843127
        }
      }
    },
    {
      ".tag": "failure",
      "failure": {
        ".tag": "lookup_failed",
        "lookup_failed": {
          ".tag": "incorrect_offset",
          "correct_offset": 2877230
        }
      }
    },
    {
      ".tag": "failure",
      "failure": {
        ".tag": "lookup_failed",
        "lookup_failed": {
          ".tag": "incorrect_offset",
          "correct_offset": 10066923
        }
      }
    },
    {
      ".tag": "failure",
      "failure": {
        ".tag": "lookup_failed",
        "lookup_failed": {
          ".tag": "incorrect_offset",
          "correct_offset": 3037603
        }
      }
    }
  ]
}
I know there's a correct_offset value, but I received it after calling
filesUploadSessionFinishBatchCheck(). However, this is something that I should know before calling the filesUploadSessionFinishBatchCheck() so that I can pass it down

1 Reply

  • Greg-DB's avatar
    Greg-DB
    Icon for Dropbox Community Moderator rankDropbox Community Moderator
    7 years ago

    You should keep track of the offset client-side, while uploading the data, so that you can specify the value when sending the next piece of the file, or when finishing the file. There's a sample implementation here.

    It looks like your usage is a little different though, in that you're not using filesUploadSessionAppendV2. It sounds like your files are small enough that you can upload all of the data with a single call to filesUploadSessionStart each. In this case, that means that your "offset" would just be the length of the file data.

About Discuss Dropbox Developer & API

Node avatar for Discuss Dropbox Developer & API
Make connections with other developers814 PostsLatest Activity: 3 days ago
276 Following

The Dropbox Community team is active from Monday to Friday. We try to respond to you as soon as we can, usually within 2 hours.

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!