@wordpress/media-utils 5.31.1-next.f56bd8138.0 → 5.32.1-next.47f435fc9.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (70) hide show
  1. package/CHANGELOG.md +2 -0
  2. package/build/components/index.js +36 -11
  3. package/build/components/index.js.map +7 -1
  4. package/build/components/media-upload/index.js +156 -221
  5. package/build/components/media-upload/index.js.map +7 -1
  6. package/build/index.js +44 -65
  7. package/build/index.js.map +7 -1
  8. package/build/lock-unlock.js +31 -14
  9. package/build/lock-unlock.js.map +7 -1
  10. package/build/private-apis.js +31 -17
  11. package/build/private-apis.js.map +7 -1
  12. package/build/utils/flatten-form-data.js +28 -20
  13. package/build/utils/flatten-form-data.js.map +7 -1
  14. package/build/utils/get-mime-types-array.js +38 -20
  15. package/build/utils/get-mime-types-array.js.map +7 -1
  16. package/build/utils/sideload-media.js +50 -38
  17. package/build/utils/sideload-media.js.map +7 -1
  18. package/build/utils/sideload-to-server.js +53 -36
  19. package/build/utils/sideload-to-server.js.map +7 -1
  20. package/build/utils/transform-attachment.js +29 -23
  21. package/build/utils/transform-attachment.js.map +7 -1
  22. package/build/utils/types.js +16 -5
  23. package/build/utils/types.js.map +7 -1
  24. package/build/utils/upload-error.js +30 -21
  25. package/build/utils/upload-error.js.map +7 -1
  26. package/build/utils/upload-media.js +62 -71
  27. package/build/utils/upload-media.js.map +7 -1
  28. package/build/utils/upload-to-server.js +54 -27
  29. package/build/utils/upload-to-server.js.map +7 -1
  30. package/build/utils/validate-file-size.js +44 -32
  31. package/build/utils/validate-file-size.js.map +7 -1
  32. package/build/utils/validate-mime-type-for-user.js +42 -30
  33. package/build/utils/validate-mime-type-for-user.js.map +7 -1
  34. package/build/utils/validate-mime-type.js +37 -32
  35. package/build/utils/validate-mime-type.js.map +7 -1
  36. package/build-module/components/index.js +5 -2
  37. package/build-module/components/index.js.map +7 -1
  38. package/build-module/components/media-upload/index.js +137 -215
  39. package/build-module/components/media-upload/index.js.map +7 -1
  40. package/build-module/index.js +16 -8
  41. package/build-module/index.js.map +7 -1
  42. package/build-module/lock-unlock.js +8 -7
  43. package/build-module/lock-unlock.js.map +7 -1
  44. package/build-module/private-apis.js +7 -11
  45. package/build-module/private-apis.js.map +7 -1
  46. package/build-module/utils/flatten-form-data.js +7 -17
  47. package/build-module/utils/flatten-form-data.js.map +7 -1
  48. package/build-module/utils/get-mime-types-array.js +17 -17
  49. package/build-module/utils/get-mime-types-array.js.map +7 -1
  50. package/build-module/utils/sideload-media.js +28 -34
  51. package/build-module/utils/sideload-media.js.map +7 -1
  52. package/build-module/utils/sideload-to-server.js +22 -33
  53. package/build-module/utils/sideload-to-server.js.map +7 -1
  54. package/build-module/utils/transform-attachment.js +8 -20
  55. package/build-module/utils/transform-attachment.js.map +7 -1
  56. package/build-module/utils/types.js +1 -2
  57. package/build-module/utils/types.js.map +7 -1
  58. package/build-module/utils/upload-error.js +9 -17
  59. package/build-module/utils/upload-error.js.map +7 -1
  60. package/build-module/utils/upload-media.js +36 -63
  61. package/build-module/utils/upload-media.js.map +7 -1
  62. package/build-module/utils/upload-to-server.js +23 -22
  63. package/build-module/utils/upload-to-server.js.map +7 -1
  64. package/build-module/utils/validate-file-size.js +19 -25
  65. package/build-module/utils/validate-file-size.js.map +7 -1
  66. package/build-module/utils/validate-mime-type-for-user.js +18 -24
  67. package/build-module/utils/validate-mime-type-for-user.js.map +7 -1
  68. package/build-module/utils/validate-mime-type.js +14 -27
  69. package/build-module/utils/validate-mime-type.js.map +7 -1
  70. package/package.json +15 -8
