2016-06-10 20:27:21 -07:00
|
|
|
import express from 'express';
|
|
|
|
|
import BodyParser from 'body-parser';
|
|
|
|
|
import * as Middlewares from '../middlewares';
|
2016-11-24 15:47:41 -05:00
|
|
|
import Parse from 'parse/node';
|
2016-06-10 20:27:21 -07:00
|
|
|
import Config from '../Config';
|
|
|
|
|
import mime from 'mime';
|
2017-01-30 09:36:30 -08:00
|
|
|
import logger from '../logger';
|
2016-02-20 12:25:43 -05:00
|
|
|
|
|
|
|
|
export class FilesRouter {
|
2016-02-22 13:59:24 -08:00
|
|
|
|
2017-09-05 17:51:11 -04:00
|
|
|
expressRouter({ maxUploadSize = '20Mb' } = {}) {
|
2016-02-20 12:25:43 -05:00
|
|
|
var router = express.Router();
|
|
|
|
|
router.get('/files/:appId/:filename', this.getHandler);
|
|
|
|
|
|
|
|
|
|
router.post('/files', function(req, res, next) {
|
|
|
|
|
next(new Parse.Error(Parse.Error.INVALID_FILE_NAME,
|
|
|
|
|
'Filename not provided.'));
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
router.post('/files/:filename',
|
|
|
|
|
Middlewares.allowCrossDomain,
|
2017-09-05 17:51:11 -04:00
|
|
|
BodyParser.raw({type: () => { return true; }, limit: maxUploadSize }), // Allow uploads without Content-Type, or with any Content-Type.
|
2016-02-20 12:25:43 -05:00
|
|
|
Middlewares.handleParseHeaders,
|
|
|
|
|
this.createHandler
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
router.delete('/files/:filename',
|
|
|
|
|
Middlewares.allowCrossDomain,
|
|
|
|
|
Middlewares.handleParseHeaders,
|
|
|
|
|
Middlewares.enforceMasterKeyAccess,
|
|
|
|
|
this.deleteHandler
|
|
|
|
|
);
|
|
|
|
|
return router;
|
|
|
|
|
}
|
2016-02-22 13:59:24 -08:00
|
|
|
|
|
|
|
|
getHandler(req, res) {
|
2017-10-23 08:43:05 -04:00
|
|
|
const config = Config.get(req.params.appId);
|
2016-02-20 12:25:43 -05:00
|
|
|
const filesController = config.filesController;
|
|
|
|
|
const filename = req.params.filename;
|
2017-11-14 21:08:15 -05:00
|
|
|
const contentType = mime.getType(filename);
|
2016-08-12 21:58:18 +02:00
|
|
|
if (isFileStreamable(req, filesController)) {
|
|
|
|
|
filesController.getFileStream(config, filename).then((stream) => {
|
|
|
|
|
handleFileStream(stream, req, res, contentType);
|
2016-11-24 15:47:41 -05:00
|
|
|
}).catch(() => {
|
2016-08-12 21:58:18 +02:00
|
|
|
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);
|
2016-11-24 15:47:41 -05:00
|
|
|
}).catch(() => {
|
2016-08-12 21:58:18 +02:00
|
|
|
res.status(404);
|
|
|
|
|
res.set('Content-Type', 'text/plain');
|
|
|
|
|
res.end('File not found.');
|
|
|
|
|
});
|
|
|
|
|
}
|
2016-02-20 12:25:43 -05:00
|
|
|
}
|
2016-02-22 13:59:24 -08:00
|
|
|
|
2016-02-20 12:25:43 -05:00
|
|
|
createHandler(req, res, next) {
|
|
|
|
|
if (!req.body || !req.body.length) {
|
|
|
|
|
next(new Parse.Error(Parse.Error.FILE_SAVE_ERROR,
|
|
|
|
|
'Invalid file upload.'));
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (req.params.filename.length > 128) {
|
|
|
|
|
next(new Parse.Error(Parse.Error.INVALID_FILE_NAME,
|
|
|
|
|
'Filename too long.'));
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!req.params.filename.match(/^[_a-zA-Z0-9][a-zA-Z0-9@\.\ ~_-]*$/)) {
|
|
|
|
|
next(new Parse.Error(Parse.Error.INVALID_FILE_NAME,
|
|
|
|
|
'Filename contains invalid characters.'));
|
|
|
|
|
return;
|
|
|
|
|
}
|
2016-02-22 13:59:24 -08:00
|
|
|
|
2016-03-01 10:14:03 -05:00
|
|
|
const filename = req.params.filename;
|
|
|
|
|
const contentType = req.get('Content-type');
|
2016-02-20 12:25:43 -05:00
|
|
|
const config = req.config;
|
|
|
|
|
const filesController = config.filesController;
|
|
|
|
|
|
2016-03-01 09:02:33 -05:00
|
|
|
filesController.createFile(config, filename, req.body, contentType).then((result) => {
|
2016-02-22 13:59:24 -08:00
|
|
|
res.status(201);
|
2016-02-20 12:25:43 -05:00
|
|
|
res.set('Location', result.url);
|
|
|
|
|
res.json(result);
|
2017-01-30 09:36:30 -08:00
|
|
|
}).catch((e) => {
|
|
|
|
|
logger.error(e.message, e);
|
2016-06-10 20:27:21 -07:00
|
|
|
next(new Parse.Error(Parse.Error.FILE_SAVE_ERROR, 'Could not store file.'));
|
2016-02-20 12:25:43 -05:00
|
|
|
});
|
|
|
|
|
}
|
2016-02-22 13:59:24 -08:00
|
|
|
|
2016-02-20 12:25:43 -05:00
|
|
|
deleteHandler(req, res, next) {
|
|
|
|
|
const filesController = req.config.filesController;
|
|
|
|
|
filesController.deleteFile(req.config, req.params.filename).then(() => {
|
|
|
|
|
res.status(200);
|
|
|
|
|
// TODO: return useful JSON here?
|
|
|
|
|
res.end();
|
2016-11-24 15:47:41 -05:00
|
|
|
}).catch(() => {
|
2016-02-20 12:25:43 -05:00
|
|
|
next(new Parse.Error(Parse.Error.FILE_DELETE_ERROR,
|
|
|
|
|
'Could not delete file.'));
|
|
|
|
|
});
|
|
|
|
|
}
|
2016-06-10 20:27:21 -07:00
|
|
|
}
|
2016-08-12 21:58:18 +02:00
|
|
|
|
|
|
|
|
function isFileStreamable(req, filesController){
|
2017-09-05 17:51:11 -04:00
|
|
|
return req.get('Range') && typeof filesController.adapter.getFileStream === 'function';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function getRange(req) {
|
|
|
|
|
const parts = req.get('Range').replace(/bytes=/, "").split("-");
|
|
|
|
|
return { start: parseInt(parts[0], 10), end: parseInt(parts[1], 10) };
|
2016-08-12 21:58:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 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) {
|
2017-09-05 17:51:11 -04:00
|
|
|
const buffer_size = 1024 * 1024; //1024Kb
|
2016-08-12 21:58:18 +02:00
|
|
|
// Range request, partiall stream the file
|
2017-09-05 17:51:11 -04:00
|
|
|
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;
|
2016-08-12 21:58:18 +02:00
|
|
|
}
|
2017-09-05 17:51:11 -04:00
|
|
|
// No start provided, we're reading backwards
|
|
|
|
|
if (notStarted) {
|
|
|
|
|
start = stream.length - end;
|
|
|
|
|
end = start + end - 1;
|
2016-08-12 21:58:18 +02:00
|
|
|
}
|
|
|
|
|
|
2017-09-05 17:51:11 -04:00
|
|
|
// Data exceeds the buffer_size, cap
|
|
|
|
|
if (end - start >= buffer_size) {
|
|
|
|
|
end = start + buffer_size - 1;
|
2016-08-12 21:58:18 +02:00
|
|
|
}
|
|
|
|
|
|
2017-09-05 17:51:11 -04:00
|
|
|
const contentLength = (end - start) + 1;
|
|
|
|
|
|
2016-08-12 21:58:18 +02:00
|
|
|
res.writeHead(206, {
|
|
|
|
|
'Content-Range': 'bytes ' + start + '-' + end + '/' + stream.length,
|
|
|
|
|
'Accept-Ranges': 'bytes',
|
2017-09-05 17:51:11 -04:00
|
|
|
'Content-Length': contentLength,
|
2016-08-12 21:58:18 +02:00
|
|
|
'Content-Type': contentType,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
stream.seek(start, function () {
|
|
|
|
|
// get gridFile stream
|
2017-09-05 17:51:11 -04:00
|
|
|
const gridFileStream = stream.stream(true);
|
|
|
|
|
let bufferAvail = 0;
|
|
|
|
|
let remainingBytesToWrite = contentLength;
|
|
|
|
|
let totalBytesWritten = 0;
|
2016-08-12 21:58:18 +02:00
|
|
|
// write to response
|
2017-09-05 17:51:11 -04:00
|
|
|
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;
|
2016-08-12 21:58:18 +02:00
|
|
|
}
|
2017-09-05 17:51:11 -04:00
|
|
|
// in case of small slices, all values will be good at that point
|
|
|
|
|
// we've written enough, end...
|
|
|
|
|
if (totalBytesWritten >= contentLength) {
|
2016-08-12 21:58:18 +02:00
|
|
|
stream.close();
|
|
|
|
|
res.end();
|
|
|
|
|
this.destroy();
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
}
|