UNPKG

@wordpress/upload-media

Version:
47 lines (43 loc) 1.32 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.validateMimeType = validateMimeType; var _i18n = require("@wordpress/i18n"); var _uploadError = require("./upload-error"); /** * WordPress dependencies */ /** * Internal dependencies */ /** * Verifies if the caller (e.g. a block) supports this mime type. * * @param file File object. * @param allowedTypes List of allowed mime types. */ function validateMimeType(file, allowedTypes) { if (!allowedTypes) { return; } // Allowed type specified by consumer. const isAllowedType = allowedTypes.some(allowedType => { // If a complete mimetype is specified verify if it matches exactly the mime type of the file. if (allowedType.includes('/')) { return allowedType === file.type; } // Otherwise a general mime type is used, and we should verify if the file mimetype starts with it. return file.type.startsWith(`${allowedType}/`); }); if (file.type && !isAllowedType) { throw new _uploadError.UploadError({ code: 'MIME_TYPE_NOT_SUPPORTED', message: (0, _i18n.sprintf)( // translators: %s: file name. (0, _i18n.__)('%s: Sorry, this file type is not supported here.'), file.name), file }); } } //# sourceMappingURL=validate-mime-type.js.map