@@ -1,22 +1,14 @@
1
- /**
2
- * MediaError class.
3
- *
4
- * Small wrapper around the `Error` class
5
- * to hold an error code and a reference to a file object.
6
- */
7
- export class UploadError extends Error {
8
- constructor({
9
- code,
10
- message,
11
- file,
12
- cause
13
- }) {
14
- super(message, {
15
- cause
16
- });
1
+ class UploadError extends Error {
2
+ code;
3
+ file;
4
+ constructor({ code, message, file, cause }) {
5
+ super(message, { cause });
17
6
  Object.setPrototypeOf(this, new.target.prototype);
18
7
  this.code = code;
19
8
  this.file = file;
20
9
  }
21
10
  }
22
- //# sourceMappingURL=upload-error.js.map
11
+ export {
12
+ UploadError
13
+ };
14
+ //# sourceMappingURL=upload-error.js.map
@@ -1 +1,7 @@
1
- {"version":3,"names":["UploadError","Error","constructor","code","message","file","cause","Object","setPrototypeOf","new","target","prototype"],"sources":["@wordpress/media-utils/src/utils/upload-error.ts"],"sourcesContent":["interface UploadErrorArgs {\n\tcode: string;\n\tmessage: string;\n\tfile: File;\n\tcause?: Error;\n}\n\n/**\n * MediaError class.\n *\n * Small wrapper around the `Error` class\n * to hold an error code and a reference to a file object.\n */\nexport class UploadError extends Error {\n\tcode: string;\n\tfile: File;\n\n\tconstructor( { code, message, file, cause }: UploadErrorArgs ) {\n\t\tsuper( message, { cause } );\n\n\t\tObject.setPrototypeOf( this, new.target.prototype );\n\n\t\tthis.code = code;\n\t\tthis.file = file;\n\t}\n}\n"],"mappings":"AAOA;AACA;AACA;AACA;AACA;AACA;AACA,OAAO,MAAMA,WAAW,SAASC,KAAK,CAAC;EAItCC,WAAWA,CAAE;IAAEC,IAAI;IAAEC,OAAO;IAAEC,IAAI;IAAEC;EAAuB,CAAC,EAAG;IAC9D,KAAK,CAAEF,OAAO,EAAE;MAAEE;IAAM,CAAE,CAAC;IAE3BC,MAAM,CAACC,cAAc,CAAE,IAAI,EAAEC,GAAG,CAACC,MAAM,CAACC,SAAU,CAAC;IAEnD,IAAI,CAACR,IAAI,GAAGA,IAAI;IAChB,IAAI,CAACE,IAAI,GAAGA,IAAI;EACjB;AACD","ignoreList":[]}
1
+ {
2
+ "version": 3,
3
+ "sources": ["../../src/utils/upload-error.ts"],
4
+ "sourcesContent": ["interface UploadErrorArgs {\n\tcode: string;\n\tmessage: string;\n\tfile: File;\n\tcause?: Error;\n}\n\n/**\n * MediaError class.\n *\n * Small wrapper around the `Error` class\n * to hold an error code and a reference to a file object.\n */\nexport class UploadError extends Error {\n\tcode: string;\n\tfile: File;\n\n\tconstructor( { code, message, file, cause }: UploadErrorArgs ) {\n\t\tsuper( message, { cause } );\n\n\t\tObject.setPrototypeOf( this, new.target.prototype );\n\n\t\tthis.code = code;\n\t\tthis.file = file;\n\t}\n}\n"],
5
+ "mappings": "AAaO,MAAM,oBAAoB,MAAM;AAAA,EACtC;AAAA,EACA;AAAA,EAEA,YAAa,EAAE,MAAM,SAAS,MAAM,MAAM,GAAqB;AAC9D,UAAO,SAAS,EAAE,MAAM,CAAE;AAE1B,WAAO,eAAgB,MAAM,WAAW,SAAU;AAElD,SAAK,OAAO;AACZ,SAAK,OAAO;AAAA,EACb;AACD;",
6
+ "names": []
7
+ }
@@ -1,34 +1,11 @@
1
- /**
2
- * WordPress dependencies
3
- */
4
- import { __, sprintf } from '@wordpress/i18n';
5
- import { createBlobURL, revokeBlobURL } from '@wordpress/blob';
6
-
7
- /**
8
- * Internal dependencies
9
- */
10
-
11
- import { uploadToServer } from './upload-to-server';
12
- import { validateMimeType } from './validate-mime-type';
13
- import { validateMimeTypeForUser } from './validate-mime-type-for-user';
14
- import { validateFileSize } from './validate-file-size';
15
- import { UploadError } from './upload-error';
16
- /**
17
- * Upload a media file when the file upload button is activated
18
- * or when adding a file to the editor via drag & drop.
19
- *
20
- * @param $0 Parameters object passed to the function.
21
- * @param $0.allowedTypes Array with the types of media that can be uploaded, if unset all types are allowed.
22
- * @param $0.additionalData Additional data to include in the request.
23
- * @param $0.filesList List of files.
24
- * @param $0.maxUploadFileSize Maximum upload size in bytes allowed for the site.
25
- * @param $0.onError Function called when an error happens.
26
- * @param $0.onFileChange Function called each time a file or a temporary representation of the file is available.
27
- * @param $0.wpAllowedMimeTypes List of allowed mime types and file extensions.
28
- * @param $0.signal Abort signal.
29
- * @param $0.multiple Whether to allow multiple files to be uploaded.
30
- */
31
- export function uploadMedia({
1
+ import { __, sprintf } from "@wordpress/i18n";
2
+ import { createBlobURL, revokeBlobURL } from "@wordpress/blob";
3
+ import { uploadToServer } from "./upload-to-server";
4
+ import { validateMimeType } from "./validate-mime-type";
5
+ import { validateMimeTypeForUser } from "./validate-mime-type-for-user";
6
+ import { validateFileSize } from "./validate-file-size";
7
+ import { UploadError } from "./upload-error";
8
+ function uploadMedia({
32
9
  wpAllowedMimeTypes,
33
10
  allowedTypes,
34
11
  additionalData = {},
@@ -40,41 +17,35 @@ export function uploadMedia({
40
17
  multiple = true
41
18
  }) {
42
19
  if (!multiple && filesList.length > 1) {
43
- onError?.(new Error(__('Only one file can be used here.')));
20
+ onError?.(new Error(__("Only one file can be used here.")));
44
21
  return;
45
22
  }
46
23
  const validFiles = [];
47
24
  const filesSet = [];
48
25
  const setAndUpdateFiles = (index, value) => {
49
- // For client-side media processing, this is handled by the upload-media package.
50
26
  if (!window.__experimentalMediaProcessing) {
51
27
  if (filesSet[index]?.url) {
52
28
  revokeBlobURL(filesSet[index].url);
53
29
  }
54
30
  }
55
31
  filesSet[index] = value;
56
- onFileChange?.(filesSet.filter(attachment => attachment !== null));
32
+ onFileChange?.(
33
+ filesSet.filter((attachment) => attachment !== null)
34
+ );
57
35
  };
58
36
  for (const mediaFile of filesList) {
59
- // Verify if user is allowed to upload this mime type.
60
- // Defer to the server when type not detected.
61
37
  try {
62
38
  validateMimeTypeForUser(mediaFile, wpAllowedMimeTypes);
63
39
  } catch (error) {
64
40
  onError?.(error);
65
41
  continue;
66
42
  }
67
-
68
- // Check if the caller (e.g. a block) supports this mime type.
69
- // Defer to the server when type not detected.
70
43
  try {
71
44
  validateMimeType(mediaFile, allowedTypes);
72
45
  } catch (error) {
73
46
  onError?.(error);
74
47
  continue;
75
48
  }
76
-
77
- // Verify if file is greater than the maximum file upload size allowed for the site.
78
49
  try {
79
50
  validateFileSize(mediaFile, maxUploadFileSize);
80
51
  } catch (error) {
@@ -82,41 +53,43 @@ export function uploadMedia({
82
53
  continue;
83
54
  }
84
55
  validFiles.push(mediaFile);
85
-
86
- // For client-side media processing, this is handled by the upload-media package.
87
56
  if (!window.__experimentalMediaProcessing) {
88
- // Set temporary URL to create placeholder media file, this is replaced
89
- // with final file from media gallery when upload is `done` below.
90
- filesSet.push({
91
- url: createBlobURL(mediaFile)
92
- });
57
+ filesSet.push({ url: createBlobURL(mediaFile) });
93
58
  onFileChange?.(filesSet);
94
59
  }
95
60
  }
96
61
  validFiles.map(async (file, index) => {
97
62
  try {
98
- const attachment = await uploadToServer(file, additionalData, signal);
63
+ const attachment = await uploadToServer(
64
+ file,
65
+ additionalData,
66
+ signal
67
+ );
99
68
  setAndUpdateFiles(index, attachment);
100
69
  } catch (error) {
101
- // Reset to empty on failure.
102
70
  setAndUpdateFiles(index, null);
103
-
104
- // @wordpress/api-fetch throws any response that isn't in the 200 range as-is.
105
71
  let message;
106
- if (typeof error === 'object' && error !== null && 'message' in error) {
107
- message = typeof error.message === 'string' ? error.message : String(error.message);
72
+ if (typeof error === "object" && error !== null && "message" in error) {
73
+ message = typeof error.message === "string" ? error.message : String(error.message);
108
74
  } else {
109
75
  message = sprintf(
110
- // translators: %s: file name
111
- __('Error while uploading file %s to the media library.'), file.name);
76
+ // translators: %s: file name
77
+ __("Error while uploading file %s to the media library."),
78
+ file.name
79
+ );
112
80
  }
113
- onError?.(new UploadError({
114
- code: 'GENERAL',
115
- message,
116
- file,
117
- cause: error instanceof Error ? error : undefined
118
- }));
81
+ onError?.(
82
+ new UploadError({
83
+ code: "GENERAL",
84
+ message,
85
+ file,
86
+ cause: error instanceof Error ? error : void 0
87
+ })
88
+ );
119
89
  }
120
90
  });
121
91
  }
122
- //# sourceMappingURL=upload-media.js.map
92
+ export {
93
+ uploadMedia
94
+ };
95
+ //# sourceMappingURL=upload-media.js.map
@@ -1 +1,7 @@
1
- {"version":3,"names":["__","sprintf","createBlobURL","revokeBlobURL","uploadToServer","validateMimeType","validateMimeTypeForUser","validateFileSize","UploadError","uploadMedia","wpAllowedMimeTypes","allowedTypes","additionalData","filesList","maxUploadFileSize","onError","onFileChange","signal","multiple","length","Error","validFiles","filesSet","setAndUpdateFiles","index","value","window","__experimentalMediaProcessing","url","filter","attachment","mediaFile","error","push","map","file","message","String","name","code","cause","undefined"],"sources":["@wordpress/media-utils/src/utils/upload-media.ts"],"sourcesContent":["/**\n * WordPress dependencies\n */\nimport { __, sprintf } from '@wordpress/i18n';\nimport { createBlobURL, revokeBlobURL } from '@wordpress/blob';\n\n/**\n * Internal dependencies\n */\nimport type {\n\tAdditionalData,\n\tAttachment,\n\tOnChangeHandler,\n\tOnErrorHandler,\n} from './types';\nimport { uploadToServer } from './upload-to-server';\nimport { validateMimeType } from './validate-mime-type';\nimport { validateMimeTypeForUser } from './validate-mime-type-for-user';\nimport { validateFileSize } from './validate-file-size';\nimport { UploadError } from './upload-error';\n\ndeclare global {\n\tinterface Window {\n\t\t__experimentalMediaProcessing?: boolean;\n\t}\n}\n\ninterface UploadMediaArgs {\n\t// Additional data to include in the request.\n\tadditionalData?: AdditionalData;\n\t// Array with the types of media that can be uploaded, if unset all types are allowed.\n\tallowedTypes?: string[];\n\t// List of files.\n\tfilesList: File[];\n\t// Maximum upload size in bytes allowed for the site.\n\tmaxUploadFileSize?: number;\n\t// Function called when an error happens.\n\tonError?: OnErrorHandler;\n\t// Function called each time a file or a temporary representation of the file is available.\n\tonFileChange?: OnChangeHandler;\n\t// List of allowed mime types and file extensions.\n\twpAllowedMimeTypes?: Record< string, string > | null;\n\t// Abort signal.\n\tsignal?: AbortSignal;\n\t// Whether to allow multiple files to be uploaded.\n\tmultiple?: boolean;\n}\n\n/**\n * Upload a media file when the file upload button is activated\n * or when adding a file to the editor via drag & drop.\n *\n * @param $0 Parameters object passed to the function.\n * @param $0.allowedTypes Array with the types of media that can be uploaded, if unset all types are allowed.\n * @param $0.additionalData Additional data to include in the request.\n * @param $0.filesList List of files.\n * @param $0.maxUploadFileSize Maximum upload size in bytes allowed for the site.\n * @param $0.onError Function called when an error happens.\n * @param $0.onFileChange Function called each time a file or a temporary representation of the file is available.\n * @param $0.wpAllowedMimeTypes List of allowed mime types and file extensions.\n * @param $0.signal Abort signal.\n * @param $0.multiple Whether to allow multiple files to be uploaded.\n */\nexport function uploadMedia( {\n\twpAllowedMimeTypes,\n\tallowedTypes,\n\tadditionalData = {},\n\tfilesList,\n\tmaxUploadFileSize,\n\tonError,\n\tonFileChange,\n\tsignal,\n\tmultiple = true,\n}: UploadMediaArgs ) {\n\tif ( ! multiple && filesList.length > 1 ) {\n\t\tonError?.( new Error( __( 'Only one file can be used here.' ) ) );\n\t\treturn;\n\t}\n\n\tconst validFiles = [];\n\n\tconst filesSet: Array< Partial< Attachment > | null > = [];\n\tconst setAndUpdateFiles = ( index: number, value: Attachment | null ) => {\n\t\t// For client-side media processing, this is handled by the upload-media package.\n\t\tif ( ! window.__experimentalMediaProcessing ) {\n\t\t\tif ( filesSet[ index ]?.url ) {\n\t\t\t\trevokeBlobURL( filesSet[ index ].url );\n\t\t\t}\n\t\t}\n\t\tfilesSet[ index ] = value;\n\t\tonFileChange?.(\n\t\t\tfilesSet.filter( ( attachment ) => attachment !== null )\n\t\t);\n\t};\n\n\tfor ( const mediaFile of filesList ) {\n\t\t// Verify if user is allowed to upload this mime type.\n\t\t// Defer to the server when type not detected.\n\t\ttry {\n\t\t\tvalidateMimeTypeForUser( mediaFile, wpAllowedMimeTypes );\n\t\t} catch ( error: unknown ) {\n\t\t\tonError?.( error as Error );\n\t\t\tcontinue;\n\t\t}\n\n\t\t// Check if the caller (e.g. a block) supports this mime type.\n\t\t// Defer to the server when type not detected.\n\t\ttry {\n\t\t\tvalidateMimeType( mediaFile, allowedTypes );\n\t\t} catch ( error: unknown ) {\n\t\t\tonError?.( error as Error );\n\t\t\tcontinue;\n\t\t}\n\n\t\t// Verify if file is greater than the maximum file upload size allowed for the site.\n\t\ttry {\n\t\t\tvalidateFileSize( mediaFile, maxUploadFileSize );\n\t\t} catch ( error: unknown ) {\n\t\t\tonError?.( error as Error );\n\t\t\tcontinue;\n\t\t}\n\n\t\tvalidFiles.push( mediaFile );\n\n\t\t// For client-side media processing, this is handled by the upload-media package.\n\t\tif ( ! window.__experimentalMediaProcessing ) {\n\t\t\t// Set temporary URL to create placeholder media file, this is replaced\n\t\t\t// with final file from media gallery when upload is `done` below.\n\t\t\tfilesSet.push( { url: createBlobURL( mediaFile ) } );\n\t\t\tonFileChange?.( filesSet as Array< Partial< Attachment > > );\n\t\t}\n\t}\n\n\tvalidFiles.map( async ( file, index ) => {\n\t\ttry {\n\t\t\tconst attachment = await uploadToServer(\n\t\t\t\tfile,\n\t\t\t\tadditionalData,\n\t\t\t\tsignal\n\t\t\t);\n\t\t\tsetAndUpdateFiles( index, attachment );\n\t\t} catch ( error ) {\n\t\t\t// Reset to empty on failure.\n\t\t\tsetAndUpdateFiles( index, null );\n\n\t\t\t// @wordpress/api-fetch throws any response that isn't in the 200 range as-is.\n\t\t\tlet message: string;\n\t\t\tif (\n\t\t\t\ttypeof error === 'object' &&\n\t\t\t\terror !== null &&\n\t\t\t\t'message' in error\n\t\t\t) {\n\t\t\t\tmessage =\n\t\t\t\t\ttypeof error.message === 'string'\n\t\t\t\t\t\t? error.message\n\t\t\t\t\t\t: String( error.message );\n\t\t\t} else {\n\t\t\t\tmessage = sprintf(\n\t\t\t\t\t// translators: %s: file name\n\t\t\t\t\t__( 'Error while uploading file %s to the media library.' ),\n\t\t\t\t\tfile.name\n\t\t\t\t);\n\t\t\t}\n\n\t\t\tonError?.(\n\t\t\t\tnew UploadError( {\n\t\t\t\t\tcode: 'GENERAL',\n\t\t\t\t\tmessage,\n\t\t\t\t\tfile,\n\t\t\t\t\tcause: error instanceof Error ? error : undefined,\n\t\t\t\t} )\n\t\t\t);\n\t\t}\n\t} );\n}\n"],"mappings":"AAAA;AACA;AACA;AACA,SAASA,EAAE,EAAEC,OAAO,QAAQ,iBAAiB;AAC7C,SAASC,aAAa,EAAEC,aAAa,QAAQ,iBAAiB;;AAE9D;AACA;AACA;;AAOA,SAASC,cAAc,QAAQ,oBAAoB;AACnD,SAASC,gBAAgB,QAA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IAAIP,WAAW,CAAE;QAChB+B,IAAI,EAAE,SAAS;QACfH,OAAO;QACPD,IAAI;QACJK,KAAK,EAAER,KAAK,YAAYZ,KAAK,GAAGY,KAAK,GAAGS;MACzC,CAAE,CACH,CAAC;IACF;EACD,CAAE,CAAC;AACJ","ignoreList":[]}
1
+ {
2
+ "version": 3,
3
+ "sources": ["../../src/utils/upload-media.ts"],
4
+ "sourcesContent": ["/**\n * WordPress dependencies\n */\nimport { __, sprintf } from '@wordpress/i18n';\nimport { createBlobURL, revokeBlobURL } from '@wordpress/blob';\n\n/**\n * Internal dependencies\n */\nimport type {\n\tAdditionalData,\n\tAttachment,\n\tOnChangeHandler,\n\tOnErrorHandler,\n} from './types';\nimport { uploadToServer } from './upload-to-server';\nimport { validateMimeType } from './validate-mime-type';\nimport { validateMimeTypeForUser } from './validate-mime-type-for-user';\nimport { validateFileSize } from './validate-file-size';\nimport { UploadError } from './upload-error';\n\ndeclare global {\n\tinterface Window {\n\t\t__experimentalMediaProcessing?: boolean;\n\t}\n}\n\ninterface UploadMediaArgs {\n\t// Additional data to include in the request.\n\tadditionalData?: AdditionalData;\n\t// Array with the types of media that can be uploaded, if unset all types are allowed.\n\tallowedTypes?: string[];\n\t// List of files.\n\tfilesList: File[];\n\t// Maximum upload size in bytes allowed for the site.\n\tmaxUploadFileSize?: number;\n\t// Function called when an error happens.\n\tonError?: OnErrorHandler;\n\t// Function called each time a file or a temporary representation of the file is available.\n\tonFileChange?: OnChangeHandler;\n\t// List of allowed mime types and file extensions.\n\twpAllowedMimeTypes?: Record< string, string > | null;\n\t// Abort signal.\n\tsignal?: AbortSignal;\n\t// Whether to allow multiple files to be uploaded.\n\tmultiple?: boolean;\n}\n\n/**\n * Upload a media file when the file upload button is activated\n * or when adding a file to the editor via drag & drop.\n *\n * @param $0 Parameters object passed to the function.\n * @param $0.allowedTypes Array with the types of media that can be uploaded, if unset all types are allowed.\n * @param $0.additionalData Additional data to include in the request.\n * @param $0.filesList List of files.\n * @param $0.maxUploadFileSize Maximum upload size in bytes allowed for the site.\n * @param $0.onError Function called when an error happens.\n * @param $0.onFileChange Function called each time a file or a temporary representation of the file is available.\n * @param $0.wpAllowedMimeTypes List of allowed mime types and file extensions.\n * @param $0.signal Abort signal.\n * @param $0.multiple Whether to allow multiple files to be uploaded.\n */\nexport function uploadMedia( {\n\twpAllowedMimeTypes,\n\tallowedTypes,\n\tadditionalData = {},\n\tfilesList,\n\tmaxUploadFileSize,\n\tonError,\n\tonFileChange,\n\tsignal,\n\tmultiple = true,\n}: UploadMediaArgs ) {\n\tif ( ! multiple && filesList.length > 1 ) {\n\t\tonError?.( new Error( __( 'Only one file can be used here.' ) ) );\n\t\treturn;\n\t}\n\n\tconst validFiles = [];\n\n\tconst filesSet: Array< Partial< Attachment > | null > = [];\n\tconst setAndUpdateFiles = ( index: number, value: Attachment | null ) => {\n\t\t// For client-side media processing, this is handled by the upload-media package.\n\t\tif ( ! window.__experimentalMediaProcessing ) {\n\t\t\tif ( filesSet[ index ]?.url ) {\n\t\t\t\trevokeBlobURL( filesSet[ index ].url );\n\t\t\t}\n\t\t}\n\t\tfilesSet[ index ] = value;\n\t\tonFileChange?.(\n\t\t\tfilesSet.filter( ( attachment ) => attachment !== null )\n\t\t);\n\t};\n\n\tfor ( const mediaFile of filesList ) {\n\t\t// Verify if user is allowed to upload this mime type.\n\t\t// Defer to the server when type not detected.\n\t\ttry {\n\t\t\tvalidateMimeTypeForUser( mediaFile, wpAllowedMimeTypes );\n\t\t} catch ( error: unknown ) {\n\t\t\tonError?.( error as Error );\n\t\t\tcontinue;\n\t\t}\n\n\t\t// Check if the caller (e.g. a block) supports this mime type.\n\t\t// Defer to the server when type not detected.\n\t\ttry {\n\t\t\tvalidateMimeType( mediaFile, allowedTypes );\n\t\t} catch ( error: unknown ) {\n\t\t\tonError?.( error as Error );\n\t\t\tcontinue;\n\t\t}\n\n\t\t// Verify if file is greater than the maximum file upload size allowed for the site.\n\t\ttry {\n\t\t\tvalidateFileSize( mediaFile, maxUploadFileSize );\n\t\t} catch ( error: unknown ) {\n\t\t\tonError?.( error as Error );\n\t\t\tcontinue;\n\t\t}\n\n\t\tvalidFiles.push( mediaFile );\n\n\t\t// For client-side media processing, this is handled by the upload-media package.\n\t\tif ( ! window.__experimentalMediaProcessing ) {\n\t\t\t// Set temporary URL to create placeholder media file, this is replaced\n\t\t\t// with final file from media gallery when upload is `done` below.\n\t\t\tfilesSet.push( { url: createBlobURL( mediaFile ) } );\n\t\t\tonFileChange?.( filesSet as Array< Partial< Attachment > > );\n\t\t}\n\t}\n\n\tvalidFiles.map( async ( file, index ) => {\n\t\ttry {\n\t\t\tconst attachment = await uploadToServer(\n\t\t\t\tfile,\n\t\t\t\tadditionalData,\n\t\t\t\tsignal\n\t\t\t);\n\t\t\tsetAndUpdateFiles( index, attachment );\n\t\t} catch ( error ) {\n\t\t\t// Reset to empty on failure.\n\t\t\tsetAndUpdateFiles( index, null );\n\n\t\t\t// @wordpress/api-fetch throws any response that isn't in the 200 range as-is.\n\t\t\tlet message: string;\n\t\t\tif (\n\t\t\t\ttypeof error === 'object' &&\n\t\t\t\terror !== null &&\n\t\t\t\t'message' in error\n\t\t\t) {\n\t\t\t\tmessage =\n\t\t\t\t\ttypeof error.message === 'string'\n\t\t\t\t\t\t? error.message\n\t\t\t\t\t\t: String( error.message );\n\t\t\t} else {\n\t\t\t\tmessage = sprintf(\n\t\t\t\t\t// translators: %s: file name\n\t\t\t\t\t__( 'Error while uploading file %s to the media library.' ),\n\t\t\t\t\tfile.name\n\t\t\t\t);\n\t\t\t}\n\n\t\t\tonError?.(\n\t\t\t\tnew UploadError( {\n\t\t\t\t\tcode: 'GENERAL',\n\t\t\t\t\tmessage,\n\t\t\t\t\tfile,\n\t\t\t\t\tcause: error instanceof Error ? error : undefined,\n\t\t\t\t} )\n\t\t\t);\n\t\t}\n\t} );\n}\n"],
5
+ "mappings": "AAGA,SAAS,IAAI,eAAe;AAC5B,SAAS,eAAe,qBAAqB;AAW7C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,YAAM,aAAa,MAAM;AAAA,QACxB;AAAA,QACA;AAAA,QACA;AAAA,MACD;AACA,wBAAmB,OAAO,UAAW;AAAA,IACtC,SAAU,OAAQ;AAEjB,wBAAmB,OAAO,IAAK;AAG/B,UAAI;AACJ,UACC,OAAO,UAAU,YACjB,UAAU,QACV,aAAa,OACZ;AACD,kBACC,OAAO,MAAM,YAAY,WACtB,MAAM,UACN,OAAQ,MAAM,OAAQ;AAAA,MAC3B,OAAO;AACN,kBAAU;AAAA;AAAA,UAET,GAAI,qDAAsD;AAAA,UAC1D,KAAK;AAAA,QACN;AAAA,MACD;AAEA;AAAA,QACC,IAAI,YAAa;AAAA,UAChB,MAAM;AAAA,UACN;AAAA,UACA;AAAA,UACA,OAAO,iBAAiB,QAAQ,QAAQ;AAAA,QACzC,CAAE;AAAA,MACH;AAAA,IACD;AAAA,EACD,CAAE;AACH;",
6
+ "names": []
7
+ }
@@ -1,26 +1,27 @@
1
- /**
2
- * WordPress dependencies
3
- */
4
- import apiFetch from '@wordpress/api-fetch';
5
-
6
- /**
7
- * Internal dependencies
8
- */
9
- import { flattenFormData } from './flatten-form-data';
10
- import { transformAttachment } from './transform-attachment';
11
- export async function uploadToServer(file, additionalData = {}, signal) {
12
- // Create upload payload.
1
+ import apiFetch from "@wordpress/api-fetch";
2
+ import { flattenFormData } from "./flatten-form-data";
3
+ import { transformAttachment } from "./transform-attachment";
4
+ async function uploadToServer(file, additionalData = {}, signal) {
13
5
  const data = new FormData();
14
- data.append('file', file, file.name || file.type.replace('/', '.'));
6
+ data.append("file", file, file.name || file.type.replace("/", "."));
15
7
  for (const [key, value] of Object.entries(additionalData)) {
16
- flattenFormData(data, key, value);
8
+ flattenFormData(
9
+ data,
10
+ key,
11
+ value
12
+ );
17
13
  }
18
- return transformAttachment(await apiFetch({
19
- // This allows the video block to directly get a video's poster image.
20
- path: '/wp/v2/media?_embed=wp:featuredmedia',
21
- body: data,
22
- method: 'POST',
23
- signal
24
- }));
14
+ return transformAttachment(
15
+ await apiFetch({
16
+ // This allows the video block to directly get a video's poster image.
17
+ path: "/wp/v2/media?_embed=wp:featuredmedia",
18
+ body: data,
19
+ method: "POST",
20
+ signal
21
+ })
22
+ );
25
23
  }
26
- //# sourceMappingURL=upload-to-server.js.map
24
+ export {
25
+ uploadToServer
26
+ };
27
+ //# sourceMappingURL=upload-to-server.js.map
@@ -1 +1,7 @@
1
- {"version":3,"names":["apiFetch","flattenFormData","transformAttachment","uploadToServer","file","additionalData","signal","data","FormData","append","name","type","replace","key","value","Object","entries","path","body","method"],"sources":["@wordpress/media-utils/src/utils/upload-to-server.ts"],"sourcesContent":["/**\n * WordPress dependencies\n */\nimport apiFetch from '@wordpress/api-fetch';\n\n/**\n * Internal dependencies\n */\nimport { flattenFormData } from './flatten-form-data';\nimport { transformAttachment } from './transform-attachment';\nimport type { CreateRestAttachment, RestAttachment } from './types';\n\nexport async function uploadToServer(\n\tfile: File,\n\tadditionalData: CreateRestAttachment = {},\n\tsignal?: AbortSignal\n) {\n\t// Create upload payload.\n\tconst data = new FormData();\n\tdata.append( 'file', file, file.name || file.type.replace( '/', '.' ) );\n\tfor ( const [ key, value ] of Object.entries( additionalData ) ) {\n\t\tflattenFormData(\n\t\t\tdata,\n\t\t\tkey,\n\t\t\tvalue as string | Record< string, string > | undefined\n\t\t);\n\t}\n\n\treturn transformAttachment(\n\t\tawait apiFetch< RestAttachment >( {\n\t\t\t// This allows the video block to directly get a video's poster image.\n\t\t\tpath: '/wp/v2/media?_embed=wp:featuredmedia',\n\t\t\tbody: data,\n\t\t\tmethod: 'POST',\n\t\t\tsignal,\n\t\t} )\n\t);\n}\n"],"mappings":"AAAA;AACA;AACA;AACA,OAAOA,QAAQ,MAAM,sBAAsB;;AAE3C;AACA;AACA;AACA,SAASC,eAAe,QAAQ,qBAAqB;AACrD,SAASC,mBAAmB,QAAQ,wBAAwB;AAG5D,OAAO,eAAeC,cAAcA,CACnCC,IAAU,EACVC,cAAoC,GAAG,CAAC,CAAC,EACzCC,MAAoB,EACnB;EACD;EACA,MAAMC,IAAI,GAAG,IAAIC,QAAQ,CAAC,CAAC;EAC3BD,IAAI,CAACE,MAAM,CAAE,MAAM,EAAEL,IAAI,EAAEA,IAAI,CAACM,IAAI,IAAIN,IAAI,CAACO,IAAI,CAACC,OAAO,CAAE,GAAG,EAAE,GAAI,CAAE,CAAC;EACvE,KAAM,MAAM,CAAEC,GAAG,EAAEC,KAAK,CAAE,IAAIC,MAAM,CAACC,OAAO,CAAEX,cAAe,CAAC,EAAG;IAChEJ,eAAe,CACdM,IAAI,EACJM,GAAG,EACHC,KACD,CAAC;EACF;EAEA,OAAOZ,mBAAmB,CACzB,MAAMF,QAAQ,CAAoB;IACjC;IACAiB,IAAI,EAAE,sCAAsC;IAC5CC,IAAI,EAAEX,IAAI;IACVY,MAAM,EAAE,MAAM;IACdb;EACD,CAAE,CACH,CAAC;AACF","ignoreList":[]}
1
+ {
2
+ "version": 3,
3
+ "sources": ["../../src/utils/upload-to-server.ts"],
4
+ "sourcesContent": ["/**\n * WordPress dependencies\n */\nimport apiFetch from '@wordpress/api-fetch';\n\n/**\n * Internal dependencies\n */\nimport { flattenFormData } from './flatten-form-data';\nimport { transformAttachment } from './transform-attachment';\nimport type { CreateRestAttachment, RestAttachment } from './types';\n\nexport async function uploadToServer(\n\tfile: File,\n\tadditionalData: CreateRestAttachment = {},\n\tsignal?: AbortSignal\n) {\n\t// Create upload payload.\n\tconst data = new FormData();\n\tdata.append( 'file', file, file.name || file.type.replace( '/', '.' ) );\n\tfor ( const [ key, value ] of Object.entries( additionalData ) ) {\n\t\tflattenFormData(\n\t\t\tdata,\n\t\t\tkey,\n\t\t\tvalue as string | Record< string, string > | undefined\n\t\t);\n\t}\n\n\treturn transformAttachment(\n\t\tawait apiFetch< RestAttachment >( {\n\t\t\t// This allows the video block to directly get a video's poster image.\n\t\t\tpath: '/wp/v2/media?_embed=wp:featuredmedia',\n\t\t\tbody: data,\n\t\t\tmethod: 'POST',\n\t\t\tsignal,\n\t\t} )\n\t);\n}\n"],
5
+ "mappings": "AAGA,OAAO,cAAc;AAKrB,SAAS,uBAAuB;AAChC,SAAS,2BAA2B;AAGpC,eAAsB,eACrB,MACA,iBAAuC,CAAC,GACxC,QACC;AAED,QAAM,OAAO,IAAI,SAAS;AAC1B,OAAK,OAAQ,QAAQ,MAAM,KAAK,QAAQ,KAAK,KAAK,QAAS,KAAK,GAAI,CAAE;AACtE,aAAY,CAAE,KAAK,KAAM,KAAK,OAAO,QAAS,cAAe,GAAI;AAChE;AAAA,MACC;AAAA,MACA;AAAA,MACA;AAAA,IACD;AAAA,EACD;AAEA,SAAO;AAAA,IACN,MAAM,SAA4B;AAAA;AAAA,MAEjC,MAAM;AAAA,MACN,MAAM;AAAA,MACN,QAAQ;AAAA,MACR;AAAA,IACD,CAAE;AAAA,EACH;AACD;",
6
+ "names": []
7
+ }
@@ -1,38 +1,32 @@
1
- /**
2
- * WordPress dependencies
3
- */
4
- import { __, sprintf } from '@wordpress/i18n';
5
-
6
- /**
7
- * Internal dependencies
8
- */
9
- import { UploadError } from './upload-error';
10
-
11
- /**
12
- * Verifies whether the file is within the file upload size limits for the site.
13
- *
14
- * @param file File object.
15
- * @param maxUploadFileSize Maximum upload size in bytes allowed for the site.
16
- */
17
- export function validateFileSize(file, maxUploadFileSize) {
18
- // Don't allow empty files to be uploaded.
1
+ import { __, sprintf } from "@wordpress/i18n";
2
+ import { UploadError } from "./upload-error";
3
+ function validateFileSize(file, maxUploadFileSize) {
19
4
  if (file.size <= 0) {
20
5
  throw new UploadError({
21
- code: 'EMPTY_FILE',
6
+ code: "EMPTY_FILE",
22
7
  message: sprintf(
23
- // translators: %s: file name.
24
- __('%s: This file is empty.'), file.name),
8
+ // translators: %s: file name.
9
+ __("%s: This file is empty."),
10
+ file.name
11
+ ),
25
12
  file
26
13
  });
27
14
  }
28
15
  if (maxUploadFileSize && file.size > maxUploadFileSize) {
29
16
  throw new UploadError({
30
- code: 'SIZE_ABOVE_LIMIT',
17
+ code: "SIZE_ABOVE_LIMIT",
31
18
  message: sprintf(
32
- // translators: %s: file name.
33
- __('%s: This file exceeds the maximum upload size for this site.'), file.name),
19
+ // translators: %s: file name.
20
+ __(
21
+ "%s: This file exceeds the maximum upload size for this site."
22
+ ),
23
+ file.name
24
+ ),
34
25
  file
35
26
  });
36
27
  }
37
28
  }
38
- //# sourceMappingURL=validate-file-size.js.map
29
+ export {
30
+ validateFileSize
31
+ };
32
+ //# sourceMappingURL=validate-file-size.js.map
@@ -1 +1,7 @@
1
- {"version":3,"names":["__","sprintf","UploadError","validateFileSize","file","maxUploadFileSize","size","code","message","name"],"sources":["@wordpress/media-utils/src/utils/validate-file-size.ts"],"sourcesContent":["/**\n * WordPress dependencies\n */\nimport { __, sprintf } from '@wordpress/i18n';\n\n/**\n * Internal dependencies\n */\nimport { UploadError } from './upload-error';\n\n/**\n * Verifies whether the file is within the file upload size limits for the site.\n *\n * @param file File object.\n * @param maxUploadFileSize Maximum upload size in bytes allowed for the site.\n */\nexport function validateFileSize( file: File, maxUploadFileSize?: number ) {\n\t// Don't allow empty files to be uploaded.\n\tif ( file.size <= 0 ) {\n\t\tthrow new UploadError( {\n\t\t\tcode: 'EMPTY_FILE',\n\t\t\tmessage: sprintf(\n\t\t\t\t// translators: %s: file name.\n\t\t\t\t__( '%s: This file is empty.' ),\n\t\t\t\tfile.name\n\t\t\t),\n\t\t\tfile,\n\t\t} );\n\t}\n\n\tif ( maxUploadFileSize && file.size > maxUploadFileSize ) {\n\t\tthrow new UploadError( {\n\t\t\tcode: 'SIZE_ABOVE_LIMIT',\n\t\t\tmessage: sprintf(\n\t\t\t\t// translators: %s: file name.\n\t\t\t\t__(\n\t\t\t\t\t'%s: This file exceeds the maximum upload size for this site.'\n\t\t\t\t),\n\t\t\t\tfile.name\n\t\t\t),\n\t\t\tfile,\n\t\t} );\n\t}\n}\n"],"mappings":"AAAA;AACA;AACA;AACA,SAASA,EAAE,EAAEC,OAAO,QAAQ,iBAAiB;;AAE7C;AACA;AACA;AACA,SAASC,WAAW,QAAQ,gBAAgB;;AAE5C;AACA;AACA;AACA;AACA;AACA;AACA,OAAO,SAASC,gBAAgBA,CAAEC,IAAU,EAAEC,iBAA0B,EAAG;EAC1E;EACA,IAAKD,IAAI,CAACE,IAAI,IAAI,CAAC,EAAG;IACrB,MAAM,IAAIJ,WAAW,CAAE;MACtBK,IAAI,EAAE,YAAY;MAClBC,OAAO,EAAEP,OAAO;MACf;MACAD,EAAE,CAAE,yBAA0B,CAAC,EAC/BI,IAAI,CAACK,IACN,CAAC;MACDL;IACD,CAAE,CAAC;EACJ;EAEA,IAAKC,iBAAiB,IAAID,IAAI,CAACE,IAAI,GAAGD,iBAAiB,EAAG;IACzD,MAAM,IAAIH,WAAW,CAAE;MACtBK,IAAI,EAAE,kBAAkB;MACxBC,OAAO,EAAEP,OAAO;MACf;MACAD,EAAE,CACD,8DACD,CAAC,EACDI,IAAI,CAACK,IACN,CAAC;MACDL;IACD,CAAE,CAAC;EACJ;AACD","ignoreList":[]}
1
+ {
2
+ "version": 3,
3
+ "sources": ["../../src/utils/validate-file-size.ts"],
4
+ "sourcesContent": ["/**\n * WordPress dependencies\n */\nimport { __, sprintf } from '@wordpress/i18n';\n\n/**\n * Internal dependencies\n */\nimport { UploadError } from './upload-error';\n\n/**\n * Verifies whether the file is within the file upload size limits for the site.\n *\n * @param file File object.\n * @param maxUploadFileSize Maximum upload size in bytes allowed for the site.\n */\nexport function validateFileSize( file: File, maxUploadFileSize?: number ) {\n\t// Don't allow empty files to be uploaded.\n\tif ( file.size <= 0 ) {\n\t\tthrow new UploadError( {\n\t\t\tcode: 'EMPTY_FILE',\n\t\t\tmessage: sprintf(\n\t\t\t\t// translators: %s: file name.\n\t\t\t\t__( '%s: This file is empty.' ),\n\t\t\t\tfile.name\n\t\t\t),\n\t\t\tfile,\n\t\t} );\n\t}\n\n\tif ( maxUploadFileSize && file.size > maxUploadFileSize ) {\n\t\tthrow new UploadError( {\n\t\t\tcode: 'SIZE_ABOVE_LIMIT',\n\t\t\tmessage: sprintf(\n\t\t\t\t// translators: %s: file name.\n\t\t\t\t__(\n\t\t\t\t\t'%s: This file exceeds the maximum upload size for this site.'\n\t\t\t\t),\n\t\t\t\tfile.name\n\t\t\t),\n\t\t\tfile,\n\t\t} );\n\t}\n}\n"],
5
+ "mappings": "AAGA,SAAS,IAAI,eAAe;AAK5B,SAAS,mBAAmB;AAQrB,SAAS,iBAAkB,MAAY,mBAA6B;AAE1E,MAAK,KAAK,QAAQ,GAAI;AACrB,UAAM,IAAI,YAAa;AAAA,MACtB,MAAM;AAAA,MACN,SAAS;AAAA;AAAA,QAER,GAAI,yBAA0B;AAAA,QAC9B,KAAK;AAAA,MACN;AAAA,MACA;AAAA,IACD,CAAE;AAAA,EACH;AAEA,MAAK,qBAAqB,KAAK,OAAO,mBAAoB;AACzD,UAAM,IAAI,YAAa;AAAA,MACtB,MAAM;AAAA,MACN,SAAS;AAAA;AAAA,QAER;AAAA,UACC;AAAA,QACD;AAAA,QACA,KAAK;AAAA,MACN;AAAA,MACA;AAAA,IACD,CAAE;AAAA,EACH;AACD;",
6
+ "names": []
7
+ }
@@ -1,35 +1,29 @@
1
- /**
2
- * WordPress dependencies
3
- */
4
- import { __, sprintf } from '@wordpress/i18n';
5
-
6
- /**
7
- * Internal dependencies
8
- */
9
- import { UploadError } from './upload-error';
10
- import { getMimeTypesArray } from './get-mime-types-array';
11
-
12
- /**
13
- * Verifies if the user is allowed to upload this mime type.
14
- *
15
- * @param file File object.
16
- * @param wpAllowedMimeTypes List of allowed mime types and file extensions.
17
- */
18
- export function validateMimeTypeForUser(file, wpAllowedMimeTypes) {
19
- // Allowed types for the current WP_User.
1
+ import { __, sprintf } from "@wordpress/i18n";
2
+ import { UploadError } from "./upload-error";
3
+ import { getMimeTypesArray } from "./get-mime-types-array";
4
+ function validateMimeTypeForUser(file, wpAllowedMimeTypes) {
20
5
  const allowedMimeTypesForUser = getMimeTypesArray(wpAllowedMimeTypes);
21
6
  if (!allowedMimeTypesForUser) {
22
7
  return;
23
8
  }
24
- const isAllowedMimeTypeForUser = allowedMimeTypesForUser.includes(file.type);
9
+ const isAllowedMimeTypeForUser = allowedMimeTypesForUser.includes(
10
+ file.type
11
+ );
25
12
  if (file.type && !isAllowedMimeTypeForUser) {
26
13
  throw new UploadError({
27
- code: 'MIME_TYPE_NOT_ALLOWED_FOR_USER',
14
+ code: "MIME_TYPE_NOT_ALLOWED_FOR_USER",
28
15
  message: sprintf(
29
- // translators: %s: file name.
30
- __('%s: Sorry, you are not allowed to upload this file type.'), file.name),
16
+ // translators: %s: file name.
17
+ __(
18
+ "%s: Sorry, you are not allowed to upload this file type."
19
+ ),
20
+ file.name
21
+ ),
31
22
  file
32
23
  });
33
24
  }
34
25
  }
35
- //# sourceMappingURL=validate-mime-type-for-user.js.map
26
+ export {
27
+ validateMimeTypeForUser
28
+ };
29
+ //# sourceMappingURL=validate-mime-type-for-user.js.map
@@ -1 +1,7 @@
1
- {"version":3,"names":["__","sprintf","UploadError","getMimeTypesArray","validateMimeTypeForUser","file","wpAllowedMimeTypes","allowedMimeTypesForUser","isAllowedMimeTypeForUser","includes","type","code","message","name"],"sources":["@wordpress/media-utils/src/utils/validate-mime-type-for-user.ts"],"sourcesContent":["/**\n * WordPress dependencies\n */\nimport { __, sprintf } from '@wordpress/i18n';\n\n/**\n * Internal dependencies\n */\nimport { UploadError } from './upload-error';\nimport { getMimeTypesArray } from './get-mime-types-array';\n\n/**\n * Verifies if the user is allowed to upload this mime type.\n *\n * @param file File object.\n * @param wpAllowedMimeTypes List of allowed mime types and file extensions.\n */\nexport function validateMimeTypeForUser(\n\tfile: File,\n\twpAllowedMimeTypes?: Record< string, string > | null\n) {\n\t// Allowed types for the current WP_User.\n\tconst allowedMimeTypesForUser = getMimeTypesArray( wpAllowedMimeTypes );\n\n\tif ( ! allowedMimeTypesForUser ) {\n\t\treturn;\n\t}\n\n\tconst isAllowedMimeTypeForUser = allowedMimeTypesForUser.includes(\n\t\tfile.type\n\t);\n\n\tif ( file.type && ! isAllowedMimeTypeForUser ) {\n\t\tthrow new UploadError( {\n\t\t\tcode: 'MIME_TYPE_NOT_ALLOWED_FOR_USER',\n\t\t\tmessage: sprintf(\n\t\t\t\t// translators: %s: file name.\n\t\t\t\t__(\n\t\t\t\t\t'%s: Sorry, you are not allowed to upload this file type.'\n\t\t\t\t),\n\t\t\t\tfile.name\n\t\t\t),\n\t\t\tfile,\n\t\t} );\n\t}\n}\n"],"mappings":"AAAA;AACA;AACA;AACA,SAASA,EAAE,EAAEC,OAAO,QAAQ,iBAAiB;;AAE7C;AACA;AACA;AACA,SAASC,WAAW,QAAQ,gBAAgB;AAC5C,SAASC,iBAAiB,QAAQ,wBAAwB;;AAE1D;AACA;AACA;AACA;AACA;AACA;AACA,OAAO,SAASC,uBAAuBA,CACtCC,IAAU,EACVC,kBAAoD,EACnD;EACD;EACA,MAAMC,uBAAuB,GAAGJ,iBAAiB,CAAEG,kBAAmB,CAAC;EAEvE,IAAK,CAAEC,uBAAuB,EAAG;IAChC;EACD;EAEA,MAAMC,wBAAwB,GAAGD,uBAAuB,CAACE,QAAQ,CAChEJ,IAAI,CAACK,IACN,CAAC;EAED,IAAKL,IAAI,CAACK,IAAI,IAAI,CAAEF,wBAAwB,EAAG;IAC9C,MAAM,IAAIN,WAAW,CAAE;MACtBS,IAAI,EAAE,gCAAgC;MACtCC,OAAO,EAAEX,OAAO;MACf;MACAD,EAAE,CACD,0DACD,CAAC,EACDK,IAAI,CAACQ,IACN,CAAC;MACDR;IACD,CAAE,CAAC;EACJ;AACD","ignoreList":[]}
1
+ {
2
+ "version": 3,
3
+ "sources": ["../../src/utils/validate-mime-type-for-user.ts"],
4
+ "sourcesContent": ["/**\n * WordPress dependencies\n */\nimport { __, sprintf } from '@wordpress/i18n';\n\n/**\n * Internal dependencies\n */\nimport { UploadError } from './upload-error';\nimport { getMimeTypesArray } from './get-mime-types-array';\n\n/**\n * Verifies if the user is allowed to upload this mime type.\n *\n * @param file File object.\n * @param wpAllowedMimeTypes List of allowed mime types and file extensions.\n */\nexport function validateMimeTypeForUser(\n\tfile: File,\n\twpAllowedMimeTypes?: Record< string, string > | null\n) {\n\t// Allowed types for the current WP_User.\n\tconst allowedMimeTypesForUser = getMimeTypesArray( wpAllowedMimeTypes );\n\n\tif ( ! allowedMimeTypesForUser ) {\n\t\treturn;\n\t}\n\n\tconst isAllowedMimeTypeForUser = allowedMimeTypesForUser.includes(\n\t\tfile.type\n\t);\n\n\tif ( file.type && ! isAllowedMimeTypeForUser ) {\n\t\tthrow new UploadError( {\n\t\t\tcode: 'MIME_TYPE_NOT_ALLOWED_FOR_USER',\n\t\t\tmessage: sprintf(\n\t\t\t\t// translators: %s: file name.\n\t\t\t\t__(\n\t\t\t\t\t'%s: Sorry, you are not allowed to upload this file type.'\n\t\t\t\t),\n\t\t\t\tfile.name\n\t\t\t),\n\t\t\tfile,\n\t\t} );\n\t}\n}\n"],
5
+ "mappings": "AAGA,SAAS,IAAI,eAAe;AAK5B,SAAS,mBAAmB;AAC5B,SAAS,yBAAyB;AAQ3B,SAAS,wBACf,MACA,oBACC;AAED,QAAM,0BAA0B,kBAAmB,kBAAmB;AAEtE,MAAK,CAAE,yBAA0B;AAChC;AAAA,EACD;AAEA,QAAM,2BAA2B,wBAAwB;AAAA,IACxD,KAAK;AAAA,EACN;AAEA,MAAK,KAAK,QAAQ,CAAE,0BAA2B;AAC9C,UAAM,IAAI,YAAa;AAAA,MACtB,MAAM;AAAA,MACN,SAAS;AAAA;AAAA,QAER;AAAA,UACC;AAAA,QACD;AAAA,QACA,KAAK;AAAA,MACN;AAAA,MACA;AAAA,IACD,CAAE;AAAA,EACH;AACD;",
6
+ "names": []
7
+ }
@@ -1,41 +1,28 @@
1
- /**
2
- * WordPress dependencies
3
- */
4
- import { __, sprintf } from '@wordpress/i18n';
5
-
6
- /**
7
- * Internal dependencies
8
- */
9
- import { UploadError } from './upload-error';
10
-
11
- /**
12
- * Verifies if the caller (e.g. a block) supports this mime type.
13
- *
14
- * @param file File object.
15
- * @param allowedTypes List of allowed mime types.
16
- */
17
- export function validateMimeType(file, allowedTypes) {
1
+ import { __, sprintf } from "@wordpress/i18n";
2
+ import { UploadError } from "./upload-error";
3
+ function validateMimeType(file, allowedTypes) {
18
4
  if (!allowedTypes) {
19
5
  return;
20
6
  }
21
-
22
- // Allowed type specified by consumer.
23
- const isAllowedType = allowedTypes.some(allowedType => {
24
- // If a complete mimetype is specified verify if it matches exactly the mime type of the file.
25
- if (allowedType.includes('/')) {
7
+ const isAllowedType = allowedTypes.some((allowedType) => {
8
+ if (allowedType.includes("/")) {
26
9
  return allowedType === file.type;
27
10
  }
28
- // Otherwise a general mime type is used, and we should verify if the file mimetype starts with it.
29
11
  return file.type.startsWith(`${allowedType}/`);
30
12
  });
31
13
  if (file.type && !isAllowedType) {
32
14
  throw new UploadError({
33
- code: 'MIME_TYPE_NOT_SUPPORTED',
15
+ code: "MIME_TYPE_NOT_SUPPORTED",
34
16
  message: sprintf(
35
- // translators: %s: file name.
36
- __('%s: Sorry, this file type is not supported here.'), file.name),
17
+ // translators: %s: file name.
18
+ __("%s: Sorry, this file type is not supported here."),
19
+ file.name
20
+ ),
37
21
  file
38
22
  });
39
23
  }
40
24
  }
41
- //# sourceMappingURL=validate-mime-type.js.map
25
+ export {
26
+ validateMimeType
27
+ };
28
+ //# sourceMappingURL=validate-mime-type.js.map
@@ -1 +1,7 @@
1
- {"version":3,"names":["__","sprintf","UploadError","validateMimeType","file","allowedTypes","isAllowedType","some","allowedType","includes","type","startsWith","code","message","name"],"sources":["@wordpress/media-utils/src/utils/validate-mime-type.ts"],"sourcesContent":["/**\n * WordPress dependencies\n */\nimport { __, sprintf } from '@wordpress/i18n';\n\n/**\n * Internal dependencies\n */\nimport { UploadError } from './upload-error';\n\n/**\n * Verifies if the caller (e.g. a block) supports this mime type.\n *\n * @param file File object.\n * @param allowedTypes List of allowed mime types.\n */\nexport function validateMimeType( file: File, allowedTypes?: string[] ) {\n\tif ( ! allowedTypes ) {\n\t\treturn;\n\t}\n\n\t// Allowed type specified by consumer.\n\tconst isAllowedType = allowedTypes.some( ( allowedType ) => {\n\t\t// If a complete mimetype is specified verify if it matches exactly the mime type of the file.\n\t\tif ( allowedType.includes( '/' ) ) {\n\t\t\treturn allowedType === file.type;\n\t\t}\n\t\t// Otherwise a general mime type is used, and we should verify if the file mimetype starts with it.\n\t\treturn file.type.startsWith( `${ allowedType }/` );\n\t} );\n\n\tif ( file.type && ! isAllowedType ) {\n\t\tthrow new UploadError( {\n\t\t\tcode: 'MIME_TYPE_NOT_SUPPORTED',\n\t\t\tmessage: sprintf(\n\t\t\t\t// translators: %s: file name.\n\t\t\t\t__( '%s: Sorry, this file type is not supported here.' ),\n\t\t\t\tfile.name\n\t\t\t),\n\t\t\tfile,\n\t\t} );\n\t}\n}\n"],"mappings":"AAAA;AACA;AACA;AACA,SAASA,EAAE,EAAEC,OAAO,QAAQ,iBAAiB;;AAE7C;AACA;AACA;AACA,SAASC,WAAW,QAAQ,gBAAgB;;AAE5C;AACA;AACA;AACA;AACA;AACA;AACA,OAAO,SAASC,gBAAgBA,CAAEC,IAAU,EAAEC,YAAuB,EAAG;EACvE,IAAK,CAAEA,YAAY,EAAG;IACrB;EACD;;EAEA;EACA,MAAMC,aAAa,GAAGD,YAAY,CAACE,IAAI,CAAIC,WAAW,IAAM;IAC3D;IACA,IAAKA,WAAW,CAACC,QAAQ,CAAE,GAAI,CAAC,EAAG;MAClC,OAAOD,WAAW,KAAKJ,IAAI,CAACM,IAAI;IACjC;IACA;IACA,OAAON,IAAI,CAACM,IAAI,CAACC,UAAU,CAAE,GAAIH,WAAW,GAAK,CAAC;EACnD,CAAE,CAAC;EAEH,IAAKJ,IAAI,CAACM,IAAI,IAAI,CAAEJ,aAAa,EAAG;IACnC,MAAM,IAAIJ,WAAW,CAAE;MACtBU,IAAI,EAAE,yBAAyB;MAC/BC,OAAO,EAAEZ,OAAO;MACf;MACAD,EAAE,CAAE,kDAAmD,CAAC,EACxDI,IAAI,CAACU,IACN,CAAC;MACDV;IACD,CAAE,CAAC;EACJ;AACD","ignoreList":[]}
1
+ {
2
+ "version": 3,
3
+ "sources": ["../../src/utils/validate-mime-type.ts"],
4
+ "sourcesContent": ["/**\n * WordPress dependencies\n */\nimport { __, sprintf } from '@wordpress/i18n';\n\n/**\n * Internal dependencies\n */\nimport { UploadError } from './upload-error';\n\n/**\n * Verifies if the caller (e.g. a block) supports this mime type.\n *\n * @param file File object.\n * @param allowedTypes List of allowed mime types.\n */\nexport function validateMimeType( file: File, allowedTypes?: string[] ) {\n\tif ( ! allowedTypes ) {\n\t\treturn;\n\t}\n\n\t// Allowed type specified by consumer.\n\tconst isAllowedType = allowedTypes.some( ( allowedType ) => {\n\t\t// If a complete mimetype is specified verify if it matches exactly the mime type of the file.\n\t\tif ( allowedType.includes( '/' ) ) {\n\t\t\treturn allowedType === file.type;\n\t\t}\n\t\t// Otherwise a general mime type is used, and we should verify if the file mimetype starts with it.\n\t\treturn file.type.startsWith( `${ allowedType }/` );\n\t} );\n\n\tif ( file.type && ! isAllowedType ) {\n\t\tthrow new UploadError( {\n\t\t\tcode: 'MIME_TYPE_NOT_SUPPORTED',\n\t\t\tmessage: sprintf(\n\t\t\t\t// translators: %s: file name.\n\t\t\t\t__( '%s: Sorry, this file type is not supported here.' ),\n\t\t\t\tfile.name\n\t\t\t),\n\t\t\tfile,\n\t\t} );\n\t}\n}\n"],
5
+ "mappings": "AAGA,SAAS,IAAI,eAAe;AAK5B,SAAS,mBAAmB;AAQrB,SAAS,iBAAkB,MAAY,cAA0B;AACvE,MAAK,CAAE,cAAe;AACrB;AAAA,EACD;AAGA,QAAM,gBAAgB,aAAa,KAAM,CAAE,gBAAiB;AAE3D,QAAK,YAAY,SAAU,GAAI,GAAI;AAClC,aAAO,gBAAgB,KAAK;AAAA,IAC7B;AAEA,WAAO,KAAK,KAAK,WAAY,GAAI,WAAY,GAAI;AAAA,EAClD,CAAE;AAEF,MAAK,KAAK,QAAQ,CAAE,eAAgB;AACnC,UAAM,IAAI,YAAa;AAAA,MACtB,MAAM;AAAA,MACN,SAAS;AAAA;AAAA,QAER,GAAI,kDAAmD;AAAA,QACvD,KAAK;AAAA,MACN;AAAA,MACA;AAAA,IACD,CAAE;AAAA,EACH;AACD;",
6
+ "names": []
7
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wordpress/media-utils",
3
- "version": "5.31.1-next.f56bd8138.0",
3
+ "version": "5.32.1-next.47f435fc9.0",
4
4
  "description": "WordPress Media Upload Utils.",
5
5
  "author": "The WordPress Contributors",
6
6
  "license": "GPL-2.0-or-later",
@@ -25,18 +25,25 @@
25
25
  },
26
26
  "main": "build/index.js",
27
27
  "module": "build-module/index.js",
28
+ "exports": {
29
+ ".": {
30
+ "types": "./build-types/index.d.ts",
31
+ "import": "./build-module/index.js",
32
+ "require": "./build/index.js"
33
+ },
34
+ "./package.json": "./package.json"
35
+ },
28
36
  "wpScript": true,
29
37
  "types": "build-types",
30
38
  "dependencies": {
31
- "@babel/runtime": "7.25.7",
32
- "@wordpress/api-fetch": "^7.31.1-next.f56bd8138.0",
33
- "@wordpress/blob": "^4.31.1-next.f56bd8138.0",
34
- "@wordpress/element": "^6.31.1-next.f56bd8138.0",
35
- "@wordpress/i18n": "^6.4.1-next.f56bd8138.0",
36
- "@wordpress/private-apis": "^1.31.1-next.f56bd8138.0"
39
+ "@wordpress/api-fetch": "^7.32.1-next.47f435fc9.0",
40
+ "@wordpress/blob": "^4.32.1-next.47f435fc9.0",
41
+ "@wordpress/element": "^6.32.1-next.47f435fc9.0",
42
+ "@wordpress/i18n": "^6.5.1-next.47f435fc9.0",
43
+ "@wordpress/private-apis": "^1.32.1-next.47f435fc9.0"
37
44
  },
38
45
  "publishConfig": {
39
46
  "access": "public"
40
47
  },
41
- "gitHead": "ea4a281fd857f48338877590de8c8eb9b9a8cef4"
48
+ "gitHead": "9720f22c138771d2ed1a0522725c3cdf1c242953"
42
49
  }