PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 16.3-a.5
Jetpack – WP Security, Backup, Speed, & Growth v16.3-a.5
16.3-a.5 16.3-a.7 16.3-a.3 16.3-a.1 16.2 16.2-beta 12.0.3 12.1.3 12.2.3 12.3.2 12.4.2 12.5.2 12.6.4 12.7.3 12.8.3 12.9.5 13.0.2 13.1.5 13.2.4 13.3.3 13.4.5 13.5.2 13.6.2 13.7.2 13.8.3 All 506 releases
jetpack / modules / videopress / js / gutenberg-video-upload.js

gutenberg-video-upload.js in Jetpack – WP Security, Backup, Speed, & Growth 16.3-a.5, at modules/videopress/js/gutenberg-video-upload.js

220 lines 5.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 window.videoPressUploadPoster = function ( guid, data ) {
2 return new Promise( function ( resolve, reject ) {
3 wp.media.ajax( 'videopress-get-upload-token', { async: true } ).done( function ( response ) {
4 // Set auth header with upload token.
5 var headers = {},
6 options = {};
7 var body = new FormData();
8 headers.Authorization =
9 'X_UPLOAD_TOKEN token="' +
10 response.upload_token +
11 '" blog_id="' +
12 response.upload_blog_id +
13 '"';
14 options.headers = headers;
15 options.method = 'POST';
16 options.url = 'https://public-api.wordpress.com/rest/v1.1/videos/' + guid + '/poster';
17
18 // Handle CORS.
19 options.credentials = 'omit';
20
21 Object.keys( data ).forEach( key => {
22 body.append( key, data[ key ] );
23 } );
24
25 options.body = body;
26
27 wp.apiFetch( options )
28 .then( function ( res ) {
29 resolve( res );
30 } )
31 .catch( function ( error ) {
32 reject( error );
33 } );
34 } );
35 } );
36 };
37
38 window.videoPressGetPoster = function ( guid ) {
39 const getPosterRequest = ( resolve, reject, jwt = null ) => {
40 let url = 'https://public-api.wordpress.com/rest/v1.1/videos/' + guid + '/poster';
41 if ( jwt && jwt.length ) {
42 url += '?metadata_token=' + jwt;
43 }
44
45 wp.apiFetch( {
46 url: url,
47 method: 'GET',
48 credentials: 'omit',
49 } )
50 .then( function ( res ) {
51 resolve( res );
52 } )
53 .catch( function ( error ) {
54 reject( error );
55 } );
56 };
57
58 return new Promise( function ( resolve, reject ) {
59 wp.ajax
60 .post( 'videopress-get-playback-jwt', {
61 async: true,
62 guid: guid,
63 } )
64 .done( function ( response ) {
65 getPosterRequest( resolve, reject, response.jwt );
66 } )
67 .fail( () => {
68 // Also try on ajax failure if the video doesn't need a jwt anyway
69 getPosterRequest( resolve, reject );
70 } );
71 } );
72 };
73
74 window.videoPressUploadTrack = function ( guid, kind, srcLang, label, tmpFile ) {
75 return new Promise( function ( resolve, reject ) {
76 wp.media
77 .ajax( 'videopress-get-upload-token', { async: true, data: { filename: tmpFile.name } } ) // todo: maybe remove filename from here (not needed)
78 .done( function ( response ) {
79 // Set auth header with upload token.
80 var headers = {},
81 options = {};
82 var body = new FormData();
83 headers.Authorization =
84 'X_UPLOAD_TOKEN token="' +
85 response.upload_token +
86 '" blog_id="' +
87 response.upload_blog_id +
88 '"';
89 options.headers = headers;
90 options.method = 'POST';
91 options.url = 'https://public-api.wordpress.com/rest/v1.1/videos/' + guid + '/tracks';
92
93 // Handle CORS.
94 options.credentials = 'omit';
95
96 body.append( 'kind', kind );
97 body.append( 'srclang', srcLang );
98 body.append( 'label', label );
99 body.append( 'vtt', tmpFile );
100
101 options.body = body;
102
103 wp.apiFetch( options )
104 .then( function ( res ) {
105 resolve( res );
106 } )
107 .catch( function ( error ) {
108 reject( error );
109 } );
110 } );
111 } );
112 };
113
114 window.videoPressDeleteTrack = function ( guid, kind, srcLang ) {
115 return new Promise( function ( resolve, reject ) {
116 wp.media.ajax( 'videopress-get-upload-token', { async: true } ).done( function ( response ) {
117 // Set auth header with upload token.
118 var headers = {},
119 options = {};
120 var body = new FormData();
121 headers.Authorization =
122 'X_UPLOAD_TOKEN token="' +
123 response.upload_token +
124 '" blog_id="' +
125 response.upload_blog_id +
126 '"';
127 options.headers = headers;
128 options.method = 'POST';
129 options.url = 'https://public-api.wordpress.com/rest/v1.1/videos/' + guid + '/tracks/delete';
130
131 // Handle CORS.
132 options.credentials = 'omit';
133
134 body.append( 'kind', kind );
135 body.append( 'srclang', srcLang );
136 options.body = body;
137
138 wp.apiFetch( options )
139 .then( function ( res ) {
140 resolve( res );
141 } )
142 .catch( function ( error ) {
143 reject( error );
144 } );
145 } );
146 } );
147 };
148
149 wp.apiFetch.use( function ( options, next ) {
150 var path = options.path;
151 var method = options.method;
152 var body = options.body;
153
154 // Override only requests to the WP REST API media endpoint uploading new videos.
155 if ( ! path || path.indexOf( '/wp/v2/media' ) === -1 ) {
156 return next( options );
157 }
158 if ( ! method || 'post' !== method.toLowerCase() ) {
159 return next( options );
160 }
161 var file = body ? body.get( 'file' ) : null;
162 if ( ! file || file.type.indexOf( 'video/' ) !== 0 ) {
163 return next( options );
164 }
165
166 // Get upload token.
167 wp.media
168 .ajax( 'videopress-get-upload-token', { async: false, data: { filename: file.name } } )
169 .done( function ( response ) {
170 // Set auth header with upload token.
171 var headers = options.headers || {};
172 headers.Authorization =
173 'X_UPLOAD_TOKEN token="' +
174 response.upload_token +
175 '" blog_id="' +
176 response.upload_blog_id +
177 '"';
178 options.headers = headers;
179
180 // Replace upload URL.
181 delete options.path;
182 options.url = response.upload_action_url;
183
184 // Handle CORS.
185 options.credentials = 'omit';
186
187 // Set data in expected param by WP.com media endpoint.
188 body.set( 'media[]', file );
189 body.delete( 'file' );
190 options.body = body;
191 } );
192
193 var result = next( options );
194
195 return new Promise( function ( resolve, reject ) {
196 result
197 .then( function ( response ) {
198 if ( response instanceof Response && response.ok ) {
199 return response.json();
200 }
201
202 return response; // if not a response object, then its our parsed body so return that
203 } )
204 .then( function ( data ) {
205 var wpcomMediaObject = data?.media?.[ 0 ];
206 var id = wpcomMediaObject?.ID;
207 var gutenbergMediaObject = wp.apiFetch( {
208 path: '/wp/v2/media/' + id,
209 } );
210 resolve( gutenbergMediaObject );
211 } )
212 .catch( function ( error ) {
213 if ( 'errors' in error && 'object' === typeof error.errors && error.errors.length > 0 ) {
214 error = error.errors.shift();
215 }
216 reject( error );
217 } );
218 } );
219 } );
220