Skip to content

Stream video with GridStoreAdapter (implements byte-range requests) #2437

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/Adapters/Files/GridStoreAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,15 @@ export class GridStoreAdapter extends FilesAdapter {
getFileLocation(config, filename) {
return (config.mount + '/files/' + config.applicationId + '/' + encodeURIComponent(filename));
}

getFileStream(filename: string) {
return this._connect().then(database => {
return GridStore.exist(database, filename).then(() => {
let gridStore = new GridStore(database, filename, 'r');
return gridStore.open();
});
});
}
}

export default GridStoreAdapter;
4 changes: 4 additions & 0 deletions src/Controllers/FilesController.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,10 @@ export class FilesController extends AdaptableController {
expectedAdapterType() {
return FilesAdapter;
}

getFileStream(config, filename) {
return this.adapter.getFileStream(filename);
}
}

export default FilesController;
126 changes: 115 additions & 11 deletions src/Routers/FilesRouter.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,27 @@ export class FilesRouter {
const config = new Config(req.params.appId);
const filesController = config.filesController;
const filename = req.params.filename;
filesController.getFileData(config, filename).then((data) => {
res.status(200);
var contentType = mime.lookup(filename);
res.set('Content-Type', contentType);
res.set('Content-Length', data.length);
res.end(data);
}).catch((err) => {
res.status(404);
res.set('Content-Type', 'text/plain');
res.end('File not found.');
});
const contentType = mime.lookup(filename);
if (isFileStreamable(req, filesController)) {
filesController.getFileStream(config, filename).then((stream) => {
handleFileStream(stream, req, res, contentType);
}).catch((err) => {
res.status(404);
res.set('Content-Type', 'text/plain');
res.end('File not found.');
});
} else {
filesController.getFileData(config, filename).then((data) => {
res.status(200);
res.set('Content-Type', contentType);
res.set('Content-Length', data.length);
res.end(data);
}).catch((err) => {
res.status(404);
res.set('Content-Type', 'text/plain');
res.end('File not found.');
});
}
}

createHandler(req, res, next) {
Expand Down Expand Up @@ -94,3 +104,97 @@ export class FilesRouter {
});
}
}

function isFileStreamable(req, filesController){
if (req.get['Range']) {
if (!(typeof filesController.adapter.getFileStream === 'function')) {
return false;
}
if (typeof filesController.adapter.constructor.name !== 'undefined') {
if (filesController.adapter.constructor.name == 'GridStoreAdapter') {
return true;
}
}
}
return false;
}

// handleFileStream is licenced under Creative Commons Attribution 4.0 International License (https://creativecommons.org/licenses/by/4.0/).
// Author: LEROIB at weightingformypizza (https://weightingformypizza.wordpress.com/2015/06/24/stream-html5-media-content-like-video-audio-from-mongodb-using-express-and-gridstore/).
function handleFileStream(stream, req, res, contentType) {
var buffer_size = 1024 * 1024;//1024Kb
// Range request, partiall stream the file
var parts = req.get["Range"].replace(/bytes=/, "").split("-");
var partialstart = parts[0];
var partialend = parts[1];
var start = partialstart ? parseInt(partialstart, 10) : 0;
var end = partialend ? parseInt(partialend, 10) : stream.length - 1;
var chunksize = (end - start) + 1;

if (chunksize == 1) {
start = 0;
partialend = false;
}

if (!partialend) {
if (((stream.length-1) - start) < (buffer_size)) {
end = stream.length - 1;
}else{
end = start + (buffer_size);
}
chunksize = (end - start) + 1;
}

if (start == 0 && end == 2) {
chunksize = 1;
}

res.writeHead(206, {
'Content-Range': 'bytes ' + start + '-' + end + '/' + stream.length,
'Accept-Ranges': 'bytes',
'Content-Length': chunksize,
'Content-Type': contentType,
});

stream.seek(start, function () {
// get gridFile stream
var gridFileStream = stream.stream(true);
var ended = false;
var bufferIdx = 0;
var bufferAvail = 0;
var range = (end - start) + 1;
var totalbyteswanted = (end - start) + 1;
var totalbyteswritten = 0;
// write to response
gridFileStream.on('data', function (buff) {
bufferAvail += buff.length;
//Ok check if we have enough to cover our range
if (bufferAvail < range) {
//Not enough bytes to satisfy our full range
if (bufferAvail > 0) {
//Write full buffer
res.write(buff);
totalbyteswritten += buff.length;
range -= buff.length;
bufferIdx += buff.length;
bufferAvail -= buff.length;
}
} else {
//Enough bytes to satisfy our full range!
if (bufferAvail > 0) {
const buffer = buff.slice(0,range);
res.write(buffer);
totalbyteswritten += buffer.length;
bufferIdx += range;
bufferAvail -= range;
}
}
if (totalbyteswritten >= totalbyteswanted) {
//totalbytes = 0;
stream.close();
res.end();
this.destroy();
}
});
});
}