summaryrefslogtreecommitdiff
blob: dfd558beba1844245f3f74e69145a58f25b91804 (plain)
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
/* globals wp, lodash */
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 } } )
			.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;
	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 ( 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 );
			} );
	} );
} );