summaryrefslogtreecommitdiff
blob: de5f9a675e7185b97b30919080de235c44b2468c (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
150
151
152
153
154
155
156
157
/* globals plupload, pluploadL10n, error */
window.wp = window.wp || {};

( function( wp ) {
	var VideoPress = {
		originalOptions: {},

		/**
		 * This is the standard uploader response handler.
		 */
		handleStandardResponse: function( response, file ) {
			if ( ! _.isObject( response ) || _.isUndefined( response.success ) ) {
				return error( pluploadL10n.default_error, null, file );
			} else if ( ! response.success ) {
				return error( response.data && response.data.message, response.data, file );
			}

			return response;
		},

		/**
		 * Handle response from the WPCOM Rest API.
		 */
		handleRestApiResponse: function( response, file ) {
			if ( response.media.length !== 1 ) {
				return error( pluploadL10n.default_error, null, file );
			}

			var media = response.media[ 0 ],
				mimeParts = media.mime_type.split( '/' ),
				data = {
					alt: '',
					author: media.author_ID || 0,
					authorName: '',
					caption: '',
					compat: { item: '', meta: '' },
					date: media.date || '',
					dateFormatted: media.date || '',
					description: media.description || '',
					editLink: '',
					filename: media.file || '',
					filesizeHumanReadable: '',
					filesizeInBytes: '',
					height: media.height,
					icon: media.icon || '',
					id: media.ID || '',
					link: media.URL || '',
					menuOrder: 0,
					meta: false,
					mime: media.mime_type || '',
					modified: 0,
					name: '',
					nonces: { update: '', delete: '', edit: '' },
					orientation: '',
					sizes: undefined,
					status: '',
					subtype: mimeParts[ 1 ] || '',
					title: media.title || '',
					type: mimeParts[ 0 ] || '',
					uploadedTo: 1,
					uploadedToLink: '',
					uploadedToTitle: '',
					url: media.URL || '',
					width: media.width,
					success: '',
					videopress: {
						guid: media.videopress_guid || null,
						processing_done: media.videopress_processing_done || false,
					},
				};

			response.data = data;

			return response;
		},

		/**
		 * Make sure that all of the original variables have been reset, so the uploader
		 * doesn't try to go to VideoPress again next time.
		 *
		 * @param up
		 */
		resetToOriginalOptions: function( up ) {
			if ( typeof VideoPress.originalOptions.url !== 'undefined' ) {
				up.setOption( 'url', VideoPress.originalOptions.url );
				delete VideoPress.originalOptions.url;
			}

			if ( typeof VideoPress.originalOptions.multipart_params !== 'undefined' ) {
				up.setOption( 'multipart_params', VideoPress.originalOptions.multipart_params );
				delete VideoPress.originalOptions.multipart_params;
			}

			if ( typeof VideoPress.originalOptions.file_data_name !== 'undefined' ) {
				up.setOption( 'file_data_name', VideoPress.originalOptions.file_data_name );
				delete VideoPress.originalOptions.file_data_name;
			}
		},
	};

	if ( typeof wp.Uploader !== 'undefined' ) {
		var media = wp.media;

		/**
		 * A plupload code specifically for videopress failures.
		 *
		 * @type {string}
		 */
		plupload.VIDEOPRESS_TOKEN_FAILURE = 'VP_TOKEN_FAILURE';

		/**
		 * Adds a filter that checks all files to see if they are videopress files and if they are
		 * it will download extra metadata for them.
		 */
		plupload.addFileFilter( 'videopress_check_uploads', function( maxSize, file, cb ) {
			var mimeParts = file.type.split( '/' );
			var self = this;

			if ( mimeParts[ 0 ] === 'video' ) {
				media
					.ajax( 'videopress-get-upload-token', { async: false, data: { filename: file.name } } )
					.done( function( response ) {
						file.videopress = response;
						cb( true );
					} )
					.fail( function( response ) {
						self.trigger( 'Error', {
							code: plupload.VIDEOPRESS_TOKEN_FAILURE,
							message: plupload.translate(
								'Could not get the VideoPress token needed for uploading'
							),
							file: file,
							response: response,
						} );
						cb( false );
					} );
			} else {
				// Handles the normal max_file_size functionality.
				var undef;

				// Invalid file size
				if ( file.size !== undef && maxSize && file.size > maxSize ) {
					this.trigger( 'Error', {
						code: plupload.FILE_SIZE_ERROR,
						message: plupload.translate( 'File size error.' ),
						file: file,
					} );
					cb( false );
				} else {
					cb( true );
				}
			}
		} );
	}

	wp.VideoPress = VideoPress;
} )( window.wp );