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