Created
August 7, 2022 01:53
-
-
Save semlinker/b594458ea6522f7e15eeead953fe2b1c to your computer and use it in GitHub Desktop.
Concurrent Upload of Large Files in JavaScript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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