@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.
Files changed (100) hide show
  1. package/CHANGELOG.md +6 -0
  2. package/README.md +70 -1
  3. package/build/index.js +43 -11
  4. package/build/index.js.map +1 -1
  5. package/build/utils/flatten-form-data.js +32 -0
  6. package/build/utils/flatten-form-data.js.map +1 -0
  7. package/build/utils/get-mime-types-array.js +27 -0
  8. package/build/utils/get-mime-types-array.js.map +1 -0
  9. package/build/utils/transform-attachment.js +33 -0
  10. package/build/utils/transform-attachment.js.map +1 -0
  11. package/build/utils/types.js +6 -0
  12. package/build/utils/types.js.map +1 -0
  13. package/build/utils/upload-error.js +29 -0
  14. package/build/utils/upload-error.js.map +1 -0
  15. package/build/utils/upload-media.js +53 -152
  16. package/build/utils/upload-media.js.map +1 -1
  17. package/build/utils/upload-to-server.js +34 -0
  18. package/build/utils/upload-to-server.js.map +1 -0
  19. package/build/utils/validate-file-size.js +44 -0
  20. package/build/utils/validate-file-size.js.map +1 -0
  21. package/build/utils/validate-mime-type-for-user.js +41 -0
  22. package/build/utils/validate-mime-type-for-user.js.map +1 -0
  23. package/build/utils/validate-mime-type.js +47 -0
  24. package/build/utils/validate-mime-type.js.map +1 -0
  25. package/build-module/index.js +5 -1
  26. package/build-module/index.js.map +1 -1
  27. package/build-module/utils/flatten-form-data.js +26 -0
  28. package/build-module/utils/flatten-form-data.js.map +1 -0
  29. package/build-module/utils/get-mime-types-array.js +21 -0
  30. package/build-module/utils/get-mime-types-array.js.map +1 -0
  31. package/build-module/utils/transform-attachment.js +27 -0
  32. package/build-module/utils/transform-attachment.js.map +1 -0
  33. package/build-module/utils/types.js +2 -0
  34. package/build-module/utils/types.js.map +1 -0
  35. package/build-module/utils/upload-error.js +22 -0
  36. package/build-module/utils/upload-error.js.map +1 -0
  37. package/build-module/utils/upload-media.js +53 -149
  38. package/build-module/utils/upload-media.js.map +1 -1
  39. package/build-module/utils/upload-to-server.js +26 -0
  40. package/build-module/utils/upload-to-server.js.map +1 -0
  41. package/build-module/utils/validate-file-size.js +38 -0
  42. package/build-module/utils/validate-file-size.js.map +1 -0
  43. package/build-module/utils/validate-mime-type-for-user.js +35 -0
  44. package/build-module/utils/validate-mime-type-for-user.js.map +1 -0
  45. package/build-module/utils/validate-mime-type.js +41 -0
  46. package/build-module/utils/validate-mime-type.js.map +1 -0
  47. package/build-types/components/index.d.ts +2 -0
  48. package/build-types/components/index.d.ts.map +1 -0
  49. package/build-types/components/media-upload/index.d.ts +30 -0
  50. package/build-types/components/media-upload/index.d.ts.map +1 -0
  51. package/build-types/index.d.ts +8 -0
  52. package/build-types/index.d.ts.map +1 -0
  53. package/build-types/utils/flatten-form-data.d.ts +9 -0
  54. package/build-types/utils/flatten-form-data.d.ts.map +1 -0
  55. package/build-types/utils/get-mime-types-array.d.ts +12 -0
  56. package/build-types/utils/get-mime-types-array.d.ts.map +1 -0
  57. package/build-types/utils/transform-attachment.d.ts +11 -0
  58. package/build-types/utils/transform-attachment.d.ts.map +1 -0
  59. package/build-types/utils/types.d.ts +199 -0
  60. package/build-types/utils/types.d.ts.map +1 -0
  61. package/build-types/utils/upload-error.d.ts +19 -0
  62. package/build-types/utils/upload-error.d.ts.map +1 -0
  63. package/build-types/utils/upload-media.d.ts +32 -0
  64. package/build-types/utils/upload-media.d.ts.map +1 -0
  65. package/build-types/utils/upload-to-server.d.ts +3 -0
  66. package/build-types/utils/upload-to-server.d.ts.map +1 -0
  67. package/build-types/utils/validate-file-size.d.ts +8 -0
  68. package/build-types/utils/validate-file-size.d.ts.map +1 -0
  69. package/build-types/utils/validate-mime-type-for-user.d.ts +8 -0
  70. package/build-types/utils/validate-mime-type-for-user.d.ts.map +1 -0
  71. package/build-types/utils/validate-mime-type.d.ts +8 -0
  72. package/build-types/utils/validate-mime-type.d.ts.map +1 -0
  73. package/package.json +8 -7
  74. package/src/index.ts +9 -0
  75. package/src/utils/flatten-form-data.ts +33 -0
  76. package/src/utils/get-mime-types-array.ts +29 -0
  77. package/src/utils/test/flatten-form-data.ts +49 -0
  78. package/src/utils/test/get-mime-types-array.ts +47 -0
  79. package/src/utils/test/upload-error.ts +24 -0
  80. package/src/utils/test/{upload-media.test.js → upload-media.ts} +47 -76
  81. package/src/utils/test/validate-file-size.ts +70 -0
  82. package/src/utils/test/validate-mime-type-for-user.ts +37 -0
  83. package/src/utils/test/validate-mime-type.ts +57 -0
  84. package/src/utils/transform-attachment.ts +24 -0
  85. package/src/utils/types.ts +207 -0
  86. package/src/utils/upload-error.ts +26 -0
  87. package/src/utils/upload-media.ts +149 -0
  88. package/src/utils/upload-to-server.ts +38 -0
  89. package/src/utils/validate-file-size.ts +44 -0
  90. package/src/utils/validate-mime-type-for-user.ts +46 -0
  91. package/src/utils/validate-mime-type.ts +43 -0
  92. package/tsconfig.json +17 -0
  93. package/tsconfig.tsbuildinfo +1 -0
  94. package/build/utils/index.js +0 -13
  95. package/build/utils/index.js.map +0 -1
  96. package/build-module/utils/index.js +0 -2
  97. package/build-module/utils/index.js.map +0 -1
  98. package/src/index.js +0 -2
  99. package/src/utils/index.js +0 -1
  100. package/src/utils/upload-media.js +0 -232
