@wordpress/media-utils 5.7.0 → 5.8.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.
- package/CHANGELOG.md +6 -0
- package/README.md +70 -1
- package/build/index.js +43 -11
- package/build/index.js.map +1 -1
- package/build/utils/flatten-form-data.js +32 -0
- package/build/utils/flatten-form-data.js.map +1 -0
- package/build/utils/get-mime-types-array.js +27 -0
- package/build/utils/get-mime-types-array.js.map +1 -0
- package/build/utils/transform-attachment.js +33 -0
- package/build/utils/transform-attachment.js.map +1 -0
- package/build/utils/types.js +6 -0
- package/build/utils/types.js.map +1 -0
- package/build/utils/upload-error.js +29 -0
- package/build/utils/upload-error.js.map +1 -0
- package/build/utils/upload-media.js +53 -152
- package/build/utils/upload-media.js.map +1 -1
- package/build/utils/upload-to-server.js +34 -0
- package/build/utils/upload-to-server.js.map +1 -0
- package/build/utils/validate-file-size.js +44 -0
- package/build/utils/validate-file-size.js.map +1 -0
- package/build/utils/validate-mime-type-for-user.js +41 -0
- package/build/utils/validate-mime-type-for-user.js.map +1 -0
- package/build/utils/validate-mime-type.js +47 -0
- package/build/utils/validate-mime-type.js.map +1 -0
- package/build-module/index.js +5 -1
- package/build-module/index.js.map +1 -1
- package/build-module/utils/flatten-form-data.js +26 -0
- package/build-module/utils/flatten-form-data.js.map +1 -0
- package/build-module/utils/get-mime-types-array.js +21 -0
- package/build-module/utils/get-mime-types-array.js.map +1 -0
- package/build-module/utils/transform-attachment.js +27 -0
- package/build-module/utils/transform-attachment.js.map +1 -0
- package/build-module/utils/types.js +2 -0
- package/build-module/utils/types.js.map +1 -0
- package/build-module/utils/upload-error.js +22 -0
- package/build-module/utils/upload-error.js.map +1 -0
- package/build-module/utils/upload-media.js +53 -149
- package/build-module/utils/upload-media.js.map +1 -1
- package/build-module/utils/upload-to-server.js +26 -0
- package/build-module/utils/upload-to-server.js.map +1 -0
- package/build-module/utils/validate-file-size.js +38 -0
- package/build-module/utils/validate-file-size.js.map +1 -0
- package/build-module/utils/validate-mime-type-for-user.js +35 -0
- package/build-module/utils/validate-mime-type-for-user.js.map +1 -0
- package/build-module/utils/validate-mime-type.js +41 -0
- package/build-module/utils/validate-mime-type.js.map +1 -0
- package/build-types/components/index.d.ts +2 -0
- package/build-types/components/index.d.ts.map +1 -0
- package/build-types/components/media-upload/index.d.ts +30 -0
- package/build-types/components/media-upload/index.d.ts.map +1 -0
- package/build-types/index.d.ts +8 -0
- package/build-types/index.d.ts.map +1 -0
- package/build-types/utils/flatten-form-data.d.ts +9 -0
- package/build-types/utils/flatten-form-data.d.ts.map +1 -0
- package/build-types/utils/get-mime-types-array.d.ts +12 -0
- package/build-types/utils/get-mime-types-array.d.ts.map +1 -0
- package/build-types/utils/transform-attachment.d.ts +11 -0
- package/build-types/utils/transform-attachment.d.ts.map +1 -0
- package/build-types/utils/types.d.ts +199 -0
- package/build-types/utils/types.d.ts.map +1 -0
- package/build-types/utils/upload-error.d.ts +19 -0
- package/build-types/utils/upload-error.d.ts.map +1 -0
- package/build-types/utils/upload-media.d.ts +32 -0
- package/build-types/utils/upload-media.d.ts.map +1 -0
- package/build-types/utils/upload-to-server.d.ts +3 -0
- package/build-types/utils/upload-to-server.d.ts.map +1 -0
- package/build-types/utils/validate-file-size.d.ts +8 -0
- package/build-types/utils/validate-file-size.d.ts.map +1 -0
- package/build-types/utils/validate-mime-type-for-user.d.ts +8 -0
- package/build-types/utils/validate-mime-type-for-user.d.ts.map +1 -0
- package/build-types/utils/validate-mime-type.d.ts +8 -0
- package/build-types/utils/validate-mime-type.d.ts.map +1 -0
- package/package.json +8 -7
- package/src/index.ts +9 -0
- package/src/utils/flatten-form-data.ts +33 -0
- package/src/utils/get-mime-types-array.ts +29 -0
- package/src/utils/test/flatten-form-data.ts +49 -0
- package/src/utils/test/get-mime-types-array.ts +47 -0
- package/src/utils/test/upload-error.ts +24 -0
- package/src/utils/test/{upload-media.test.js → upload-media.ts} +47 -76
- package/src/utils/test/validate-file-size.ts +70 -0
- package/src/utils/test/validate-mime-type-for-user.ts +37 -0
- package/src/utils/test/validate-mime-type.ts +57 -0
- package/src/utils/transform-attachment.ts +24 -0
- package/src/utils/types.ts +207 -0
- package/src/utils/upload-error.ts +26 -0
- package/src/utils/upload-media.ts +149 -0
- package/src/utils/upload-to-server.ts +38 -0
- package/src/utils/validate-file-size.ts +44 -0
- package/src/utils/validate-mime-type-for-user.ts +46 -0
- package/src/utils/validate-mime-type.ts +43 -0
- package/tsconfig.json +17 -0
- package/tsconfig.tsbuildinfo +1 -0
- package/build/utils/index.js +0 -13
- package/build/utils/index.js.map +0 -1
- package/build-module/utils/index.js +0 -2
- package/build-module/utils/index.js.map +0 -1
- package/src/index.js +0 -2
- package/src/utils/index.js +0 -1
- package/src/utils/upload-media.js +0 -232
|
@@ -1,137 +1,75 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* WordPress dependencies
|
|
3
3
|
*/
|
|
4
|
-
import apiFetch from '@wordpress/api-fetch';
|
|
5
|
-
import { createBlobURL, revokeBlobURL } from '@wordpress/blob';
|
|
6
4
|
import { __, sprintf } from '@wordpress/i18n';
|
|
7
|
-
|
|
5
|
+
import { createBlobURL, revokeBlobURL } from '@wordpress/blob';
|
|
8
6
|
|
|
9
7
|
/**
|
|
10
|
-
*
|
|
11
|
-
* This function computes a flexible array of mime types from the mime type structured provided by the server.
|
|
12
|
-
* Converts { jpg|jpeg|jpe: "image/jpeg" } into [ "image/jpeg", "image/jpg", "image/jpeg", "image/jpe" ]
|
|
13
|
-
* The computation of this array instead of directly using the object,
|
|
14
|
-
* solves the problem in chrome where mp3 files have audio/mp3 as mime type instead of audio/mpeg.
|
|
15
|
-
* https://bugs.chromium.org/p/chromium/issues/detail?id=227004
|
|
16
|
-
*
|
|
17
|
-
* @param {?Object} wpMimeTypesObject Mime type object received from the server.
|
|
18
|
-
* Extensions are keys separated by '|' and values are mime types associated with an extension.
|
|
19
|
-
*
|
|
20
|
-
* @return {?Array} An array of mime types or the parameter passed if it was "falsy".
|
|
8
|
+
* Internal dependencies
|
|
21
9
|
*/
|
|
22
|
-
export function getMimeTypesArray(wpMimeTypesObject) {
|
|
23
|
-
if (!wpMimeTypesObject) {
|
|
24
|
-
return wpMimeTypesObject;
|
|
25
|
-
}
|
|
26
|
-
return Object.entries(wpMimeTypesObject).map(([extensionsString, mime]) => {
|
|
27
|
-
const [type] = mime.split('/');
|
|
28
|
-
const extensions = extensionsString.split('|');
|
|
29
|
-
return [mime, ...extensions.map(extension => `${type}/${extension}`)];
|
|
30
|
-
}).flat();
|
|
31
|
-
}
|
|
32
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';
|
|
33
16
|
/**
|
|
34
|
-
*
|
|
35
|
-
*
|
|
17
|
+
* Upload a media file when the file upload button is activated
|
|
18
|
+
* or when adding a file to the editor via drag & drop.
|
|
36
19
|
*
|
|
37
|
-
*
|
|
38
|
-
*
|
|
39
|
-
* @param
|
|
40
|
-
* @param
|
|
41
|
-
* @param
|
|
42
|
-
* @param
|
|
43
|
-
* @param
|
|
44
|
-
* @param
|
|
45
|
-
* @param
|
|
46
|
-
* @param {?Object} $0.wpAllowedMimeTypes List of allowed mime types and file extensions.
|
|
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.
|
|
47
29
|
*/
|
|
48
|
-
export
|
|
30
|
+
export function uploadMedia({
|
|
31
|
+
wpAllowedMimeTypes,
|
|
49
32
|
allowedTypes,
|
|
50
33
|
additionalData = {},
|
|
51
34
|
filesList,
|
|
52
35
|
maxUploadFileSize,
|
|
53
|
-
onError
|
|
36
|
+
onError,
|
|
54
37
|
onFileChange,
|
|
55
|
-
|
|
38
|
+
signal
|
|
56
39
|
}) {
|
|
57
|
-
|
|
58
|
-
const files = [...filesList];
|
|
40
|
+
const validFiles = [];
|
|
59
41
|
const filesSet = [];
|
|
60
|
-
const setAndUpdateFiles = (
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
onFileChange(filesSet.filter(Boolean));
|
|
64
|
-
};
|
|
65
|
-
|
|
66
|
-
// Allowed type specified by consumer.
|
|
67
|
-
const isAllowedType = fileType => {
|
|
68
|
-
if (!allowedTypes) {
|
|
69
|
-
return true;
|
|
42
|
+
const setAndUpdateFiles = (index, value) => {
|
|
43
|
+
if (filesSet[index]?.url) {
|
|
44
|
+
revokeBlobURL(filesSet[index].url);
|
|
70
45
|
}
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
if (allowedType.includes('/')) {
|
|
74
|
-
return allowedType === fileType;
|
|
75
|
-
}
|
|
76
|
-
// Otherwise a general mime type is used and we should verify if the file mimetype starts with it.
|
|
77
|
-
return fileType.startsWith(`${allowedType}/`);
|
|
78
|
-
});
|
|
46
|
+
filesSet[index] = value;
|
|
47
|
+
onFileChange?.(filesSet.filter(attachment => attachment !== null));
|
|
79
48
|
};
|
|
80
|
-
|
|
81
|
-
// Allowed types for the current WP_User.
|
|
82
|
-
const allowedMimeTypesForUser = getMimeTypesArray(wpAllowedMimeTypes);
|
|
83
|
-
const isAllowedMimeTypeForUser = fileType => {
|
|
84
|
-
return allowedMimeTypesForUser.includes(fileType);
|
|
85
|
-
};
|
|
86
|
-
const validFiles = [];
|
|
87
|
-
for (const mediaFile of files) {
|
|
49
|
+
for (const mediaFile of filesList) {
|
|
88
50
|
// Verify if user is allowed to upload this mime type.
|
|
89
51
|
// Defer to the server when type not detected.
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
// translators: %s: file name.
|
|
95
|
-
__('%s: Sorry, you are not allowed to upload this file type.'), mediaFile.name),
|
|
96
|
-
file: mediaFile
|
|
97
|
-
});
|
|
52
|
+
try {
|
|
53
|
+
validateMimeTypeForUser(mediaFile, wpAllowedMimeTypes);
|
|
54
|
+
} catch (error) {
|
|
55
|
+
onError?.(error);
|
|
98
56
|
continue;
|
|
99
57
|
}
|
|
100
58
|
|
|
101
|
-
// Check if the block supports this mime type.
|
|
59
|
+
// Check if the caller (e.g. a block) supports this mime type.
|
|
102
60
|
// Defer to the server when type not detected.
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
// translators: %s: file name.
|
|
108
|
-
__('%s: Sorry, this file type is not supported here.'), mediaFile.name),
|
|
109
|
-
file: mediaFile
|
|
110
|
-
});
|
|
61
|
+
try {
|
|
62
|
+
validateMimeType(mediaFile, allowedTypes);
|
|
63
|
+
} catch (error) {
|
|
64
|
+
onError?.(error);
|
|
111
65
|
continue;
|
|
112
66
|
}
|
|
113
67
|
|
|
114
68
|
// Verify if file is greater than the maximum file upload size allowed for the site.
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
// translators: %s: file name.
|
|
120
|
-
__('%s: This file exceeds the maximum upload size for this site.'), mediaFile.name),
|
|
121
|
-
file: mediaFile
|
|
122
|
-
});
|
|
123
|
-
continue;
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
// Don't allow empty files to be uploaded.
|
|
127
|
-
if (mediaFile.size <= 0) {
|
|
128
|
-
onError({
|
|
129
|
-
code: 'EMPTY_FILE',
|
|
130
|
-
message: sprintf(
|
|
131
|
-
// translators: %s: file name.
|
|
132
|
-
__('%s: This file is empty.'), mediaFile.name),
|
|
133
|
-
file: mediaFile
|
|
134
|
-
});
|
|
69
|
+
try {
|
|
70
|
+
validateFileSize(mediaFile, maxUploadFileSize);
|
|
71
|
+
} catch (error) {
|
|
72
|
+
onError?.(error);
|
|
135
73
|
continue;
|
|
136
74
|
}
|
|
137
75
|
validFiles.push(mediaFile);
|
|
@@ -141,64 +79,30 @@ export async function uploadMedia({
|
|
|
141
79
|
filesSet.push({
|
|
142
80
|
url: createBlobURL(mediaFile)
|
|
143
81
|
});
|
|
144
|
-
onFileChange(filesSet);
|
|
82
|
+
onFileChange?.(filesSet);
|
|
145
83
|
}
|
|
146
|
-
|
|
147
|
-
const mediaFile = validFiles[idx];
|
|
84
|
+
validFiles.map(async (file, index) => {
|
|
148
85
|
try {
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
// eslint-disable-next-line camelcase
|
|
152
|
-
const {
|
|
153
|
-
alt_text,
|
|
154
|
-
source_url,
|
|
155
|
-
...savedMediaProps
|
|
156
|
-
} = savedMedia;
|
|
157
|
-
const mediaObject = {
|
|
158
|
-
...savedMediaProps,
|
|
159
|
-
alt: savedMedia.alt_text,
|
|
160
|
-
caption: (_savedMedia$caption$r = savedMedia.caption?.raw) !== null && _savedMedia$caption$r !== void 0 ? _savedMedia$caption$r : '',
|
|
161
|
-
title: savedMedia.title.raw,
|
|
162
|
-
url: savedMedia.source_url
|
|
163
|
-
};
|
|
164
|
-
setAndUpdateFiles(idx, mediaObject);
|
|
86
|
+
const attachment = await uploadToServer(file, additionalData, signal);
|
|
87
|
+
setAndUpdateFiles(index, attachment);
|
|
165
88
|
} catch (error) {
|
|
166
89
|
// Reset to empty on failure.
|
|
167
|
-
setAndUpdateFiles(
|
|
90
|
+
setAndUpdateFiles(index, null);
|
|
168
91
|
let message;
|
|
169
|
-
if (error
|
|
92
|
+
if (error instanceof Error) {
|
|
170
93
|
message = error.message;
|
|
171
94
|
} else {
|
|
172
95
|
message = sprintf(
|
|
173
96
|
// translators: %s: file name
|
|
174
|
-
__('Error while uploading file %s to the media library.'),
|
|
97
|
+
__('Error while uploading file %s to the media library.'), file.name);
|
|
175
98
|
}
|
|
176
|
-
onError({
|
|
99
|
+
onError?.(new UploadError({
|
|
177
100
|
code: 'GENERAL',
|
|
178
101
|
message,
|
|
179
|
-
file
|
|
180
|
-
|
|
102
|
+
file,
|
|
103
|
+
cause: error instanceof Error ? error : undefined
|
|
104
|
+
}));
|
|
181
105
|
}
|
|
182
|
-
}
|
|
183
|
-
}
|
|
184
|
-
|
|
185
|
-
/**
|
|
186
|
-
* @param {File} file Media File to Save.
|
|
187
|
-
* @param {?Object} additionalData Additional data to include in the request.
|
|
188
|
-
*
|
|
189
|
-
* @return {Promise} Media Object Promise.
|
|
190
|
-
*/
|
|
191
|
-
function createMediaFromFile(file, additionalData) {
|
|
192
|
-
// Create upload payload.
|
|
193
|
-
const data = new window.FormData();
|
|
194
|
-
data.append('file', file, file.name || file.type.replace('/', '.'));
|
|
195
|
-
if (additionalData) {
|
|
196
|
-
Object.entries(additionalData).forEach(([key, value]) => data.append(key, value));
|
|
197
|
-
}
|
|
198
|
-
return apiFetch({
|
|
199
|
-
path: '/wp/v2/media',
|
|
200
|
-
body: data,
|
|
201
|
-
method: 'POST'
|
|
202
106
|
});
|
|
203
107
|
}
|
|
204
108
|
//# sourceMappingURL=upload-media.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"names":["apiFetch","createBlobURL","revokeBlobURL","__","sprintf","noop","getMimeTypesArray","wpMimeTypesObject","Object","entries","map","extensionsString","mime","type","split","extensions","extension","flat","uploadMedia","allowedTypes","additionalData","filesList","maxUploadFileSize","onError","onFileChange","wpAllowedMimeTypes","files","filesSet","setAndUpdateFiles","idx","value","url","filter","Boolean","isAllowedType","fileType","some","allowedType","includes","startsWith","allowedMimeTypesForUser","isAllowedMimeTypeForUser","validFiles","mediaFile","code","message","name","file","size","push","length","_savedMedia$caption$r","savedMedia","createMediaFromFile","alt_text","source_url","savedMediaProps","mediaObject","alt","caption","raw","title","error","data","window","FormData","append","replace","forEach","key","path","body","method"],"sources":["@wordpress/media-utils/src/utils/upload-media.js"],"sourcesContent":["/**\n * WordPress dependencies\n */\nimport apiFetch from '@wordpress/api-fetch';\nimport { createBlobURL, revokeBlobURL } from '@wordpress/blob';\nimport { __, sprintf } from '@wordpress/i18n';\n\nconst noop = () => {};\n\n/**\n * Browsers may use unexpected mime types, and they differ from browser to browser.\n * This function computes a flexible array of mime types from the mime type structured provided by the server.\n * Converts { jpg|jpeg|jpe: \"image/jpeg\" } into [ \"image/jpeg\", \"image/jpg\", \"image/jpeg\", \"image/jpe\" ]\n * The computation of this array instead of directly using the object,\n * solves the problem in chrome where mp3 files have audio/mp3 as mime type instead of audio/mpeg.\n * https://bugs.chromium.org/p/chromium/issues/detail?id=227004\n *\n * @param {?Object} wpMimeTypesObject Mime type object received from the server.\n * Extensions are keys separated by '|' and values are mime types associated with an extension.\n *\n * @return {?Array} An array of mime types or the parameter passed if it was \"falsy\".\n */\nexport function getMimeTypesArray( wpMimeTypesObject ) {\n\tif ( ! wpMimeTypesObject ) {\n\t\treturn wpMimeTypesObject;\n\t}\n\treturn Object.entries( wpMimeTypesObject )\n\t\t.map( ( [ extensionsString, mime ] ) => {\n\t\t\tconst [ type ] = mime.split( '/' );\n\t\t\tconst extensions = extensionsString.split( '|' );\n\t\t\treturn [\n\t\t\t\tmime,\n\t\t\t\t...extensions.map(\n\t\t\t\t\t( extension ) => `${ type }/${ extension }`\n\t\t\t\t),\n\t\t\t];\n\t\t} )\n\t\t.flat();\n}\n\n/**\n *\tMedia Upload is used by audio, image, gallery, video, and file blocks to\n *\thandle uploading a media file when a file upload button is activated.\n *\n *\tTODO: future enhancement to add an upload indicator.\n *\n * @param {Object} $0 Parameters object passed to the function.\n * @param {?Array} $0.allowedTypes Array with the types of media that can be uploaded, if unset all types are allowed.\n * @param {?Object} $0.additionalData Additional data to include in the request.\n * @param {Array} $0.filesList List of files.\n * @param {?number} $0.maxUploadFileSize Maximum upload size in bytes allowed for the site.\n * @param {Function} $0.onError Function called when an error happens.\n * @param {Function} $0.onFileChange Function called each time a file or a temporary representation of the file is available.\n * @param {?Object} $0.wpAllowedMimeTypes List of allowed mime types and file extensions.\n */\nexport async function uploadMedia( {\n\tallowedTypes,\n\tadditionalData = {},\n\tfilesList,\n\tmaxUploadFileSize,\n\tonError = noop,\n\tonFileChange,\n\twpAllowedMimeTypes = null,\n} ) {\n\t// Cast filesList to array.\n\tconst files = [ ...filesList ];\n\n\tconst filesSet = [];\n\tconst setAndUpdateFiles = ( idx, value ) => {\n\t\trevokeBlobURL( filesSet[ idx ]?.url );\n\t\tfilesSet[ idx ] = value;\n\t\tonFileChange( filesSet.filter( Boolean ) );\n\t};\n\n\t// Allowed type specified by consumer.\n\tconst isAllowedType = ( fileType ) => {\n\t\tif ( ! allowedTypes ) {\n\t\t\treturn true;\n\t\t}\n\t\treturn allowedTypes.some( ( allowedType ) => {\n\t\t\t// If a complete mimetype is specified verify if it matches exactly the mime type of the file.\n\t\t\tif ( allowedType.includes( '/' ) ) {\n\t\t\t\treturn allowedType === fileType;\n\t\t\t}\n\t\t\t// Otherwise a general mime type is used and we should verify if the file mimetype starts with it.\n\t\t\treturn fileType.startsWith( `${ allowedType }/` );\n\t\t} );\n\t};\n\n\t// Allowed types for the current WP_User.\n\tconst allowedMimeTypesForUser = getMimeTypesArray( wpAllowedMimeTypes );\n\tconst isAllowedMimeTypeForUser = ( fileType ) => {\n\t\treturn allowedMimeTypesForUser.includes( fileType );\n\t};\n\n\tconst validFiles = [];\n\n\tfor ( const mediaFile of files ) {\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\tif (\n\t\t\tallowedMimeTypesForUser &&\n\t\t\tmediaFile.type &&\n\t\t\t! isAllowedMimeTypeForUser( mediaFile.type )\n\t\t) {\n\t\t\tonError( {\n\t\t\t\tcode: 'MIME_TYPE_NOT_ALLOWED_FOR_USER',\n\t\t\t\tmessage: sprintf(\n\t\t\t\t\t// translators: %s: file name.\n\t\t\t\t\t__(\n\t\t\t\t\t\t'%s: Sorry, you are not allowed to upload this file type.'\n\t\t\t\t\t),\n\t\t\t\t\tmediaFile.name\n\t\t\t\t),\n\t\t\t\tfile: mediaFile,\n\t\t\t} );\n\t\t\tcontinue;\n\t\t}\n\n\t\t// Check if the block supports this mime type.\n\t\t// Defer to the server when type not detected.\n\t\tif ( mediaFile.type && ! isAllowedType( mediaFile.type ) ) {\n\t\t\tonError( {\n\t\t\t\tcode: 'MIME_TYPE_NOT_SUPPORTED',\n\t\t\t\tmessage: sprintf(\n\t\t\t\t\t// translators: %s: file name.\n\t\t\t\t\t__( '%s: Sorry, this file type is not supported here.' ),\n\t\t\t\t\tmediaFile.name\n\t\t\t\t),\n\t\t\t\tfile: mediaFile,\n\t\t\t} );\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\tif ( maxUploadFileSize && mediaFile.size > maxUploadFileSize ) {\n\t\t\tonError( {\n\t\t\t\tcode: 'SIZE_ABOVE_LIMIT',\n\t\t\t\tmessage: sprintf(\n\t\t\t\t\t// translators: %s: file name.\n\t\t\t\t\t__(\n\t\t\t\t\t\t'%s: This file exceeds the maximum upload size for this site.'\n\t\t\t\t\t),\n\t\t\t\t\tmediaFile.name\n\t\t\t\t),\n\t\t\t\tfile: mediaFile,\n\t\t\t} );\n\t\t\tcontinue;\n\t\t}\n\n\t\t// Don't allow empty files to be uploaded.\n\t\tif ( mediaFile.size <= 0 ) {\n\t\t\tonError( {\n\t\t\t\tcode: 'EMPTY_FILE',\n\t\t\t\tmessage: sprintf(\n\t\t\t\t\t// translators: %s: file name.\n\t\t\t\t\t__( '%s: This file is empty.' ),\n\t\t\t\t\tmediaFile.name\n\t\t\t\t),\n\t\t\t\tfile: mediaFile,\n\t\t\t} );\n\t\t\tcontinue;\n\t\t}\n\n\t\tvalidFiles.push( mediaFile );\n\n\t\t// Set temporary URL to create placeholder media file, this is replaced\n\t\t// with final file from media gallery when upload is `done` below.\n\t\tfilesSet.push( { url: createBlobURL( mediaFile ) } );\n\t\tonFileChange( filesSet );\n\t}\n\n\tfor ( let idx = 0; idx < validFiles.length; ++idx ) {\n\t\tconst mediaFile = validFiles[ idx ];\n\t\ttry {\n\t\t\tconst savedMedia = await createMediaFromFile(\n\t\t\t\tmediaFile,\n\t\t\t\tadditionalData\n\t\t\t);\n\t\t\t// eslint-disable-next-line camelcase\n\t\t\tconst { alt_text, source_url, ...savedMediaProps } = savedMedia;\n\t\t\tconst mediaObject = {\n\t\t\t\t...savedMediaProps,\n\t\t\t\talt: savedMedia.alt_text,\n\t\t\t\tcaption: savedMedia.caption?.raw ?? '',\n\t\t\t\ttitle: savedMedia.title.raw,\n\t\t\t\turl: savedMedia.source_url,\n\t\t\t};\n\t\t\tsetAndUpdateFiles( idx, mediaObject );\n\t\t} catch ( error ) {\n\t\t\t// Reset to empty on failure.\n\t\t\tsetAndUpdateFiles( idx, null );\n\t\t\tlet message;\n\t\t\tif ( error.message ) {\n\t\t\t\tmessage = 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\tmediaFile.name\n\t\t\t\t);\n\t\t\t}\n\t\t\tonError( {\n\t\t\t\tcode: 'GENERAL',\n\t\t\t\tmessage,\n\t\t\t\tfile: mediaFile,\n\t\t\t} );\n\t\t}\n\t}\n}\n\n/**\n * @param {File} file Media File to Save.\n * @param {?Object} additionalData Additional data to include in the request.\n *\n * @return {Promise} Media Object Promise.\n */\nfunction createMediaFromFile( file, additionalData ) {\n\t// Create upload payload.\n\tconst data = new window.FormData();\n\tdata.append( 'file', file, file.name || file.type.replace( '/', '.' ) );\n\tif ( additionalData ) {\n\t\tObject.entries( additionalData ).forEach( ( [ key, value ] ) =>\n\t\t\tdata.append( key, value )\n\t\t);\n\t}\n\treturn ap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AAE,KACzDiC,IAAI,CAACG,MAAM,CAAEG,GAAG,EAAEvC,KAAM,CACzB,CAAC;EACF;EACA,OAAO9B,QAAQ,CAAE;IAChBsE,IAAI,EAAE,cAAc;IACpBC,IAAI,EAAER,IAAI;IACVS,MAAM,EAAE;EACT,CAAE,CAAC;AACJ","ignoreList":[]}
|
|
1
|
+
{"version":3,"names":["__","sprintf","createBlobURL","revokeBlobURL","uploadToServer","validateMimeType","validateMimeTypeForUser","validateFileSize","UploadError","uploadMedia","wpAllowedMimeTypes","allowedTypes","additionalData","filesList","maxUploadFileSize","onError","onFileChange","signal","validFiles","filesSet","setAndUpdateFiles","index","value","url","filter","attachment","mediaFile","error","push","map","file","message","Error","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\tOnSuccessHandler,\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\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// Function called once a file has completely finished uploading, including thumbnails.\n\tonSuccess?: OnSuccessHandler;\n\t// List of allowed mime types and file extensions.\n\twpAllowedMimeTypes?: Record< string, string > | null;\n\t// Abort signal.\n\tsignal?: AbortSignal;\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 */\nexport function uploadMedia( {\n\twpAllowedMimeTypes,\n\tallowedTypes,\n\tadditionalData = {},\n\tfilesList,\n\tmaxUploadFileSize,\n\tonError,\n\tonFileChange,\n\tsignal,\n}: UploadMediaArgs ) {\n\tconst validFiles = [];\n\n\tconst filesSet: Array< Partial< Attachment > | null > = [];\n\tconst setAndUpdateFiles = ( index: number, value: Attachment | null ) => {\n\t\tif ( filesSet[ index ]?.url ) {\n\t\t\trevokeBlobURL( filesSet[ index ].url );\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// Set temporary URL to create placeholder media file, this is replaced\n\t\t// with final file from media gallery when upload is `done` below.\n\t\tfilesSet.push( { url: createBlobURL( mediaFile ) } );\n\t\tonFileChange?.( filesSet as Array< Partial< Attachment > > );\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\tlet message;\n\t\t\tif ( error instanceof Error ) {\n\t\t\t\tmessage = 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;;AAQA,SAASC,cAAc,QAAQ,oBAAoB;AACnD,SAASC,gBAAgB,QAAQ,sBAAsB;AACvD,SAASC,uBAAuB,QAAQ,+BAA+B;AACvE,SAASC,gBAAgB,QAAQ,sBAAsB;AACvD,SAASC,WAAW,QAAQ,gBAAgB;AAuB5C;AACA;AACA;AACA;AACA;AACA;AACA;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B,aAAa,CAAEwB,SAAU;IAAE,CAAE,CAAC;IACpDV,YAAY,GAAIG,QAA2C,CAAC;EAC7D;EAEAD,UAAU,CAACW,GAAG,CAAE,OAAQC,IAAI,EAAET,KAAK,KAAM;IACxC,IAAI;MACH,MAAMI,UAAU,GAAG,MAAMrB,cAAc,CACtC0B,IAAI,EACJlB,cAAc,EACdK,MACD,CAAC;MACDG,iBAAiB,CAAEC,KAAK,EAAEI,UAAW,CAAC;IACvC,CAAC,CAAC,OAAQE,KAAK,EAAG;MACjB;MACAP,iBAAiB,CAAEC,KAAK,EAAE,IAAK,CAAC;MAEhC,IAAIU,OAAO;MACX,IAAKJ,KAAK,YAAYK,KAAK,EAAG;QAC7BD,OAAO,GAAGJ,KAAK,CAACI,OAAO;MACxB,CAAC,MAAM;QACNA,OAAO,GAAG9B,OAAO;QAChB;QACAD,EAAE,CAAE,qDAAsD,CAAC,EAC3D8B,IAAI,CAACG,IACN,CAAC;MACF;MAEAlB,OAAO,GACN,IAAIP,WAAW,CAAE;QAChB0B,IAAI,EAAE,SAAS;QACfH,OAAO;QACPD,IAAI;QACJK,KAAK,EAAER,KAAK,YAAYK,KAAK,GAAGL,KAAK,GAAGS;MACzC,CAAE,CACH,CAAC;IACF;EACD,CAAE,CAAC;AACJ","ignoreList":[]}
|
|
@@ -0,0 +1,26 @@
|
|
|
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.
|
|
13
|
+
const data = new FormData();
|
|
14
|
+
data.append('file', file, file.name || file.type.replace('/', '.'));
|
|
15
|
+
for (const [key, value] of Object.entries(additionalData)) {
|
|
16
|
+
flattenFormData(data, key, value);
|
|
17
|
+
}
|
|
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
|
+
}));
|
|
25
|
+
}
|
|
26
|
+
//# sourceMappingURL=upload-to-server.js.map
|
|
@@ -0,0 +1 @@
|
|
|
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":[]}
|
|
@@ -0,0 +1,38 @@
|
|
|
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.
|
|
19
|
+
if (file.size <= 0) {
|
|
20
|
+
throw new UploadError({
|
|
21
|
+
code: 'EMPTY_FILE',
|
|
22
|
+
message: sprintf(
|
|
23
|
+
// translators: %s: file name.
|
|
24
|
+
__('%s: This file is empty.'), file.name),
|
|
25
|
+
file
|
|
26
|
+
});
|
|
27
|
+
}
|
|
28
|
+
if (maxUploadFileSize && file.size > maxUploadFileSize) {
|
|
29
|
+
throw new UploadError({
|
|
30
|
+
code: 'SIZE_ABOVE_LIMIT',
|
|
31
|
+
message: sprintf(
|
|
32
|
+
// translators: %s: file name.
|
|
33
|
+
__('%s: This file exceeds the maximum upload size for this site.'), file.name),
|
|
34
|
+
file
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
//# sourceMappingURL=validate-file-size.js.map
|
|
@@ -0,0 +1 @@
|
|
|
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":[]}
|
|
@@ -0,0 +1,35 @@
|
|
|
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.
|
|
20
|
+
const allowedMimeTypesForUser = getMimeTypesArray(wpAllowedMimeTypes);
|
|
21
|
+
if (!allowedMimeTypesForUser) {
|
|
22
|
+
return;
|
|
23
|
+
}
|
|
24
|
+
const isAllowedMimeTypeForUser = allowedMimeTypesForUser.includes(file.type);
|
|
25
|
+
if (file.type && !isAllowedMimeTypeForUser) {
|
|
26
|
+
throw new UploadError({
|
|
27
|
+
code: 'MIME_TYPE_NOT_ALLOWED_FOR_USER',
|
|
28
|
+
message: sprintf(
|
|
29
|
+
// translators: %s: file name.
|
|
30
|
+
__('%s: Sorry, you are not allowed to upload this file type.'), file.name),
|
|
31
|
+
file
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
//# sourceMappingURL=validate-mime-type-for-user.js.map
|
|
@@ -0,0 +1 @@
|
|
|
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":[]}
|
|
@@ -0,0 +1,41 @@
|
|
|
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) {
|
|
18
|
+
if (!allowedTypes) {
|
|
19
|
+
return;
|
|
20
|
+
}
|
|
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('/')) {
|
|
26
|
+
return allowedType === file.type;
|
|
27
|
+
}
|
|
28
|
+
// Otherwise a general mime type is used, and we should verify if the file mimetype starts with it.
|
|
29
|
+
return file.type.startsWith(`${allowedType}/`);
|
|
30
|
+
});
|
|
31
|
+
if (file.type && !isAllowedType) {
|
|
32
|
+
throw new UploadError({
|
|
33
|
+
code: 'MIME_TYPE_NOT_SUPPORTED',
|
|
34
|
+
message: sprintf(
|
|
35
|
+
// translators: %s: file name.
|
|
36
|
+
__('%s: Sorry, this file type is not supported here.'), file.name),
|
|
37
|
+
file
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
//# sourceMappingURL=validate-mime-type.js.map
|
|
@@ -0,0 +1 @@
|
|
|
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,CAAG,GAAGH,WAAa,GAAG,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":[]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/components/index.js"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
export default MediaUpload;
|
|
2
|
+
declare class MediaUpload extends Component<any, any, any> {
|
|
3
|
+
constructor(...args: any[]);
|
|
4
|
+
openModal(): void;
|
|
5
|
+
onOpen(): void;
|
|
6
|
+
onSelect(): void;
|
|
7
|
+
onUpdate(selections: any): void;
|
|
8
|
+
onClose(): void;
|
|
9
|
+
initializeListeners(): void;
|
|
10
|
+
/**
|
|
11
|
+
* Sets the Gallery frame and initializes listeners.
|
|
12
|
+
*
|
|
13
|
+
* @return {void}
|
|
14
|
+
*/
|
|
15
|
+
buildAndSetGalleryFrame(): void;
|
|
16
|
+
lastGalleryValue: any;
|
|
17
|
+
GalleryDetailsMediaFrame: any;
|
|
18
|
+
frame: any;
|
|
19
|
+
/**
|
|
20
|
+
* Initializes the Media Library requirements for the featured image flow.
|
|
21
|
+
*
|
|
22
|
+
* @return {void}
|
|
23
|
+
*/
|
|
24
|
+
buildAndSetFeatureImageFrame(): void;
|
|
25
|
+
componentWillUnmount(): void;
|
|
26
|
+
updateCollection(): void;
|
|
27
|
+
render(): any;
|
|
28
|
+
}
|
|
29
|
+
import { Component } from '@wordpress/element';
|
|
30
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/components/media-upload/index.js"],"names":[],"mappings":";AAiOA;IACC,4BAOC;IA+LD,kBAkCC;IArGD,eAqCC;IA5CD,iBAKC;IAzBD,gCAkBC;IAgDD,gBAQC;IAzKD,4BAMC;IAED;;;;OAIG;IACH,2BAFY,IAAI,CA+Cf;IA7BA,sBAA6B;IAa5B,8BAA6D;IAO9D,WAMG;IAKJ;;;;OAIG;IACH,gCAFY,IAAI,CA0Bf;IAED,6BAEC;IA8ED,yBAgBC;IAsCD,cAEC;CACD;0BA5cyB,oBAAoB"}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export * from './components';
|
|
2
|
+
export { uploadMedia } from './utils/upload-media';
|
|
3
|
+
export { transformAttachment } from './utils/transform-attachment';
|
|
4
|
+
export { validateFileSize } from './utils/validate-file-size';
|
|
5
|
+
export { validateMimeType } from './utils/validate-mime-type';
|
|
6
|
+
export { validateMimeTypeForUser } from './utils/validate-mime-type-for-user';
|
|
7
|
+
export type { Attachment, RestAttachment } from './utils/types';
|
|
8
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,cAAc,CAAC;AAE7B,OAAO,EAAE,WAAW,EAAE,MAAM,sBAAsB,CAAC;AACnD,OAAO,EAAE,mBAAmB,EAAE,MAAM,8BAA8B,CAAC;AACnE,OAAO,EAAE,gBAAgB,EAAE,MAAM,4BAA4B,CAAC;AAC9D,OAAO,EAAE,gBAAgB,EAAE,MAAM,4BAA4B,CAAC;AAC9D,OAAO,EAAE,uBAAuB,EAAE,MAAM,qCAAqC,CAAC;AAE9E,YAAY,EAAE,UAAU,EAAE,cAAc,EAAE,MAAM,eAAe,CAAC"}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Recursively flatten data passed to form data, to allow using multi-level objects.
|
|
3
|
+
*
|
|
4
|
+
* @param {FormData} formData Form data object.
|
|
5
|
+
* @param {string} key Key to amend to form data object
|
|
6
|
+
* @param {string|Object} data Data to be amended to form data.
|
|
7
|
+
*/
|
|
8
|
+
export declare function flattenFormData(formData: FormData, key: string, data: string | undefined | Record<string, string>): void;
|
|
9
|
+
//# sourceMappingURL=flatten-form-data.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"flatten-form-data.d.ts","sourceRoot":"","sources":["../../src/utils/flatten-form-data.ts"],"names":[],"mappings":"AAaA;;;;;;GAMG;AACH,wBAAgB,eAAe,CAC9B,QAAQ,EAAE,QAAQ,EAClB,GAAG,EAAE,MAAM,EACX,IAAI,EAAE,MAAM,GAAG,SAAS,GAAG,MAAM,CAAE,MAAM,EAAE,MAAM,CAAE,QASnD"}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Browsers may use unexpected mime types, and they differ from browser to browser.
|
|
3
|
+
* This function computes a flexible array of mime types from the mime type structured provided by the server.
|
|
4
|
+
* Converts { jpg|jpeg|jpe: "image/jpeg" } into [ "image/jpeg", "image/jpg", "image/jpeg", "image/jpe" ]
|
|
5
|
+
*
|
|
6
|
+
* @param {?Object} wpMimeTypesObject Mime type object received from the server.
|
|
7
|
+
* Extensions are keys separated by '|' and values are mime types associated with an extension.
|
|
8
|
+
*
|
|
9
|
+
* @return An array of mime types or null
|
|
10
|
+
*/
|
|
11
|
+
export declare function getMimeTypesArray(wpMimeTypesObject?: Record<string, string> | null): string[] | null;
|
|
12
|
+
//# sourceMappingURL=get-mime-types-array.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"get-mime-types-array.d.ts","sourceRoot":"","sources":["../../src/utils/get-mime-types-array.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AACH,wBAAgB,iBAAiB,CAChC,iBAAiB,CAAC,EAAE,MAAM,CAAE,MAAM,EAAE,MAAM,CAAE,GAAG,IAAI,mBAiBnD"}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Internal dependencies
|
|
3
|
+
*/
|
|
4
|
+
import type { Attachment, RestAttachment } from './types';
|
|
5
|
+
/**
|
|
6
|
+
* Transforms an attachment object from the REST API shape into the shape expected by the block editor and other consumers.
|
|
7
|
+
*
|
|
8
|
+
* @param attachment REST API attachment object.
|
|
9
|
+
*/
|
|
10
|
+
export declare function transformAttachment(attachment: RestAttachment): Attachment;
|
|
11
|
+
//# sourceMappingURL=transform-attachment.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"transform-attachment.d.ts","sourceRoot":"","sources":["../../src/utils/transform-attachment.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,OAAO,KAAK,EAAE,UAAU,EAAE,cAAc,EAAE,MAAM,SAAS,CAAC;AAE1D;;;;GAIG;AACH,wBAAgB,mBAAmB,CAAE,UAAU,EAAE,cAAc,GAAI,UAAU,CAa5E"}
|