Skip to content

Commit

Permalink
feat(packager): serve mp4 files in ranges
Browse files Browse the repository at this point in the history
  • Loading branch information
mroswald committed Jun 18, 2016
1 parent ee0333c commit 4c64464
Showing 1 changed file with 22 additions and 1 deletion.
23 changes: 22 additions & 1 deletion packager/react-packager/src/Server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,28 @@ class Server {
const assetEvent = Activity.startEvent(`processing asset request ${assetPath[1]}`);
this._assetServer.get(assetPath[1], urlObj.query.platform)
.then(
data => res.end(data),
data => {
if (assetPath[1].match(/\.mp4$/)) {
let [rangeStart, rangeEnd] = req.headers.range.replace(/bytes=/, '').split('-');
let dataStart = parseInt(rangeStart, 10);
let dataEnd = rangeEnd ? parseInt(rangeEnd, 10) : total - 1;
let chunksize = (dataEnd - dataStart) + 1;

res.writeHead(206, {
'Accept-Ranges': 'bytes',
'Content-Length': chunksize,
'Content-Range': 'bytes ' + dataStart + '-' + dataEnd + '/' + data.length,
'Content-Type': 'video/mp4'
});

let output = new Buffer(chunksize);
data.copy(output, 0, dataStart, dataEnd);

res.end(output);
} else {
res.end(data);
}
},
error => {
console.error(error.stack);
res.writeHead('404');
Expand Down

0 comments on commit 4c64464

Please sign in to comment.