Fix GraphQL max upload size (#5940)

This commit is contained in:
Antonio Davi Macedo Coelho de Castro
2019-08-18 23:23:59 -07:00
committed by Diamond Lewis
parent c4e016e5de
commit f5ac94ddb2
2 changed files with 35 additions and 10 deletions

View File

@@ -111,6 +111,20 @@ describe('ParseGraphQLServer', () => {
}); });
}); });
describe('_transformMaxUploadSizeToBytes', () => {
it('should transform to bytes', () => {
expect(parseGraphQLServer._transformMaxUploadSizeToBytes('20mb')).toBe(
20971520
);
expect(parseGraphQLServer._transformMaxUploadSizeToBytes('333Gb')).toBe(
357556027392
);
expect(
parseGraphQLServer._transformMaxUploadSizeToBytes('123456KB')
).toBe(126418944);
});
});
describe('applyGraphQL', () => { describe('applyGraphQL', () => {
it('should require an Express.js app instance', () => { it('should require an Express.js app instance', () => {
expect(() => parseGraphQLServer.applyGraphQL()).toThrow( expect(() => parseGraphQLServer.applyGraphQL()).toThrow(

View File

@@ -54,21 +54,32 @@ class ParseGraphQLServer {
} }
} }
_transformMaxUploadSizeToBytes(maxUploadSize) {
const unitMap = {
kb: 1,
mb: 2,
gb: 3,
};
return (
Number(maxUploadSize.slice(0, -2)) *
Math.pow(1024, unitMap[maxUploadSize.slice(-2).toLowerCase()])
);
}
applyGraphQL(app) { applyGraphQL(app) {
if (!app || !app.use) { if (!app || !app.use) {
requiredParameter('You must provide an Express.js app instance!'); requiredParameter('You must provide an Express.js app instance!');
} }
const maxUploadSize = this.parseServer.config.maxUploadSize || '20mb'; app.use(
const maxFileSize = this.config.graphQLPath,
(Number(maxUploadSize.slice(0, -2)) * 1024) ^ graphqlUploadExpress({
{ maxFileSize: this._transformMaxUploadSizeToBytes(
kb: 1, this.parseServer.config.maxUploadSize || '20mb'
mb: 2, ),
gb: 3, })
}[maxUploadSize.slice(-2).toLowerCase()]; );
app.use(this.config.graphQLPath, graphqlUploadExpress({ maxFileSize }));
app.use(this.config.graphQLPath, corsMiddleware()); app.use(this.config.graphQLPath, corsMiddleware());
app.use(this.config.graphQLPath, bodyParser.json()); app.use(this.config.graphQLPath, bodyParser.json());
app.use(this.config.graphQLPath, handleParseHeaders); app.use(this.config.graphQLPath, handleParseHeaders);