| 1 |
/* globals plupload, pluploadL10n, error */ |
| 2 |
window.wp = window.wp || {}; |
| 3 |
|
| 4 |
( function ( wp ) { |
| 5 |
var VideoPress = { |
| 6 |
originalOptions: {}, |
| 7 |
|
| 8 |
/** |
| 9 |
* This is the standard uploader response handler. |
| 10 |
*/ |
| 11 |
handleStandardResponse: function ( response, file ) { |
| 12 |
if ( |
| 13 |
response === null || |
| 14 |
typeof response !== 'object' || |
| 15 |
typeof response.success === 'undefined' |
| 16 |
) { |
| 17 |
return error( pluploadL10n.default_error, null, file ); |
| 18 |
} else if ( ! response.success ) { |
| 19 |
return error( response.data && response.data.message, response.data, file ); |
| 20 |
} |
| 21 |
|
| 22 |
return response; |
| 23 |
}, |
| 24 |
|
| 25 |
/** |
| 26 |
* Handle response from the WPCOM Rest API. |
| 27 |
*/ |
| 28 |
handleRestApiResponse: function ( response, file ) { |
| 29 |
if ( response.media.length !== 1 ) { |
| 30 |
return error( pluploadL10n.default_error, null, file ); |
| 31 |
} |
| 32 |
|
| 33 |
var media = response.media[ 0 ], |
| 34 |
mimeParts = media.mime_type.split( '/' ), |
| 35 |
data = { |
| 36 |
alt: '', |
| 37 |
author: media.author_ID || 0, |
| 38 |
authorName: '', |
| 39 |
caption: '', |
| 40 |
compat: { item: '', meta: '' }, |
| 41 |
date: media.date || '', |
| 42 |
dateFormatted: media.date || '', |
| 43 |
description: media.description || '', |
| 44 |
editLink: '', |
| 45 |
filename: media.file || '', |
| 46 |
filesizeHumanReadable: '', |
| 47 |
filesizeInBytes: '', |
| 48 |
height: media.height, |
| 49 |
icon: media.icon || '', |
| 50 |
id: media.ID || '', |
| 51 |
link: media.URL || '', |
| 52 |
menuOrder: 0, |
| 53 |
meta: false, |
| 54 |
mime: media.mime_type || '', |
| 55 |
modified: 0, |
| 56 |
name: '', |
| 57 |
nonces: { update: '', delete: '', edit: '' }, |
| 58 |
orientation: '', |
| 59 |
sizes: undefined, |
| 60 |
status: '', |
| 61 |
subtype: mimeParts[ 1 ] || '', |
| 62 |
title: media.title || '', |
| 63 |
type: mimeParts[ 0 ] || '', |
| 64 |
uploadedTo: 1, |
| 65 |
uploadedToLink: '', |
| 66 |
uploadedToTitle: '', |
| 67 |
url: media.URL || '', |
| 68 |
width: media.width, |
| 69 |
success: '', |
| 70 |
videopress: { |
| 71 |
guid: media.videopress_guid || null, |
| 72 |
processing_done: media.videopress_processing_done || false, |
| 73 |
}, |
| 74 |
}; |
| 75 |
|
| 76 |
response.data = data; |
| 77 |
|
| 78 |
return response; |
| 79 |
}, |
| 80 |
|
| 81 |
/** |
| 82 |
* Make sure that all of the original variables have been reset, so the uploader |
| 83 |
* doesn't try to go to VideoPress again next time. |
| 84 |
* |
| 85 |
* @param up |
| 86 |
*/ |
| 87 |
resetToOriginalOptions: function ( up ) { |
| 88 |
if ( typeof VideoPress.originalOptions.url !== 'undefined' ) { |
| 89 |
up.setOption( 'url', VideoPress.originalOptions.url ); |
| 90 |
delete VideoPress.originalOptions.url; |
| 91 |
} |
| 92 |
|
| 93 |
if ( typeof VideoPress.originalOptions.multipart_params !== 'undefined' ) { |
| 94 |
up.setOption( 'multipart_params', VideoPress.originalOptions.multipart_params ); |
| 95 |
delete VideoPress.originalOptions.multipart_params; |
| 96 |
} |
| 97 |
|
| 98 |
if ( typeof VideoPress.originalOptions.file_data_name !== 'undefined' ) { |
| 99 |
up.setOption( 'file_data_name', VideoPress.originalOptions.file_data_name ); |
| 100 |
delete VideoPress.originalOptions.file_data_name; |
| 101 |
} |
| 102 |
}, |
| 103 |
}; |
| 104 |
|
| 105 |
if ( typeof wp.Uploader !== 'undefined' ) { |
| 106 |
var media = wp.media; |
| 107 |
|
| 108 |
/** |
| 109 |
* A plupload code specifically for videopress failures. |
| 110 |
* |
| 111 |
* @type {string} |
| 112 |
*/ |
| 113 |
plupload.VIDEOPRESS_TOKEN_FAILURE = 'VP_TOKEN_FAILURE'; |
| 114 |
|
| 115 |
/** |
| 116 |
* Adds a filter that checks all files to see if they are videopress files and if they are |
| 117 |
* it will download extra metadata for them. |
| 118 |
*/ |
| 119 |
plupload.addFileFilter( 'videopress_check_uploads', function ( maxSize, file, cb ) { |
| 120 |
var mimeParts = file.type.split( '/' ); |
| 121 |
var self = this; |
| 122 |
|
| 123 |
if ( mimeParts[ 0 ] === 'video' ) { |
| 124 |
media |
| 125 |
.ajax( 'videopress-get-upload-token', { async: false, data: { filename: file.name } } ) |
| 126 |
.done( function ( response ) { |
| 127 |
file.videopress = response; |
| 128 |
cb( true ); |
| 129 |
} ) |
| 130 |
.fail( function ( response ) { |
| 131 |
self.trigger( 'Error', { |
| 132 |
code: plupload.VIDEOPRESS_TOKEN_FAILURE, |
| 133 |
message: plupload.translate( |
| 134 |
'Could not get the VideoPress token needed for uploading' |
| 135 |
), |
| 136 |
file: file, |
| 137 |
response: response, |
| 138 |
} ); |
| 139 |
cb( false ); |
| 140 |
} ); |
| 141 |
} else if ( typeof file.size !== 'undefined' && maxSize && file.size > maxSize ) { |
| 142 |
// Invalid file size |
| 143 |
this.trigger( 'Error', { |
| 144 |
code: plupload.FILE_SIZE_ERROR, |
| 145 |
message: plupload.translate( 'File size error.' ), |
| 146 |
file: file, |
| 147 |
} ); |
| 148 |
cb( false ); |
| 149 |
} else { |
| 150 |
// Handles the normal max_file_size functionality. |
| 151 |
cb( true ); |
| 152 |
} |
| 153 |
} ); |
| 154 |
} |
| 155 |
|
| 156 |
wp.VideoPress = VideoPress; |
| 157 |
} )( window.wp ); |
| 158 |
|