Skip to content

Instantly share code, notes, and snippets.

@semlinker
Created August 7, 2022 01:53
Show Gist options
  • Save semlinker/b594458ea6522f7e15eeead953fe2b1c to your computer and use it in GitHub Desktop.
Save semlinker/b594458ea6522f7e15eeead953fe2b1c to your computer and use it in GitHub Desktop.
Concurrent Upload of Large Files in JavaScript
function upload({
url,
file,
fileMd5,
fileSize,
chunkSize,
chunkIds,
poolLimit = 1,
}) {
const chunks =
typeof chunkSize === "number" ? Math.ceil(fileSize / chunkSize) : 1;
return asyncPool(poolLimit, [...new Array(chunks).keys()], (i) => {
if (chunkIds.indexOf(i + "") !== -1) {
// Ignore uploaded chunks
return Promise.resolve();
}
let start = i * chunkSize;
let end = i + 1 == chunks ? fileSize : (i + 1) * chunkSize;
const chunk = file.slice(start, end);
return uploadChunk({
url,
chunk,
chunkIndex: i,
fileMd5,
fileName: file.name,
});
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment