File size: 3,072 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import {
	VIDEO_EDITOR_UPDATE_POSTER,
	VIDEO_EDITOR_REFRESH_POSTER,
} from 'calypso/state/action-types';
import { registerHandlers } from 'calypso/state/data-layer/handler-registry';
import { http } from 'calypso/state/data-layer/wpcom-http/actions';
import { dispatchRequest } from 'calypso/state/data-layer/wpcom-http/utils';
import {
	setPosterUrl,
	showError,
	showUploadProgress,
} from 'calypso/state/editor/video-editor/actions';
import { receiveMedia } from 'calypso/state/media/actions';
import getMediaItem from 'calypso/state/selectors/get-media-item';
import { getSelectedSiteId } from 'calypso/state/ui/selectors';

// Internal action
const refreshAction = ( videoId, meta ) => ( {
	type: VIDEO_EDITOR_REFRESH_POSTER,
	meta: meta,
	videoId: videoId,
} );

const refresh = ( action ) => {
	const params = {
		apiVersion: '1.1',
		method: 'GET',
		path: `/videos/${ action.videoId }/poster`,
	};
	return http( params, action );
};

const fetch = ( action ) => {
	if ( ! ( 'file' in action.params || 'atTime' in action.params ) ) {
		return;
	}

	const { atTime, file, isMillisec } = action.params;
	const params = Object.assign(
		{
			apiVersion: '1.1',
			method: 'POST',
			path: `/videos/${ action.videoId }/poster`,
		},
		file && { formData: [ [ 'poster', file ] ] },
		atTime !== undefined && { body: { at_time: atTime, is_millisec: isMillisec } }
	);

	return http( params, action );
};

const onSuccess = ( action, data ) => ( dispatch, getState ) => {
	const { poster: posterUrl } = data;
	const isGeneratingThumbnail = !! data.generating;

	if ( isGeneratingThumbnail ) {
		setTimeout( () => {
			dispatch( refreshAction( action.videoId, { mediaId: action.meta.mediaId } ) );
		}, 1000 );

		dispatch( showUploadProgress( 100 ) );
		return;
	}

	dispatch( setPosterUrl( posterUrl ) );

	const currentState = getState();

	const siteId = getSelectedSiteId( currentState );
	const mediaItem = getMediaItem( currentState, siteId, action.meta.mediaId );

	// Photon does not support URLs with a querystring component.
	const urlBeforeQuery = ( posterUrl || '' ).split( '?' )[ 0 ];

	const updatedMediaItem = {
		...mediaItem,
		thumbnails: {
			fmt_hd: urlBeforeQuery,
			fmt_dvd: urlBeforeQuery,
			fmt_std: urlBeforeQuery,
		},
	};

	dispatch( receiveMedia( siteId, updatedMediaItem ) );
};

const onError = () => showError();

const onProgress = ( action, progress ) => {
	const hasProgressData = 'loaded' in progress && 'total' in progress;
	const percentage = hasProgressData
		? Math.min( Math.round( ( progress.loaded / ( Number.EPSILON + progress.total ) ) * 100 ), 100 )
		: 0;

	return showUploadProgress( percentage );
};

const dispatchUpdatePosterRequest = dispatchRequest( { fetch, onSuccess, onError, onProgress } );
const dispatchRefreshPosterRequest = dispatchRequest( {
	fetch: refresh,
	onSuccess,
	onError,
	onProgress,
} );

registerHandlers( 'state/data-layer/wpcom/videos/poster/index.js', {
	[ VIDEO_EDITOR_UPDATE_POSTER ]: [ dispatchUpdatePosterRequest ],
	[ VIDEO_EDITOR_REFRESH_POSTER ]: [ dispatchRefreshPosterRequest ],
} );