The following code once successfully uploaded video, but now does not. No errors thrown on execution of the upload() function and I get 200 a response back from the server for each chunk read from the file. I wonder if there is something wrong with my account - I got a warning to renew and afterwards I started having issues? Thanks much for your help.
const fs = require('fs');
const path = require('path');
const { dbx } = require('../dropbox/dbx');
const MAX_RETRIES = 100;
const CHUNK_SIZE = 10485760;
const uploadBigFile = async (localPath, uploadPath, size) => {
const stream = fs.createReadStream(localPath, {
highWaterMark: CHUNK_SIZE,
});
let chunkNumber = 0;
let offset = 0;
let session_id;
for await (let chunk of stream) {
if (!chunkNumber++) {
const { result } = await dbx.filesUploadSessionStart({
contents: chunk,
});
session_id = result.session_id;
} else if (offset + chunk.length === size) {
await dbx.filesUploadSessionFinish({
contents: chunk,
cursor: { session_id, offset },
commit: {
path: uploadPath,
mode: 'overwrite',
client_modified: date,
},
});
} else {
await dbx.filesUploadSessionAppendV2({
contents: chunk,
cursor: { offset, session_id },
close: false,
});
}
offset += chunk.length;
}
};
const upload = ({ futureDropboxFileName, date }) => {
return new Promise(async (resolve, reject) => {
async function doUpload(futureDropboxFileName, date, photo, retries = 0) {
const dropboxPath = `${process.env.DROPBOX_FOLDER}${futureDropboxFileName}`;
const convertedPath = path.join(__dirname, '/tmp/', futureDropboxFileName);
try {
const { size } = fs.statSync(convertedPath);
await uploadBigFile(convertedPath, dropboxPath, date, size);
resolve('ok');
} catch (error) {
if (retries <= MAX_RETRIES) {
doUpload(futureDropboxFileName, date, photo, retries + 1);
} else {
return reject(error);
}
}
}
return doUpload(futureDropboxFileName, date, photo);
});
};