Stream video with GridFSBucketAdapter (implements byte-range requests) (#6028)

* Stream video with GridFSBucketAdapter (implements byte-range requests)

Closes: https://github.com/parse-community/parse-server/issues/5834

Similar to https://github.com/parse-community/parse-server/pull/2437

I ran into this issue while trying to view a mov file in safari from the dashboard.

* Rename getFileStream to handleFileStream
This commit is contained in:
Diamond Lewis
2019-09-11 09:34:39 -05:00
committed by GitHub
parent 84776810b8
commit 63cabb8423
4 changed files with 111 additions and 87 deletions

View File

@@ -45,10 +45,7 @@ export class FilesRouter {
const contentType = mime.getType(filename);
if (isFileStreamable(req, filesController)) {
filesController
.getFileStream(config, filename)
.then(stream => {
handleFileStream(stream, req, res, contentType);
})
.handleFileStream(config, filename, req, res, contentType)
.catch(() => {
res.status(404);
res.set('Content-Type', 'text/plain');
@@ -142,80 +139,6 @@ export class FilesRouter {
function isFileStreamable(req, filesController) {
return (
req.get('Range') &&
typeof filesController.adapter.getFileStream === 'function'
typeof filesController.adapter.handleFileStream === 'function'
);
}
function getRange(req) {
const parts = req
.get('Range')
.replace(/bytes=/, '')
.split('-');
return { start: parseInt(parts[0], 10), end: parseInt(parts[1], 10) };
}
// 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) {
const buffer_size = 1024 * 1024; //1024Kb
// Range request, partiall stream the file
let { start, end } = getRange(req);
const notEnded = !end && end !== 0;
const notStarted = !start && start !== 0;
// No end provided, we want all bytes
if (notEnded) {
end = stream.length - 1;
}
// No start provided, we're reading backwards
if (notStarted) {
start = stream.length - end;
end = start + end - 1;
}
// Data exceeds the buffer_size, cap
if (end - start >= buffer_size) {
end = start + buffer_size - 1;
}
const contentLength = end - start + 1;
res.writeHead(206, {
'Content-Range': 'bytes ' + start + '-' + end + '/' + stream.length,
'Accept-Ranges': 'bytes',
'Content-Length': contentLength,
'Content-Type': contentType,
});
stream.seek(start, function() {
// get gridFile stream
const gridFileStream = stream.stream(true);
let bufferAvail = 0;
let remainingBytesToWrite = contentLength;
let totalBytesWritten = 0;
// write to response
gridFileStream.on('data', function(data) {
bufferAvail += data.length;
if (bufferAvail > 0) {
// slice returns the same buffer if overflowing
// safe to call in any case
const buffer = data.slice(0, remainingBytesToWrite);
// write the buffer
res.write(buffer);
// increment total
totalBytesWritten += buffer.length;
// decrement remaining
remainingBytesToWrite -= data.length;
// decrement the avaialbe buffer
bufferAvail -= buffer.length;
}
// in case of small slices, all values will be good at that point
// we've written enough, end...
if (totalBytesWritten >= contentLength) {
stream.close();
res.end();
this.destroy();
}
});
});
}