@@ -1 +1 @@
1
- {"version":3,"names":["_apiFetch","_interopRequireDefault","require","_blob","_i18n","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","revokeBlobURL","url","filter","Boolean","isAllowedType","fileType","some","allowedType","includes","startsWith","allowedMimeTypesForUser","isAllowedMimeTypeForUser","validFiles","mediaFile","code","message","sprintf","__","name","file","size","push","createBlobURL","length","_savedMedia$caption$r","savedMedia","createMediaFromFile","alt_text","source_url","savedMediaProps","mediaObject","alt","caption","raw","title","error","data","window","FormData","append","replace","forEach","key","apiFetch","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 apiFetch( {\n\t\tpath: '/wp/v2/media',\n\t\tbody: data,\n\t\tmethod: 'POST',\n\t} );\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EG,GAAG,EAAE3C,KAAM,CACzB,CAAC;EACF;EACA,OAAO,IAAA4C,iBAAQ,EAAE;IAChBC,IAAI,EAAE,cAAc;IACpBC,IAAI,EAAET,IAAI;IACVU,MAAM,EAAE;EACT,CAAE,CAAC;AACJ","ignoreList":[]}
1
+ {"version":3,"names":["_i18n","require","_blob","_uploadToServer","_validateMimeType","_validateMimeTypeForUser","_validateFileSize","_uploadError","uploadMedia","wpAllowedMimeTypes","allowedTypes","additionalData","filesList","maxUploadFileSize","onError","onFileChange","signal","validFiles","filesSet","setAndUpdateFiles","index","value","url","revokeBlobURL","filter","attachment","mediaFile","validateMimeTypeForUser","error","validateMimeType","validateFileSize","push","createBlobURL","map","file","uploadToServer","message","Error","sprintf","__","name","UploadError","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":";;;;;;AAGA,IAAAA,KAAA,GAAAC,OAAA;AACA,IAAAC,KAAA,GAAAD,OAAA;AAYA,IAAAE,eAAA,GAAAF,OAAA;AACA,IAAAG,iBAAA,GAAAH,OAAA;AACA,IAAAI,wBAAA,GAAAJ,OAAA;AACA,IAAAK,iBAAA,GAAAL,OAAA;AACA,IAAAM,YAAA,GAAAN,OAAA;AApBA;AACA;AACA;;AAIA;AACA;AACA;;AAmCA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;A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AAAU,mBAAa,EAAEN,SAAU;IAAE,CAAE,CAAC;IACpDX,YAAY,GAAIG,QAA2C,CAAC;EAC7D;EAEAD,UAAU,CAACgB,GAAG,CAAE,OAAQC,IAAI,EAAEd,KAAK,KAAM;IACxC,IAAI;MACH,MAAMK,UAAU,GAAG,MAAM,IAAAU,8BAAc,EACtCD,IAAI,EACJvB,cAAc,EACdK,MACD,CAAC;MACDG,iBAAiB,CAAEC,KAAK,EAAEK,UAAW,CAAC;IACvC,CAAC,CAAC,OAAQG,KAAK,EAAG;MACjB;MACAT,iBAAiB,CAAEC,KAAK,EAAE,IAAK,CAAC;MAEhC,IAAIgB,OAAO;MACX,IAAKR,KAAK,YAAYS,KAAK,EAAG;QAC7BD,OAAO,GAAGR,KAAK,CAACQ,OAAO;MACxB,CAAC,MAAM;QACNA,OAAO,GAAG,IAAAE,aAAO;QAChB;QACA,IAAAC,QAAE,EAAE,qDAAsD,CAAC,EAC3DL,IAAI,CAACM,IACN,CAAC;MACF;MAEA1B,OAAO,GACN,IAAI2B,wBAAW,CAAE;QAChBC,IAAI,EAAE,SAAS;QACfN,OAAO;QACPF,IAAI;QACJS,KAAK,EAAEf,KAAK,YAAYS,KAAK,GAAGT,KAAK,GAAGgB;MACzC,CAAE,CACH,CAAC;IACF;EACD,CAAE,CAAC;AACJ","ignoreList":[]}
@@ -0,0 +1,34 @@
1
+ "use strict";
2
+
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
+ Object.defineProperty(exports, "__esModule", {
5
+ value: true
6
+ });
7
+ exports.uploadToServer = uploadToServer;
8
+ var _apiFetch = _interopRequireDefault(require("@wordpress/api-fetch"));
9
+ var _flattenFormData = require("./flatten-form-data");
10
+ var _transformAttachment = require("./transform-attachment");
11
+ /**
12
+ * WordPress dependencies
13
+ */
14
+
15
+ /**
16
+ * Internal dependencies
17
+ */
18
+
19
+ async function uploadToServer(file, additionalData = {}, signal) {
20
+ // Create upload payload.
21
+ const data = new FormData();
22
+ data.append('file', file, file.name || file.type.replace('/', '.'));
23
+ for (const [key, value] of Object.entries(additionalData)) {
24
+ (0, _flattenFormData.flattenFormData)(data, key, value);
25
+ }
26
+ return (0, _transformAttachment.transformAttachment)(await (0, _apiFetch.default)({
27
+ // This allows the video block to directly get a video's poster image.
28
+ path: '/wp/v2/media?_embed=wp:featuredmedia',
29
+ body: data,
30
+ method: 'POST',
31
+ signal
32
+ }));
33
+ }
34
+ //# sourceMappingURL=upload-to-server.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"names":["_apiFetch","_interopRequireDefault","require","_flattenFormData","_transformAttachment","uploadToServer","file","additionalData","signal","data","FormData","append","name","type","replace","key","value","Object","entries","flattenFormData","transformAttachment","apiFetch","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":";;;;;;;AAGA,IAAAA,SAAA,GAAAC,sBAAA,CAAAC,OAAA;AAKA,IAAAC,gBAAA,GAAAD,OAAA;AACA,IAAAE,oBAAA,GAAAF,OAAA;AATA;AACA;AACA;;AAGA;AACA;AACA;;AAKO,eAAeG,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;IAChE,IAAAY,gCAAe,EACdV,IAAI,EACJM,GAAG,EACHC,KACD,CAAC;EACF;EAEA,OAAO,IAAAI,wCAAmB,EACzB,MAAM,IAAAC,iBAAQ,EAAoB;IACjC;IACAC,IAAI,EAAE,sCAAsC;IAC5CC,IAAI,EAAEd,IAAI;IACVe,MAAM,EAAE,MAAM;IACdhB;EACD,CAAE,CACH,CAAC;AACF","ignoreList":[]}
@@ -0,0 +1,44 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.validateFileSize = validateFileSize;
7
+ var _i18n = require("@wordpress/i18n");
8
+ var _uploadError = require("./upload-error");
9
+ /**
10
+ * WordPress dependencies
11
+ */
12
+
13
+ /**
14
+ * Internal dependencies
15
+ */
16
+
17
+ /**
18
+ * Verifies whether the file is within the file upload size limits for the site.
19
+ *
20
+ * @param file File object.
21
+ * @param maxUploadFileSize Maximum upload size in bytes allowed for the site.
22
+ */
23
+ function validateFileSize(file, maxUploadFileSize) {
24
+ // Don't allow empty files to be uploaded.
25
+ if (file.size <= 0) {
26
+ throw new _uploadError.UploadError({
27
+ code: 'EMPTY_FILE',
28
+ message: (0, _i18n.sprintf)(
29
+ // translators: %s: file name.
30
+ (0, _i18n.__)('%s: This file is empty.'), file.name),
31
+ file
32
+ });
33
+ }
34
+ if (maxUploadFileSize && file.size > maxUploadFileSize) {
35
+ throw new _uploadError.UploadError({
36
+ code: 'SIZE_ABOVE_LIMIT',
37
+ message: (0, _i18n.sprintf)(
38
+ // translators: %s: file name.
39
+ (0, _i18n.__)('%s: This file exceeds the maximum upload size for this site.'), file.name),
40
+ file
41
+ });
42
+ }
43
+ }
44
+ //# sourceMappingURL=validate-file-size.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"names":["_i18n","require","_uploadError","validateFileSize","file","maxUploadFileSize","size","UploadError","code","message","sprintf","__","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":";;;;;;AAGA,IAAAA,KAAA,GAAAC,OAAA;AAKA,IAAAC,YAAA,GAAAD,OAAA;AARA;AACA;AACA;;AAGA;AACA;AACA;;AAGA;AACA;AACA;AACA;AACA;AACA;AACO,SAASE,gBAAgBA,CAAEC,IAAU,EAAEC,iBAA0B,EAAG;EAC1E;EACA,IAAKD,IAAI,CAACE,IAAI,IAAI,CAAC,EAAG;IACrB,MAAM,IAAIC,wBAAW,CAAE;MACtBC,IAAI,EAAE,YAAY;MAClBC,OAAO,EAAE,IAAAC,aAAO;MACf;MACA,IAAAC,QAAE,EAAE,yBAA0B,CAAC,EAC/BP,IAAI,CAACQ,IACN,CAAC;MACDR;IACD,CAAE,CAAC;EACJ;EAEA,IAAKC,iBAAiB,IAAID,IAAI,CAACE,IAAI,GAAGD,iBAAiB,EAAG;IACzD,MAAM,IAAIE,wBAAW,CAAE;MACtBC,IAAI,EAAE,kBAAkB;MACxBC,OAAO,EAAE,IAAAC,aAAO;MACf;MACA,IAAAC,QAAE,EACD,8DACD,CAAC,EACDP,IAAI,CAACQ,IACN,CAAC;MACDR;IACD,CAAE,CAAC;EACJ;AACD","ignoreList":[]}
@@ -0,0 +1,41 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.validateMimeTypeForUser = validateMimeTypeForUser;
7
+ var _i18n = require("@wordpress/i18n");
8
+ var _uploadError = require("./upload-error");
9
+ var _getMimeTypesArray = require("./get-mime-types-array");
10
+ /**
11
+ * WordPress dependencies
12
+ */
13
+
14
+ /**
15
+ * Internal dependencies
16
+ */
17
+
18
+ /**
19
+ * Verifies if the user is allowed to upload this mime type.
20
+ *
21
+ * @param file File object.
22
+ * @param wpAllowedMimeTypes List of allowed mime types and file extensions.
23
+ */
24
+ function validateMimeTypeForUser(file, wpAllowedMimeTypes) {
25
+ // Allowed types for the current WP_User.
26
+ const allowedMimeTypesForUser = (0, _getMimeTypesArray.getMimeTypesArray)(wpAllowedMimeTypes);
27
+ if (!allowedMimeTypesForUser) {
28
+ return;
29
+ }
30
+ const isAllowedMimeTypeForUser = allowedMimeTypesForUser.includes(file.type);
31
+ if (file.type && !isAllowedMimeTypeForUser) {
32
+ throw new _uploadError.UploadError({
33
+ code: 'MIME_TYPE_NOT_ALLOWED_FOR_USER',
34
+ message: (0, _i18n.sprintf)(
35
+ // translators: %s: file name.
36
+ (0, _i18n.__)('%s: Sorry, you are not allowed to upload this file type.'), file.name),
37
+ file
38
+ });
39
+ }
40
+ }
41
+ //# sourceMappingURL=validate-mime-type-for-user.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"names":["_i18n","require","_uploadError","_getMimeTypesArray","validateMimeTypeForUser","file","wpAllowedMimeTypes","allowedMimeTypesForUser","getMimeTypesArray","isAllowedMimeTypeForUser","includes","type","UploadError","code","message","sprintf","__","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":";;;;;;AAGA,IAAAA,KAAA,GAAAC,OAAA;AAKA,IAAAC,YAAA,GAAAD,OAAA;AACA,IAAAE,kBAAA,GAAAF,OAAA;AATA;AACA;AACA;;AAGA;AACA;AACA;;AAIA;AACA;AACA;AACA;AACA;AACA;AACO,SAASG,uBAAuBA,CACtCC,IAAU,EACVC,kBAAoD,EACnD;EACD;EACA,MAAMC,uBAAuB,GAAG,IAAAC,oCAAiB,EAAEF,kBAAmB,CAAC;EAEvE,IAAK,CAAEC,uBAAuB,EAAG;IAChC;EACD;EAEA,MAAME,wBAAwB,GAAGF,uBAAuB,CAACG,QAAQ,CAChEL,IAAI,CAACM,IACN,CAAC;EAED,IAAKN,IAAI,CAACM,IAAI,IAAI,CAAEF,wBAAwB,EAAG;IAC9C,MAAM,IAAIG,wBAAW,CAAE;MACtBC,IAAI,EAAE,gCAAgC;MACtCC,OAAO,EAAE,IAAAC,aAAO;MACf;MACA,IAAAC,QAAE,EACD,0DACD,CAAC,EACDX,IAAI,CAACY,IACN,CAAC;MACDZ;IACD,CAAE,CAAC;EACJ;AACD","ignoreList":[]}
@@ -0,0 +1,47 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.validateMimeType = validateMimeType;
7
+ var _i18n = require("@wordpress/i18n");
8
+ var _uploadError = require("./upload-error");
9
+ /**
10
+ * WordPress dependencies
11
+ */
12
+
13
+ /**
14
+ * Internal dependencies
15
+ */
16
+
17
+ /**
18
+ * Verifies if the caller (e.g. a block) supports this mime type.
19
+ *
20
+ * @param file File object.
21
+ * @param allowedTypes List of allowed mime types.
22
+ */
23
+ function validateMimeType(file, allowedTypes) {
24
+ if (!allowedTypes) {
25
+ return;
26
+ }
27
+
28
+ // Allowed type specified by consumer.
29
+ const isAllowedType = allowedTypes.some(allowedType => {
30
+ // If a complete mimetype is specified verify if it matches exactly the mime type of the file.
31
+ if (allowedType.includes('/')) {
32
+ return allowedType === file.type;
33
+ }
34
+ // Otherwise a general mime type is used, and we should verify if the file mimetype starts with it.
35
+ return file.type.startsWith(`${allowedType}/`);
36
+ });
37
+ if (file.type && !isAllowedType) {
38
+ throw new _uploadError.UploadError({
39
+ code: 'MIME_TYPE_NOT_SUPPORTED',
40
+ message: (0, _i18n.sprintf)(
41
+ // translators: %s: file name.
42
+ (0, _i18n.__)('%s: Sorry, this file type is not supported here.'), file.name),
43
+ file
44
+ });
45
+ }
46
+ }
47
+ //# sourceMappingURL=validate-mime-type.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"names":["_i18n","require","_uploadError","validateMimeType","file","allowedTypes","isAllowedType","some","allowedType","includes","type","startsWith","UploadError","code","message","sprintf","__","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":";;;;;;AAGA,IAAAA,KAAA,GAAAC,OAAA;AAKA,IAAAC,YAAA,GAAAD,OAAA;AARA;AACA;AACA;;AAGA;AACA;AACA;;AAGA;AACA;AACA;AACA;AACA;AACA;AACO,SAASE,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,IAAIM,wBAAW,CAAE;MACtBC,IAAI,EAAE,yBAAyB;MAC/BC,OAAO,EAAE,IAAAC,aAAO;MACf;MACA,IAAAC,QAAE,EAAE,kDAAmD,CAAC,EACxDZ,IAAI,CAACa,IACN,CAAC;MACDb;IACD,CAAE,CAAC;EACJ;AACD","ignoreList":[]}
@@ -1,3 +1,7 @@
1
1
  export * from './components';
2
- export * from './utils';
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';
3
7
  //# sourceMappingURL=index.js.map
@@ -1 +1 @@
1
- {"version":3,"names":[],"sources":["@wordpress/media-utils/src/index.js"],"sourcesContent":["export * from './components';\nexport * from './utils';\n"],"mappings":"AAAA,cAAc,cAAc;AAC5B,cAAc,SAAS","ignoreList":[]}
1
+ {"version":3,"names":["uploadMedia","transformAttachment","validateFileSize","validateMimeType","validateMimeTypeForUser"],"sources":["@wordpress/media-utils/src/index.ts"],"sourcesContent":["export * from './components';\n\nexport { uploadMedia } from './utils/upload-media';\nexport { transformAttachment } from './utils/transform-attachment';\nexport { validateFileSize } from './utils/validate-file-size';\nexport { validateMimeType } from './utils/validate-mime-type';\nexport { validateMimeTypeForUser } from './utils/validate-mime-type-for-user';\n\nexport type { Attachment, RestAttachment } from './utils/types';\n"],"mappings":"AAAA,cAAc,cAAc;AAE5B,SAASA,WAAW,QAAQ,sBAAsB;AAClD,SAASC,mBAAmB,QAAQ,8BAA8B;AAClE,SAASC,gBAAgB,QAAQ,4BAA4B;AAC7D,SAASC,gBAAgB,QAAQ,4BAA4B;AAC7D,SAASC,uBAAuB,QAAQ,qCAAqC","ignoreList":[]}
@@ -0,0 +1,26 @@
1
+ /**
2
+ * Determines whether the passed argument appears to be a plain object.
3
+ *
4
+ * @param data The object to inspect.
5
+ */
6
+ function isPlainObject(data) {
7
+ return data !== null && typeof data === 'object' && Object.getPrototypeOf(data) === Object.prototype;
8
+ }
9
+
10
+ /**
11
+ * Recursively flatten data passed to form data, to allow using multi-level objects.
12
+ *
13
+ * @param {FormData} formData Form data object.
14
+ * @param {string} key Key to amend to form data object
15
+ * @param {string|Object} data Data to be amended to form data.
16
+ */
17
+ export function flattenFormData(formData, key, data) {
18
+ if (isPlainObject(data)) {
19
+ for (const [name, value] of Object.entries(data)) {
20
+ flattenFormData(formData, `${key}[${name}]`, value);
21
+ }
22
+ } else if (data !== undefined) {
23
+ formData.append(key, String(data));
24
+ }
25
+ }
26
+ //# sourceMappingURL=flatten-form-data.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"names":["isPlainObject","data","Object","getPrototypeOf","prototype","flattenFormData","formData","key","name","value","entries","undefined","append","String"],"sources":["@wordpress/media-utils/src/utils/flatten-form-data.ts"],"sourcesContent":["/**\n * Determines whether the passed argument appears to be a plain object.\n *\n * @param data The object to inspect.\n */\nfunction isPlainObject( data: unknown ): data is Record< string, unknown > {\n\treturn (\n\t\tdata !== null &&\n\t\ttypeof data === 'object' &&\n\t\tObject.getPrototypeOf( data ) === Object.prototype\n\t);\n}\n\n/**\n * Recursively flatten data passed to form data, to allow using multi-level objects.\n *\n * @param {FormData} formData Form data object.\n * @param {string} key Key to amend to form data object\n * @param {string|Object} data Data to be amended to form data.\n */\nexport function flattenFormData(\n\tformData: FormData,\n\tkey: string,\n\tdata: string | undefined | Record< string, string >\n) {\n\tif ( isPlainObject( data ) ) {\n\t\tfor ( const [ name, value ] of Object.entries( data ) ) {\n\t\t\tflattenFormData( formData, `${ key }[${ name }]`, value );\n\t\t}\n\t} else if ( data !== undefined ) {\n\t\tformData.append( key, String( data ) );\n\t}\n}\n"],"mappings":"AAAA;AACA;AACA;AACA;AACA;AACA,SAASA,aAAaA,CAAEC,IAAa,EAAsC;EAC1E,OACCA,IAAI,KAAK,IAAI,IACb,OAAOA,IAAI,KAAK,QAAQ,IACxBC,MAAM,CAACC,cAAc,CAAEF,IAAK,CAAC,KAAKC,MAAM,CAACE,SAAS;AAEpD;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,OAAO,SAASC,eAAeA,CAC9BC,QAAkB,EAClBC,GAAW,EACXN,IAAmD,EAClD;EACD,IAAKD,aAAa,CAAEC,IAAK,CAAC,EAAG;IAC5B,KAAM,MAAM,CAAEO,IAAI,EAAEC,KAAK,CAAE,IAAIP,MAAM,CAACQ,OAAO,CAAET,IAAK,CAAC,EAAG;MACvDI,eAAe,CAAEC,QAAQ,EAAG,GAAGC,GAAK,IAAIC,IAAM,GAAE,EAAEC,KAAM,CAAC;IAC1D;EACD,CAAC,MAAM,IAAKR,IAAI,KAAKU,SAAS,EAAG;IAChCL,QAAQ,CAACM,MAAM,CAAEL,GAAG,EAAEM,MAAM,CAAEZ,IAAK,CAAE,CAAC;EACvC;AACD","ignoreList":[]}
@@ -0,0 +1,21 @@
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 function getMimeTypesArray(wpMimeTypesObject) {
12
+ if (!wpMimeTypesObject) {
13
+ return null;
14
+ }
15
+ return Object.entries(wpMimeTypesObject).flatMap(([extensionsString, mime]) => {
16
+ const [type] = mime.split('/');
17
+ const extensions = extensionsString.split('|');
18
+ return [mime, ...extensions.map(extension => `${type}/${extension}`)];
19
+ });
20
+ }
21
+ //# sourceMappingURL=get-mime-types-array.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"names":["getMimeTypesArray","wpMimeTypesObject","Object","entries","flatMap","extensionsString","mime","type","split","extensions","map","extension"],"sources":["@wordpress/media-utils/src/utils/get-mime-types-array.ts"],"sourcesContent":["/**\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 *\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 An array of mime types or null\n */\nexport function getMimeTypesArray(\n\twpMimeTypesObject?: Record< string, string > | null\n) {\n\tif ( ! wpMimeTypesObject ) {\n\t\treturn null;\n\t}\n\treturn Object.entries( wpMimeTypesObject ).flatMap(\n\t\t( [ 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);\n}\n"],"mappings":"AAAA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,OAAO,SAASA,iBAAiBA,CAChCC,iBAAmD,EAClD;EACD,IAAK,CAAEA,iBAAiB,EAAG;IAC1B,OAAO,IAAI;EACZ;EACA,OAAOC,MAAM,CAACC,OAAO,CAAEF,iBAAkB,CAAC,CAACG,OAAO,CACjD,CAAE,CAAEC,gBAAgB,EAAEC,IAAI,CAAE,KAAM;IACjC,MAAM,CAAEC,IAAI,CAAE,GAAGD,IAAI,CAACE,KAAK,CAAE,GAAI,CAAC;IAClC,MAAMC,UAAU,GAAGJ,gBAAgB,CAACG,KAAK,CAAE,GAAI,CAAC;IAChD,OAAO,CACNF,IAAI,EACJ,GAAGG,UAAU,CAACC,GAAG,CACdC,SAAS,IAAO,GAAGJ,IAAM,IAAII,SAAW,EAC3C,CAAC,CACD;EACF,CACD,CAAC;AACF","ignoreList":[]}
@@ -0,0 +1,27 @@
1
+ /**
2
+ * Internal dependencies
3
+ */
4
+
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 function transformAttachment(attachment) {
11
+ var _attachment$caption$r;
12
+ // eslint-disable-next-line camelcase
13
+ const {
14
+ alt_text,
15
+ source_url,
16
+ ...savedMediaProps
17
+ } = attachment;
18
+ return {
19
+ ...savedMediaProps,
20
+ alt: attachment.alt_text,
21
+ caption: (_attachment$caption$r = attachment.caption?.raw) !== null && _attachment$caption$r !== void 0 ? _attachment$caption$r : '',
22
+ title: attachment.title.raw,
23
+ url: attachment.source_url,
24
+ poster: attachment._embedded?.['wp:featuredmedia']?.[0]?.source_url || undefined
25
+ };
26
+ }
27
+ //# sourceMappingURL=transform-attachment.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"names":["transformAttachment","attachment","_attachment$caption$r","alt_text","source_url","savedMediaProps","alt","caption","raw","title","url","poster","_embedded","undefined"],"sources":["@wordpress/media-utils/src/utils/transform-attachment.ts"],"sourcesContent":["/**\n * Internal dependencies\n */\nimport type { Attachment, RestAttachment } from './types';\n\n/**\n * Transforms an attachment object from the REST API shape into the shape expected by the block editor and other consumers.\n *\n * @param attachment REST API attachment object.\n */\nexport function transformAttachment( attachment: RestAttachment ): Attachment {\n\t// eslint-disable-next-line camelcase\n\tconst { alt_text, source_url, ...savedMediaProps } = attachment;\n\treturn {\n\t\t...savedMediaProps,\n\t\talt: attachment.alt_text,\n\t\tcaption: attachment.caption?.raw ?? '',\n\t\ttitle: attachment.title.raw,\n\t\turl: attachment.source_url,\n\t\tposter:\n\t\t\tattachment._embedded?.[ 'wp:featuredmedia' ]?.[ 0 ]?.source_url ||\n\t\t\tundefined,\n\t};\n}\n"],"mappings":"AAAA;AACA;AACA;;AAGA;AACA;AACA;AACA;AACA;AACA,OAAO,SAASA,mBAAmBA,CAAEC,UAA0B,EAAe;EAAA,IAAAC,qBAAA;EAC7E;EACA,MAAM;IAAEC,QAAQ;IAAEC,UAAU;IAAE,GAAGC;EAAgB,CAAC,GAAGJ,UAAU;EAC/D,OAAO;IACN,GAAGI,eAAe;IAClBC,GAAG,EAAEL,UAAU,CAACE,QAAQ;IACxBI,OAAO,GAAAL,qBAAA,GAAED,UAAU,CAACM,OAAO,EAAEC,GAAG,cAAAN,qBAAA,cAAAA,qBAAA,GAAI,EAAE;IACtCO,KAAK,EAAER,UAAU,CAACQ,KAAK,CAACD,GAAG;IAC3BE,GAAG,EAAET,UAAU,CAACG,UAAU;IAC1BO,MAAM,EACLV,UAAU,CAACW,SAAS,GAAI,kBAAkB,CAAE,GAAI,CAAC,CAAE,EAAER,UAAU,IAC/DS;EACF,CAAC;AACF","ignoreList":[]}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=types.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"names":[],"sources":["@wordpress/media-utils/src/utils/types.ts"],"sourcesContent":["/**\n * A media attachment object in a REST API context.\n *\n * Simplified version of what's defined in the wp-types package.\n *\n * @see https://www.npmjs.com/package/wp-types\n */\ninterface WP_REST_API_Attachment {\n\t/**\n\t * Unique identifier for the attachment.\n\t */\n\tid: number;\n\t/**\n\t * The ID of the featured media for the post.\n\t */\n\tfeatured_media: number;\n\t/**\n\t * URL to the attachment.\n\t */\n\tlink: string;\n\t/**\n\t * The date the attachment was published, in the site's timezone.\n\t */\n\tdate: string;\n\t/**\n\t * The date the attachment was published, as GMT.\n\t */\n\tdate_gmt: string;\n\t/**\n\t * The date the attachment was last modified, in the site's timezone.\n\t */\n\tmodified: string;\n\t/**\n\t * The date the attachment was last modified, as GMT.\n\t */\n\tmodified_gmt: string;\n\t/**\n\t * An alphanumeric identifier for the attachment unique to its type.\n\t */\n\tslug: string;\n\t/**\n\t * A named status for the attachment.\n\t */\n\tstatus: string;\n\t/**\n\t * Type of Post for the attachment.\n\t */\n\ttype: 'attachment';\n\t/**\n\t * Alternative text to display when attachment is not displayed.\n\t */\n\talt_text: string;\n\t/**\n\t * The attachment caption.\n\t */\n\tcaption: {\n\t\t/**\n\t\t * Caption for the attachment, as it exists in the database. Only present when using the 'edit' context.\n\t\t */\n\t\traw?: string;\n\t\t/**\n\t\t * HTML caption for the attachment, transformed for display.\n\t\t */\n\t\trendered: string;\n\t};\n\t/**\n\t * The attachment description.\n\t */\n\tdescription: {\n\t\t/**\n\t\t * Description for the attachment, as it exists in the database. Only present when using the 'edit' context.\n\t\t */\n\t\traw?: string;\n\t\t/**\n\t\t * HTML description for the attachment, transformed for display.\n\t\t */\n\t\trendered: string;\n\t};\n\t/**\n\t * Attachment type.\n\t */\n\tmedia_type: 'image' | 'file';\n\t/**\n\t * The attachment MIME type.\n\t */\n\tmime_type: string;\n\t/**\n\t * Details about the media file, specific to its type.\n\t */\n\tmedia_details: {\n\t\t[ k: string ]: unknown;\n\t};\n\t/**\n\t * The ID for the associated post of the attachment.\n\t */\n\tpost: number | null;\n\t/**\n\t * URL to the original attachment file.\n\t */\n\tsource_url: string;\n\t/**\n\t * List of the missing image sizes of the attachment. Only present when using the 'edit' context.\n\t */\n\tmissing_image_sizes?: string[];\n\t/**\n\t * Permalink template for the attachment. Only present when using the 'edit' context and the post type is public.\n\t */\n\tpermalink_template?: string;\n\t/**\n\t * Slug automatically generated from the attachment title. Only present when using the 'edit' context and the post type is public.\n\t */\n\tgenerated_slug?: string;\n\t/**\n\t * An array of the class names for the post container element.\n\t */\n\tclass_list: string[];\n\t/**\n\t * The title for the attachment.\n\t */\n\ttitle: {\n\t\t/**\n\t\t * Title for the attachment, as it exists in the database. Only present when using the 'edit' context.\n\t\t */\n\t\traw?: string;\n\t\t/**\n\t\t * HTML title for the attachment, transformed for display.\n\t\t */\n\t\trendered: string;\n\t};\n\t/**\n\t * The ID for the author of the attachment.\n\t */\n\tauthor: number;\n\t/**\n\t * Whether or not comments are open on the attachment.\n\t */\n\tcomment_status: string;\n\t/**\n\t * Whether or not the attachment can be pinged.\n\t */\n\tping_status: string;\n\t/**\n\t * Meta fields.\n\t */\n\tmeta:\n\t\t| []\n\t\t| {\n\t\t\t\t[ k: string ]: unknown;\n\t\t };\n\t/**\n\t * The theme file to use to display the attachment.\n\t */\n\ttemplate: string;\n\t_links: {\n\t\t[ k: string ]: {\n\t\t\thref: string;\n\t\t\tembeddable?: boolean;\n\t\t\t[ k: string ]: unknown;\n\t\t}[];\n\t};\n\t/**\n\t * The embedded representation of relations. Only present when the '_embed' query parameter is set.\n\t */\n\t_embedded?: {\n\t\t/**\n\t\t * The author of the post.\n\t\t */\n\t\tauthor: unknown[];\n\t\t/**\n\t\t * The featured image post.\n\t\t */\n\t\t'wp:featuredmedia'?: WP_REST_API_Attachment[];\n\t\t[ k: string ]: unknown;\n\t};\n\t[ k: string ]: unknown;\n}\n\n/**\n * REST API attachment object with additional fields added by this project.\n */\nexport interface RestAttachment extends WP_REST_API_Attachment {}\n\ntype BetterOmit< T, K extends PropertyKey > = {\n\t[ P in keyof T as P extends K ? never : P ]: T[ P ];\n};\n\n/**\n * Transformed attachment object.\n */\nexport type Attachment = BetterOmit<\n\tRestAttachment,\n\t'alt_text' | 'source_url' | 'caption' | 'title'\n> & {\n\talt: WP_REST_API_Attachment[ 'alt_text' ];\n\tcaption: WP_REST_API_Attachment[ 'caption' ][ 'raw' ] & string;\n\ttitle: WP_REST_API_Attachment[ 'title' ][ 'raw' ];\n\turl: WP_REST_API_Attachment[ 'source_url' ];\n\tposter?: WP_REST_API_Attachment[ 'source_url' ];\n};\n\nexport type OnChangeHandler = ( attachments: Partial< Attachment >[] ) => void;\nexport type OnSuccessHandler = ( attachments: Partial< Attachment >[] ) => void;\nexport type OnErrorHandler = ( error: Error ) => void;\n\nexport type CreateRestAttachment = Partial< RestAttachment >;\n\nexport type AdditionalData = BetterOmit< CreateRestAttachment, 'meta' >;\n"],"mappings":"","ignoreList":[]}
@@ -0,0 +1,22 @@
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
+ });
17
+ Object.setPrototypeOf(this, new.target.prototype);
18
+ this.code = code;
19
+ this.file = file;
20
+ }
21
+ }
22
+ //# sourceMappingURL=upload-error.js.map
@@ -0,0 +1 @@
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":[]}