Plugin Directory

source: jetpack/trunk/jetpack_vendor/automattic/jetpack-videopress/src/client/lib/video-tracks/index.ts @ 2832426

Last change on this file since 2832426 was 2832426, checked in by wpkaren, 20 months ago

Updating trunk to version 11.7-a.3

File size: 3.8 KB
Line 
1/**
2 * External dependencies
3 */
4import { isAtomicSite, isSimpleSite } from '@automattic/jetpack-shared-extension-utils';
5import apiFetch from '@wordpress/api-fetch';
6/**
7 * Internal dependencies
8 */
9import getMediaToken from '../get-media-token';
10import { DeleteTrackDataProps, UploadTrackDataProps } from './types';
11
12export const TRACK_KIND_OPTIONS = [
13        'subtitles',
14        'captions',
15        'descriptions',
16        'chapters',
17        'metadata',
18] as const;
19
20const shouldUseJetpackVideoFetch = () => ! ( isSimpleSite() || isAtomicSite() );
21
22const videoPressUploadTrack = function ( track: UploadTrackDataProps, guid: string ) {
23        return new Promise( function ( resolve, reject ) {
24                const { kind, srcLang, label, tmpFile: vttFile } = track;
25
26                getMediaToken( 'upload' ).then( ( { token, blogId } ) => {
27                        const body = new FormData();
28                        body.append( 'kind', kind );
29                        body.append( 'srclang', srcLang );
30                        body.append( 'label', label );
31                        body.append( 'vtt', vttFile );
32
33                        const requestOptions = {
34                                headers: {
35                                        // Set auth header with upload token.
36                                        Authorization: `X_UPLOAD_TOKEN token="${ token }" blog_id="${ blogId }"`,
37                                },
38                                method: 'POST',
39                                body,
40                        };
41
42                        fetch( `https://public-api.wordpress.com/rest/v1.1/videos/${ guid }/tracks`, requestOptions )
43                                .then( data => {
44                                        try {
45                                                return resolve( data.json() );
46                                        } catch ( error ) {
47                                                return reject( error );
48                                        }
49                                } )
50                                .catch( reject );
51                } );
52        } );
53};
54
55/**
56 * Uploads a track to a video.
57 * Uses different methods depending on Jetpack or WPCOM.
58 *
59 * @param {object} track - the track file
60 * @param {string} guid - the video guid
61 * @returns {Promise} the api request promise
62 */
63export const uploadTrackForGuid = ( track: UploadTrackDataProps, guid: string ) => {
64        const { kind, srcLang, label, tmpFile } = track;
65
66        if ( shouldUseJetpackVideoFetch() ) {
67                return videoPressUploadTrack( { kind, srcLang, label, tmpFile }, guid );
68        }
69
70        return new Promise( function ( resolve, reject ) {
71                return apiFetch( {
72                        method: 'POST',
73                        path: `/videos/${ guid }/tracks`,
74                        apiNamespace: 'rest/v1.1',
75                        global: true,
76                        parse: false,
77                        formData: [
78                                [ 'kind', kind ],
79                                [ 'srclang', srcLang ],
80                                [ 'label', label ],
81                                [ 'vtt', tmpFile ],
82                        ],
83                } )
84                        .then( data => {
85                                try {
86                                        return resolve( data.json() );
87                                } catch ( error ) {
88                                        return reject( error );
89                                }
90                        } )
91                        .catch( reject );
92        } );
93};
94
95const videoPressDeleteTrack = function ( { kind, srcLang }, guid ) {
96        // eslint-disable-next-line no-undef
97        return new Promise( function ( resolve, reject ) {
98                getMediaToken( 'upload' ).then( ( { token, blogId } ) => {
99                        const body = new FormData();
100                        body.append( 'kind', kind );
101                        body.append( 'srclang', srcLang );
102
103                        const requestOptions = {
104                                headers: {
105                                        // Set auth header with upload token.
106                                        Authorization: `X_UPLOAD_TOKEN token="${ token }" blog_id="${ blogId }"`,
107                                },
108                                method: 'POST',
109                                body,
110                        };
111
112                        fetch(
113                                `https://public-api.wordpress.com/rest/v1.1/videos/${ guid }/tracks/delete`,
114                                requestOptions
115                        )
116                                .then( data => {
117                                        try {
118                                                return resolve( data.json() );
119                                        } catch ( error ) {
120                                                return reject( error );
121                                        }
122                                } )
123                                .catch( reject );
124                } );
125        } );
126};
127
128/**
129 * -Deletes a track from a video.
130 * -Uses different methods depending on Jetpack or WPCOM.
131 *
132 * @param {object} track - the track file
133 * @param {string} guid - the video guid
134 * @returns {Promise} the api request promise
135 */
136export const deleteTrackForGuid = ( track: DeleteTrackDataProps, guid: string ) => {
137        const { kind, srcLang } = track;
138
139        if ( shouldUseJetpackVideoFetch() ) {
140                return videoPressDeleteTrack( { kind, srcLang }, guid );
141        }
142
143        const options = {
144                method: 'POST',
145                path: `/videos/${ guid }/tracks/delete`,
146                apiNamespace: 'rest/v1.1',
147                global: true,
148                parse: false,
149                formData: [
150                        [ 'kind', kind ],
151                        [ 'srclang', srcLang ],
152                ],
153        };
154
155        return apiFetch( options );
156};
Note: See TracBrowser for help on using the repository browser.