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