@wordpress/media-utils
Version:
WordPress Media Upload Utils.
8 lines (7 loc) • 1.7 kB
Source Map (JSON)
{
"version": 3,
"sources": ["../../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": ";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;",
"names": []
}