2018-09-01 13:58:06 -04:00
|
|
|
import express from 'express';
|
|
|
|
|
import BodyParser from 'body-parser';
|
|
|
|
|
import * as Middlewares from '../middlewares';
|
|
|
|
|
import Parse from 'parse/node';
|
|
|
|
|
import Config from '../Config';
|
|
|
|
|
import mime from 'mime';
|
|
|
|
|
import logger from '../logger';
|
2016-02-20 12:25:43 -05:00
|
|
|
|
|
|
|
|
export class FilesRouter {
|
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) {
|
2018-09-01 13:58:06 -04:00
|
|
|
next(
|
|
|
|
|
new Parse.Error(Parse.Error.INVALID_FILE_NAME, 'Filename not provided.')
|
|
|
|
|
);
|
2016-02-20 12:25:43 -05:00
|
|
|
});
|
|
|
|
|
|
2018-09-01 13:58:06 -04:00
|
|
|
router.post(
|
|
|
|
|
'/files/:filename',
|
|
|
|
|
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
|
|
|
|
|
);
|
|
|
|
|
|
2018-09-01 13:58:06 -04:00
|
|
|
router.delete(
|
|
|
|
|
'/files/:filename',
|
2016-02-20 12:25:43 -05:00
|
|
|
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)) {
|
2018-09-01 13:58:06 -04:00
|
|
|
filesController
|
2019-09-11 09:34:39 -05:00
|
|
|
.handleFileStream(config, filename, req, res, contentType)
|
2018-09-01 13:58:06 -04:00
|
|
|
.catch(() => {
|
|
|
|
|
res.status(404);
|
|
|
|
|
res.set('Content-Type', 'text/plain');
|
|
|
|
|
res.end('File not found.');
|
|
|
|
|
});
|
2016-08-12 21:58:18 +02:00
|
|
|
} else {
|
2018-09-01 13:58:06 -04:00
|
|
|
filesController
|
|
|
|
|
.getFileData(config, filename)
|
|
|
|
|
.then(data => {
|
|
|
|
|
res.status(200);
|
|
|
|
|
res.set('Content-Type', contentType);
|
|
|
|
|
res.set('Content-Length', data.length);
|
|
|
|
|
res.end(data);
|
|
|
|
|
})
|
|
|
|
|
.catch(() => {
|
|
|
|
|
res.status(404);
|
|
|
|
|
res.set('Content-Type', 'text/plain');
|
|
|
|
|
res.end('File not found.');
|
|
|
|
|
});
|
2016-08-12 21:58:18 +02:00
|
|
|
}
|
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) {
|
2018-09-01 13:58:06 -04:00
|
|
|
next(
|
|
|
|
|
new Parse.Error(Parse.Error.FILE_SAVE_ERROR, 'Invalid file upload.')
|
|
|
|
|
);
|
2016-02-20 12:25:43 -05:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (req.params.filename.length > 128) {
|
2018-09-01 13:58:06 -04:00
|
|
|
next(
|
|
|
|
|
new Parse.Error(Parse.Error.INVALID_FILE_NAME, 'Filename too long.')
|
|
|
|
|
);
|
2016-02-20 12:25:43 -05:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!req.params.filename.match(/^[_a-zA-Z0-9][a-zA-Z0-9@\.\ ~_-]*$/)) {
|
2018-09-01 13:58:06 -04:00
|
|
|
next(
|
|
|
|
|
new Parse.Error(
|
|
|
|
|
Parse.Error.INVALID_FILE_NAME,
|
|
|
|
|
'Filename contains invalid characters.'
|
|
|
|
|
)
|
|
|
|
|
);
|
2016-02-20 12:25:43 -05:00
|
|
|
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;
|
|
|
|
|
|
2018-09-01 13:58:06 -04:00
|
|
|
filesController
|
|
|
|
|
.createFile(config, filename, req.body, contentType)
|
|
|
|
|
.then(result => {
|
|
|
|
|
res.status(201);
|
|
|
|
|
res.set('Location', result.url);
|
|
|
|
|
res.json(result);
|
|
|
|
|
})
|
|
|
|
|
.catch(e => {
|
2019-04-15 09:03:33 +10:00
|
|
|
logger.error('Error creating a file: ', e);
|
2018-09-01 13:58:06 -04:00
|
|
|
next(
|
2019-04-15 09:03:33 +10:00
|
|
|
new Parse.Error(
|
|
|
|
|
Parse.Error.FILE_SAVE_ERROR,
|
|
|
|
|
`Could not store file: ${filename}.`
|
|
|
|
|
)
|
2018-09-01 13:58:06 -04:00
|
|
|
);
|
|
|
|
|
});
|
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;
|
2018-09-01 13:58:06 -04:00
|
|
|
filesController
|
|
|
|
|
.deleteFile(req.config, req.params.filename)
|
|
|
|
|
.then(() => {
|
|
|
|
|
res.status(200);
|
|
|
|
|
// TODO: return useful JSON here?
|
|
|
|
|
res.end();
|
|
|
|
|
})
|
|
|
|
|
.catch(() => {
|
|
|
|
|
next(
|
|
|
|
|
new Parse.Error(
|
|
|
|
|
Parse.Error.FILE_DELETE_ERROR,
|
|
|
|
|
'Could not delete file.'
|
|
|
|
|
)
|
|
|
|
|
);
|
|
|
|
|
});
|
2016-02-20 12:25:43 -05:00
|
|
|
}
|
2016-06-10 20:27:21 -07:00
|
|
|
}
|
2016-08-12 21:58:18 +02:00
|
|
|
|
2018-09-01 13:58:06 -04:00
|
|
|
function isFileStreamable(req, filesController) {
|
|
|
|
|
return (
|
|
|
|
|
req.get('Range') &&
|
2019-09-11 09:34:39 -05:00
|
|
|
typeof filesController.adapter.handleFileStream === 'function'
|
2018-09-01 13:58:06 -04:00
|
|
|
);
|
2017-09-05 17:51:11 -04:00
|
|
|
}
|