@wordpress/media-utils
Version:
WordPress Media Upload Utils.
8 lines (7 loc) • 2.02 kB
Source Map (JSON)
{
"version": 3,
"sources": ["../../src/utils/sideload-to-server.ts"],
"sourcesContent": ["/**\n * WordPress dependencies\n */\nimport apiFetch from '@wordpress/api-fetch';\n\n/**\n * Internal dependencies\n */\nimport type { CreateSideloadFile, RestAttachment, SubSizeData } from './types';\nimport { flattenFormData } from './flatten-form-data';\n\n/**\n * Uploads a file to the server without creating an attachment.\n *\n * Returns lightweight sub-size data instead of a full attachment.\n * The client accumulates these responses and sends them to the\n * finalize endpoint.\n *\n * @param file Media File to Save.\n * @param attachmentId Parent attachment ID.\n * @param additionalData Additional data to include in the request.\n * @param signal Abort signal.\n *\n * @return Sub-size data for the uploaded file.\n */\nexport async function sideloadToServer(\n\tfile: File,\n\tattachmentId: RestAttachment[ 'id' ],\n\tadditionalData: CreateSideloadFile = {},\n\tsignal?: AbortSignal\n): Promise< SubSizeData > {\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 apiFetch< SubSizeData >( {\n\t\tpath: `/wp/v2/media/${ attachmentId }/sideload`,\n\t\tbody: data,\n\t\tmethod: 'POST',\n\t\tsignal,\n\t} );\n}\n"],
"mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAGA,uBAAqB;AAMrB,+BAAgC;AAgBhC,eAAsB,iBACrB,MACA,cACA,iBAAqC,CAAC,GACtC,QACyB;AAEzB,QAAM,OAAO,IAAI,SAAS;AAC1B,OAAK,OAAQ,QAAQ,MAAM,KAAK,QAAQ,KAAK,KAAK,QAAS,KAAK,GAAI,CAAE;AACtE,aAAY,CAAE,KAAK,KAAM,KAAK,OAAO,QAAS,cAAe,GAAI;AAChE;AAAA,MACC;AAAA,MACA;AAAA,MACA;AAAA,IACD;AAAA,EACD;AAEA,aAAO,iBAAAA,SAAyB;AAAA,IAC/B,MAAM,gBAAiB,YAAa;AAAA,IACpC,MAAM;AAAA,IACN,QAAQ;AAAA,IACR;AAAA,EACD,CAAE;AACH;",
"names": ["apiFetch"]
}