'node.js Get file through http request in chunks, then join them
I am using range header to download mp4 file in parts like so:
const chunkFile = fs.createWriteStream('5a52e9ba-9328-11ec-b909-0242ac120002.mp4.chunk');
const downloadRequest = https.get({
...
range: 'bytes=0-10000'
}, response => {
...
response.pipe(chunkFile);
}
I do this in loop for all ranges and I end up with a bunch of chunk files in directory, I simplified it for the sake of the question. Then I join the chunks back into one file like so:
function joinChunks({chunkHashes, fileName}) {
const outputFile = fs.createWriteStream(fileName);
for (let i = 0, {length} = chunkHashes; i < length; i++) {
const chunkData = fs.readFileSync(chunkHashes[i]);
outputFile.write(chunkData);
fs.unlinkSync(chunkHashes[i]);
}
}
Was I naive to think it would work like that? The resulting file is the same size but it's broken. Is there a way to store raw data from http response in chunk files on disk, and then join that data to end up with the original file?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
