diff options
author | Anthony G. Basile <blueness@gentoo.org> | 2020-01-06 14:32:30 -0500 |
---|---|---|
committer | Anthony G. Basile <blueness@gentoo.org> | 2020-01-06 14:32:30 -0500 |
commit | 10ef81bf85ad0a4bad0d204838e14c99ca2526f7 (patch) | |
tree | b4bb36a326d41de12d1a6181d2a2baf34696ac24 /plugins/jetpack/modules/videopress/js/gutenberg-video-upload.js | |
parent | Updating script for Update (diff) | |
download | blogs-gentoo-10ef81bf85ad0a4bad0d204838e14c99ca2526f7.tar.gz blogs-gentoo-10ef81bf85ad0a4bad0d204838e14c99ca2526f7.tar.bz2 blogs-gentoo-10ef81bf85ad0a4bad0d204838e14c99ca2526f7.zip |
Update jetpack 8.0
Signed-off-by: Anthony G. Basile <blueness@gentoo.org>
Diffstat (limited to 'plugins/jetpack/modules/videopress/js/gutenberg-video-upload.js')
-rw-r--r-- | plugins/jetpack/modules/videopress/js/gutenberg-video-upload.js | 63 |
1 files changed, 63 insertions, 0 deletions
diff --git a/plugins/jetpack/modules/videopress/js/gutenberg-video-upload.js b/plugins/jetpack/modules/videopress/js/gutenberg-video-upload.js new file mode 100644 index 00000000..c3ba1739 --- /dev/null +++ b/plugins/jetpack/modules/videopress/js/gutenberg-video-upload.js @@ -0,0 +1,63 @@ +/* globals wp, lodash */ + +wp.apiFetch.use( function( options, next ) { + var path = options.path; + var method = options.method; + var body = options.body; + var file = body ? body.get( 'file' ) : null; + + // Override only requests to the WP REST API media endpoint uploading new videos. + if ( ! path || path.indexOf( '/wp/v2/media' ) === -1 ) { + return next( options ); + } + if ( ! method || 'post' !== method.toLowerCase() ) { + return next( options ); + } + if ( ! file || file.type.indexOf( 'video/' ) !== 0 ) { + return next( options ); + } + + // Get upload token. + wp.media + .ajax( 'videopress-get-upload-token', { async: false, data: { filename: file.name } } ) + .done( function( response ) { + // Set auth header with upload token. + var headers = options.headers || {}; + headers.Authorization = + 'X_UPLOAD_TOKEN token="' + + response.upload_token + + '" blog_id="' + + response.upload_blog_id + + '"'; + options.headers = headers; + + // Replace upload URL. + delete options.path; + options.url = response.upload_action_url; + + // Handle CORS. + options.credentials = 'omit'; + + // Set data in expected param by WP.com media endpoint. + body.set( 'media[]', file ); + body.delete( 'file' ); + options.body = body; + } ); + + var result = next( options ); + + return new Promise( function( resolve, reject ) { + result + .then( function( data ) { + var wpcomMediaObject = lodash.get( data, 'media[0]' ); + var id = lodash.get( wpcomMediaObject, 'ID' ); + var gutenbergMediaObject = wp.apiFetch( { + path: '/wp/v2/media/' + id, + } ); + resolve( gutenbergMediaObject ); + } ) + .catch( function() { + reject(); + } ); + } ); +} ); |