Aucune description

videopress-uploader.js 4.2KB

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