I'm trying to download a file in chunks with HTTP Range header but I got inconsistent results:
function generateRange(originalSize, split) {
let resultSize = 0;
let arr = [];
var chunkSize = Math.ceil(originalSize / split)
for (i = 0; i < split; i++) {
var min = i * chunkSize;
var max = min + chunkSize - 1;
resultSize += max - min + 1
arr.push(`${min}-${max}`)
}
console.log(JSON.stringify(arr))
console.log(`split: ${split} | originalSize: ${originalSize} bytes | resultSize: ${resultSize} bytes\n\n`)
}
generateRange(200, 4) // work
generateRange(200, 7)
generateRange(469, 4)
.as-console-wrapper { max-height: 100% !important; }
You have to add the following code:
if(i == split - 1)
max = originalSize - 1;
function generateRange(originalSize, split) {
let resultSize = 0;
let arr = [];
var chunkSize = Math. floor(originalSize / split)
for (i = 0; i < split; i++) {
var min = i * chunkSize;
var max = min + chunkSize - 1;
if(i == split - 1)
max = originalSize - 1;
resultSize += max - min + 1
arr.push(`${min}-${max}`)
}
console.log(JSON.stringify(arr))
console.log(`split: ${split} | originalSize: ${originalSize} bytes | resultSize: ${resultSize} bytes\n\n`)
}
generateRange(200, 4) // work
generateRange(200, 7)
generateRange(469, 4)
.as-console-wrapper { max-height: 100% !important; }