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 4 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));
}

getFileRange(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;
}

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

export default FilesController;
114 changes: 103 additions & 11 deletions src/Routers/FilesRouter.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,31 @@ 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 (req.get['Range']) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we just check that typeof this.adapter.getFileStream === 'function'

This way we'll fall through regular response for non supporting adapters and it will be forward compatible?

if (typeof filesController.adapter.constructor.name !== 'undefined') {
if (filesController.adapter.constructor.name == 'GridStoreAdapter') {
filesController.getFileRange(config, filename).then((gridFile) => {
handleRangeRequest(gridFile, 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 +108,81 @@ export class FilesRouter {
});
}
}

function handleRangeRequest(gridFile, 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) : gridFile.length - 1;
var chunksize = (end - start) + 1;

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

if (!partialend) {
if (((gridFile.length-1) - start) < (buffer_size)) {
end = gridFile.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 + '/' + gridFile.length,
'Accept-Ranges': 'bytes',
'Content-Length': chunksize,
'Content-Type': contentType,
});

gridFile.seek(start, function () {
// get gridFile stream
var stream = gridFile.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
stream.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;
gridFile.close();
res.end();
this.destroy();
}
});
});
}