Skip to content

Commit f5ac94d

Browse files
davimacedodplewis
authored andcommitted
Fix GraphQL max upload size (#5940)
1 parent c4e016e commit f5ac94d

File tree

2 files changed

+35
-10
lines changed

2 files changed

+35
-10
lines changed

spec/ParseGraphQLServer.spec.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,20 @@ describe('ParseGraphQLServer', () => {
111111
});
112112
});
113113

114+
describe('_transformMaxUploadSizeToBytes', () => {
115+
it('should transform to bytes', () => {
116+
expect(parseGraphQLServer._transformMaxUploadSizeToBytes('20mb')).toBe(
117+
20971520
118+
);
119+
expect(parseGraphQLServer._transformMaxUploadSizeToBytes('333Gb')).toBe(
120+
357556027392
121+
);
122+
expect(
123+
parseGraphQLServer._transformMaxUploadSizeToBytes('123456KB')
124+
).toBe(126418944);
125+
});
126+
});
127+
114128
describe('applyGraphQL', () => {
115129
it('should require an Express.js app instance', () => {
116130
expect(() => parseGraphQLServer.applyGraphQL()).toThrow(

src/GraphQL/ParseGraphQLServer.js

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -54,21 +54,32 @@ class ParseGraphQLServer {
5454
}
5555
}
5656

57+
_transformMaxUploadSizeToBytes(maxUploadSize) {
58+
const unitMap = {
59+
kb: 1,
60+
mb: 2,
61+
gb: 3,
62+
};
63+
64+
return (
65+
Number(maxUploadSize.slice(0, -2)) *
66+
Math.pow(1024, unitMap[maxUploadSize.slice(-2).toLowerCase()])
67+
);
68+
}
69+
5770
applyGraphQL(app) {
5871
if (!app || !app.use) {
5972
requiredParameter('You must provide an Express.js app instance!');
6073
}
6174

62-
const maxUploadSize = this.parseServer.config.maxUploadSize || '20mb';
63-
const maxFileSize =
64-
(Number(maxUploadSize.slice(0, -2)) * 1024) ^
65-
{
66-
kb: 1,
67-
mb: 2,
68-
gb: 3,
69-
}[maxUploadSize.slice(-2).toLowerCase()];
70-
71-
app.use(this.config.graphQLPath, graphqlUploadExpress({ maxFileSize }));
75+
app.use(
76+
this.config.graphQLPath,
77+
graphqlUploadExpress({
78+
maxFileSize: this._transformMaxUploadSizeToBytes(
79+
this.parseServer.config.maxUploadSize || '20mb'
80+
),
81+
})
82+
);
7283
app.use(this.config.graphQLPath, corsMiddleware());
7384
app.use(this.config.graphQLPath, bodyParser.json());
7485
app.use(this.config.graphQLPath, handleParseHeaders);

0 commit comments

Comments
 (0)