Use Prettier JS (#5017)

* Adds prettier

* Run lint before tests
This commit is contained in:
Florent Vilmart
2018-09-01 13:58:06 -04:00
committed by GitHub
parent 189cd259ee
commit d83a0b6808
240 changed files with 41098 additions and 29020 deletions

View File

@@ -1,30 +1,37 @@
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';
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';
export class FilesRouter {
expressRouter({ maxUploadSize = '20Mb' } = {}) {
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.'));
next(
new Parse.Error(Parse.Error.INVALID_FILE_NAME, 'Filename not provided.')
);
});
router.post('/files/:filename',
router.post(
'/files/:filename',
Middlewares.allowCrossDomain,
BodyParser.raw({type: () => { return true; }, limit: maxUploadSize }), // Allow uploads without Content-Type, or with any Content-Type.
BodyParser.raw({
type: () => {
return true;
},
limit: maxUploadSize,
}), // Allow uploads without Content-Type, or with any Content-Type.
Middlewares.handleParseHeaders,
this.createHandler
);
router.delete('/files/:filename',
router.delete(
'/files/:filename',
Middlewares.allowCrossDomain,
Middlewares.handleParseHeaders,
Middlewares.enforceMasterKeyAccess,
@@ -39,43 +46,55 @@ export class FilesRouter {
const filename = req.params.filename;
const contentType = mime.getType(filename);
if (isFileStreamable(req, filesController)) {
filesController.getFileStream(config, filename).then((stream) => {
handleFileStream(stream, req, res, contentType);
}).catch(() => {
res.status(404);
res.set('Content-Type', 'text/plain');
res.end('File not found.');
});
filesController
.getFileStream(config, filename)
.then(stream => {
handleFileStream(stream, req, res, contentType);
})
.catch(() => {
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(() => {
res.status(404);
res.set('Content-Type', 'text/plain');
res.end('File not found.');
});
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.');
});
}
}
createHandler(req, res, next) {
if (!req.body || !req.body.length) {
next(new Parse.Error(Parse.Error.FILE_SAVE_ERROR,
'Invalid file upload.'));
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.'));
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.'));
next(
new Parse.Error(
Parse.Error.INVALID_FILE_NAME,
'Filename contains invalid characters.'
)
);
return;
}
@@ -84,35 +103,53 @@ export class FilesRouter {
const config = req.config;
const filesController = config.filesController;
filesController.createFile(config, filename, req.body, contentType).then((result) => {
res.status(201);
res.set('Location', result.url);
res.json(result);
}).catch((e) => {
logger.error(e.message, e);
next(new Parse.Error(Parse.Error.FILE_SAVE_ERROR, 'Could not store file.'));
});
filesController
.createFile(config, filename, req.body, contentType)
.then(result => {
res.status(201);
res.set('Location', result.url);
res.json(result);
})
.catch(e => {
logger.error(e.message, e);
next(
new Parse.Error(Parse.Error.FILE_SAVE_ERROR, 'Could not store file.')
);
});
}
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();
}).catch(() => {
next(new Parse.Error(Parse.Error.FILE_DELETE_ERROR,
'Could not delete file.'));
});
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.'
)
);
});
}
}
function isFileStreamable(req, filesController){
return req.get('Range') && typeof filesController.adapter.getFileStream === 'function';
function isFileStreamable(req, filesController) {
return (
req.get('Range') &&
typeof filesController.adapter.getFileStream === 'function'
);
}
function getRange(req) {
const parts = req.get('Range').replace(/bytes=/, "").split("-");
const parts = req
.get('Range')
.replace(/bytes=/, '')
.split('-');
return { start: parseInt(parts[0], 10), end: parseInt(parts[1], 10) };
}
@@ -121,12 +158,10 @@ function getRange(req) {
function handleFileStream(stream, req, res, contentType) {
const buffer_size = 1024 * 1024; //1024Kb
// Range request, partiall stream the file
let {
start, end
} = getRange(req);
let { start, end } = getRange(req);
const notEnded = (!end && end !== 0);
const notStarted = (!start && start !== 0);
const notEnded = !end && end !== 0;
const notStarted = !start && start !== 0;
// No end provided, we want all bytes
if (notEnded) {
end = stream.length - 1;
@@ -142,7 +177,7 @@ function handleFileStream(stream, req, res, contentType) {
end = start + buffer_size - 1;
}
const contentLength = (end - start) + 1;
const contentLength = end - start + 1;
res.writeHead(206, {
'Content-Range': 'bytes ' + start + '-' + end + '/' + stream.length,
@@ -151,14 +186,14 @@ function handleFileStream(stream, req, res, contentType) {
'Content-Type': contentType,
});
stream.seek(start, function () {
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) {
gridFileStream.on('data', function(data) {
bufferAvail += data.length;
if (bufferAvail > 0) {
// slice returns the same buffer if overflowing