From e0e5ae928924c498da12887d104a16e8fc1e690a Mon Sep 17 00:00:00 2001 From: Mark Holmquist Date: Mon, 29 Jun 2015 09:41:05 -0500 Subject: [PATCH] Add stash support for mw.Api.upload Bug: T103399 Change-Id: I2be7d5d874861e4cc24897316cd4933eb85b2c8f --- .../src/mediawiki.api/mediawiki.api.upload.js | 60 ++++++++++++++++++- 1 file changed, 58 insertions(+), 2 deletions(-) diff --git a/resources/src/mediawiki.api/mediawiki.api.upload.js b/resources/src/mediawiki.api/mediawiki.api.upload.js index b03e52c9bd..f147ac85f6 100644 --- a/resources/src/mediawiki.api/mediawiki.api.upload.js +++ b/resources/src/mediawiki.api/mediawiki.api.upload.js @@ -6,6 +6,8 @@ ( function ( mw, $ ) { var nonce = 0, fieldsAllowed = { + stash: true, + filekey: true, filename: true, comment: true, text: true, @@ -218,7 +220,7 @@ } } ); - if ( !filenameFound ) { + if ( !filenameFound && !data.stash ) { return $.Deferred().reject( 'Filename not included in file data.' ); } @@ -256,7 +258,7 @@ } } ); - if ( !filenameFound ) { + if ( !filenameFound && !data.stash ) { return $.Deferred().reject( 'Filename not included in file data.' ); } @@ -293,6 +295,60 @@ } ); return deferred.promise(); + }, + + /** + * Upload a file to the stash. + * + * This function will return a promise, which when resolved, will pass back a function + * to finish the stash upload. You can call that function with an argument containing + * more, or conflicting, data to pass to the server. For example: + * // upload a file to the stash with a placeholder filename + * api.uploadToStash( file, { filename: 'testing.png' } ).done( function ( finish ) { + * // finish is now the function we can use to finalize the upload + * // pass it a new filename from user input to override the initial value + * finish( { filename: getFilenameFromUser() } ).done( function ( data ) { + * // the upload is complete, data holds the API response + * } ); + * } ); + * @param {File|HTMLInputElement} file + * @param {Object} [data] + * @return {jQuery.Promise} + * @return {Function} return.finishStashUpload Call this function to finish the upload. + * @return {Object} return.finishStashUpload.data Additional data for the upload. + * @return {jQuery.Promise} return.finishStashUpload.return API promise for the final upload + * @return {Object} return.finishStashUpload.return.data API return value for the final upload + */ + uploadToStash: function ( file, data ) { + var filekey, + api = this; + + if ( !data.filename ) { + return $.Deferred().reject( 'Filename not included in file data.' ); + } + + function finishUpload( moreData ) { + data = $.extend( data, moreData ); + data.filekey = filekey; + data.action = 'upload'; + data.format = 'json'; + + if ( !data.filename ) { + return $.Deferred().reject( 'Filename not included in file data.' ); + } + + return api.postWithEditToken( data ); + } + + return this.upload( file, { stash: true, filename: data.filename } ).then( function ( result ) { + if ( result && result.upload && result.upload.filekey ) { + filekey = result.upload.filekey; + } else if ( result && ( result.error || result.warning ) ) { + return $.Deferred().reject( result ); + } + + return finishUpload; + } ); } } ); -- 2.20.1