Skip to content

Commit 80cab9b

Browse files
authored
feat: Add Parse.File option maxUploadSize to override the Parse Server option maxUploadSize per file upload (#2940)
1 parent c7b2517 commit 80cab9b

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

src/ParseFile.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,17 @@ export type FileSaveOptions = FullOptions & {
1919
metadata?: Record<string, any>;
2020
tags?: Record<string, any>;
2121
directory?: string;
22+
/**
23+
* Overrides the server's `maxUploadSize` for this file upload. Requires the
24+
* master key (`useMasterKey: true`). The value uses the same format as the
25+
* server option (e.g. `'50mb'`, `'1gb'`).
26+
*
27+
* Only supported for Buffer and Stream source types. Files created from
28+
* base64 strings, number arrays, Blobs, or URIs do not support this option.
29+
*
30+
* Requires Parse Server >= 9.5.0.
31+
*/
32+
maxUploadSize?: string;
2233
};
2334
export type FileSource =
2435
| {
@@ -309,6 +320,10 @@ class ParseFile {
309320
* }
310321
* });
311322
* </pre>
323+
* <li>maxUploadSize: Overrides the server's maxUploadSize for this upload.
324+
* Requires the master key. Only supported for Buffer and Stream source
325+
* types; files created from base64 strings, number arrays, Blobs, or URIs
326+
* do not support this option. Requires Parse Server >= 9.5.0.
312327
* </ul>
313328
* @returns {Promise | undefined} Promise that is resolved when the save finishes.
314329
*/
@@ -333,7 +348,7 @@ class ParseFile {
333348
(this._tags && Object.keys(this._tags).length > 0) ||
334349
!!this._directory;
335350

336-
if (controller.saveBinary && (this._source.format === 'stream' || !hasFileData)) {
351+
if (controller.saveBinary && (this._source.format === 'stream' || !hasFileData || options.maxUploadSize)) {
337352
// Binary upload via ajax (file data sent via headers for streams)
338353
this._previousSave = controller
339354
.saveBinary(this._name, this._source, options)
@@ -632,6 +647,9 @@ const DefaultController = {
632647
if (options.tags && Object.keys(options.tags).length > 0) {
633648
headers['X-Parse-File-Tags'] = JSON.stringify(options.tags);
634649
}
650+
if (options.maxUploadSize) {
651+
headers['X-Parse-File-Max-Upload-Size'] = options.maxUploadSize.replace(/[\r\n]/g, '');
652+
}
635653
const jsKey = CoreManager.get('JAVASCRIPT_KEY');
636654
if (jsKey) {
637655
headers['X-Parse-JavaScript-Key'] = jsKey;

types/ParseFile.d.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,17 @@ export type FileSaveOptions = FullOptions & {
1010
metadata?: Record<string, any>;
1111
tags?: Record<string, any>;
1212
directory?: string;
13+
/**
14+
* Overrides the server's `maxUploadSize` for this file upload. Requires the
15+
* master key (`useMasterKey: true`). The value uses the same format as the
16+
* server option (e.g. `'50mb'`, `'1gb'`).
17+
*
18+
* Only supported for Buffer and Stream source types. Files created from
19+
* base64 strings, number arrays, Blobs, or URIs do not support this option.
20+
*
21+
* Requires Parse Server >= 9.5.0.
22+
*/
23+
maxUploadSize?: string;
1324
};
1425
export type FileSource = {
1526
format: 'file';
@@ -171,6 +182,10 @@ declare class ParseFile {
171182
* }
172183
* });
173184
* </pre>
185+
* <li>maxUploadSize: Overrides the server's maxUploadSize for this upload.
186+
* Requires the master key. Only supported for Buffer and Stream source
187+
* types; files created from base64 strings, number arrays, Blobs, or URIs
188+
* do not support this option. Requires Parse Server >= 9.5.0.
174189
* </ul>
175190
* @returns {Promise | undefined} Promise that is resolved when the save finishes.
176191
*/

0 commit comments

Comments
 (0)