UNPKG

@wordpress/media-utils

Version:
128 lines (121 loc) 4.48 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.uploadMedia = uploadMedia; var _i18n = require("@wordpress/i18n"); var _blob = require("@wordpress/blob"); var _uploadToServer = require("./upload-to-server"); var _validateMimeType = require("./validate-mime-type"); var _validateMimeTypeForUser = require("./validate-mime-type-for-user"); var _validateFileSize = require("./validate-file-size"); var _uploadError = require("./upload-error"); /** * WordPress dependencies */ /** * Internal dependencies */ /** * Upload a media file when the file upload button is activated * or when adding a file to the editor via drag & drop. * * @param $0 Parameters object passed to the function. * @param $0.allowedTypes Array with the types of media that can be uploaded, if unset all types are allowed. * @param $0.additionalData Additional data to include in the request. * @param $0.filesList List of files. * @param $0.maxUploadFileSize Maximum upload size in bytes allowed for the site. * @param $0.onError Function called when an error happens. * @param $0.onFileChange Function called each time a file or a temporary representation of the file is available. * @param $0.wpAllowedMimeTypes List of allowed mime types and file extensions. * @param $0.signal Abort signal. * @param $0.multiple Whether to allow multiple files to be uploaded. */ function uploadMedia({ wpAllowedMimeTypes, allowedTypes, additionalData = {}, filesList, maxUploadFileSize, onError, onFileChange, signal, multiple = true }) { if (!multiple && filesList.length > 1) { onError?.(new Error((0, _i18n.__)('Only one file can be used here.'))); return; } const validFiles = []; const filesSet = []; const setAndUpdateFiles = (index, value) => { // For client-side media processing, this is handled by the upload-media package. if (!window.__experimentalMediaProcessing) { if (filesSet[index]?.url) { (0, _blob.revokeBlobURL)(filesSet[index].url); } } filesSet[index] = value; onFileChange?.(filesSet.filter(attachment => attachment !== null)); }; for (const mediaFile of filesList) { // Verify if user is allowed to upload this mime type. // Defer to the server when type not detected. try { (0, _validateMimeTypeForUser.validateMimeTypeForUser)(mediaFile, wpAllowedMimeTypes); } catch (error) { onError?.(error); continue; } // Check if the caller (e.g. a block) supports this mime type. // Defer to the server when type not detected. try { (0, _validateMimeType.validateMimeType)(mediaFile, allowedTypes); } catch (error) { onError?.(error); continue; } // Verify if file is greater than the maximum file upload size allowed for the site. try { (0, _validateFileSize.validateFileSize)(mediaFile, maxUploadFileSize); } catch (error) { onError?.(error); continue; } validFiles.push(mediaFile); // For client-side media processing, this is handled by the upload-media package. if (!window.__experimentalMediaProcessing) { // Set temporary URL to create placeholder media file, this is replaced // with final file from media gallery when upload is `done` below. filesSet.push({ url: (0, _blob.createBlobURL)(mediaFile) }); onFileChange?.(filesSet); } } validFiles.map(async (file, index) => { try { const attachment = await (0, _uploadToServer.uploadToServer)(file, additionalData, signal); setAndUpdateFiles(index, attachment); } catch (error) { // Reset to empty on failure. setAndUpdateFiles(index, null); // @wordpress/api-fetch throws any response that isn't in the 200 range as-is. let message; if (typeof error === 'object' && error !== null && 'message' in error) { message = typeof error.message === 'string' ? error.message : String(error.message); } else { message = (0, _i18n.sprintf)( // translators: %s: file name (0, _i18n.__)('Error while uploading file %s to the media library.'), file.name); } onError?.(new _uploadError.UploadError({ code: 'GENERAL', message, file, cause: error instanceof Error ? error : undefined })); } }); } //# sourceMappingURL=upload-media.js.map