Debian Codegraph @

resources/src/mediawiki.Upload.js

raw symbols SCIP
1 ( function () {
2 /**
3 * @classdesc Upload to a wiki. Most of the functionality is implemented
4 * in {@link mw.Api#upload} and friends, but this model class will tie it
5 * together as well as let you perform actions in a logical way.
6 *
7 * A simple example:
8 * ```
9 * var file = new OO.ui.SelectFileWidget(),
10 * button = new OO.ui.ButtonWidget( { label: 'Save' } ),
11 * upload = new mw.Upload;
12 *
13 * button.on( 'click', function () {
14 * upload.setFile( file.getValue() );
15 * upload.setFilename( file.getValue().name );
16 * upload.upload();
17 * } );
18 *
19 * $( document.body ).append( file.$element, button.$element );
20 * ```
21 * You can also choose to {@link mw.Upload#uploadToStash stash the upload}
22 * and {@link mw.Upload#finishStashUpload finalize} it later:
23 * ```
24 * var file, // Some file object
25 * upload = new mw.Upload,
26 * stashPromise = $.Deferred();
27 *
28 * upload.setFile( file );
29 * upload.uploadToStash().then( function () {
30 * stashPromise.resolve();
31 * } );
32 *
33 * stashPromise.then( function () {
34 * upload.setFilename( 'foo' );
35 * upload.setText( 'bar' );
36 * upload.finishStashUpload().then( function () {
37 * console.log( 'Done!' );
38 * } );
39 * } );
40 * ```
41 * @class mw.Upload
42 *
43 * @constructor
44 * @description Used to represent an upload in progress on the frontend.
45 * @param {Object|mw.Api} [apiconfig] A mw.Api object (or subclass), or configuration
46 * to pass to the constructor of mw.Api.
47 */
48 function Upload( apiconfig ) {
49 this.api = ( apiconfig instanceof mw.Api ) ? apiconfig : new mw.Api( apiconfig );
50
51 this.watchlist = false;
52 this.text = '';
53 this.comment = '';
54 this.filename = null;
55 this.file = null;
56 this.setState( Upload.State.NEW );
57
58 this.imageinfo = undefined;
59 }
60
61 const UP = Upload.prototype;
62
63 /**
64 * Get the mw.Api instance used by this Upload object.
65 *
66 * @name mw.Upload.prototype.getApi
67 * @method
68 * @return {jQuery.Promise<mw.Api>}
69 */
70 UP.getApi = function () {
71 return $.Deferred().resolve( this.api ).promise();
72 };
73
74 /**
75 * Set the text of the file page, to be created on file upload.
76 *
77 * @name mw.Upload.prototype.setText
78 * @method
79 * @param {string} text
80 */
81 UP.setText = function ( text ) {
82 this.text = text;
83 };
84
85 /**
86 * Set the filename, to be finalized on upload.
87 *
88 * @name mw.Upload.prototype.setFilename
89 * @method
90 * @param {string} filename
91 */
92 UP.setFilename = function ( filename ) {
93 this.filename = filename;
94 };
95
96 /**
97 * Set the stashed file to finish uploading.
98 *
99 * @name mw.Upload.prototype.setFilekey
100 * @method
101 * @param {string} filekey
102 */
103 UP.setFilekey = function ( filekey ) {
104 const upload = this;
105
106 this.setState( Upload.State.STASHED );
107 this.stashPromise = $.Deferred().resolve( ( data ) => upload.api.uploadFromStash( filekey, data ) );
108 };
109
110 /**
111 * Sets the filename based on the filename as it was on the upload.
112 *
113 * @name mw.Upload.prototype.setFilenameFromFile
114 * @method
115 */
116 UP.setFilenameFromFile = function () {
117 const file = this.getFile();
118 if ( !file ) {
119 return;
120 }
121 if ( file.nodeType && file.nodeType === Node.ELEMENT_NODE ) {
122 // File input element, use getBasename to cut out the path
123 this.setFilename( this.getBasename( file.value ) );
124 } else if ( file.name ) {
125 // HTML5 FileAPI File object, but use getBasename to be safe
126 this.setFilename( this.getBasename( file.name ) );
127 } else {
128 // If we ever implement uploading files from clipboard, they might not have a name
129 this.setFilename( '?' );
130 }
131 };
132
133 /**
134 * Set the file to be uploaded.
135 *
136 * @name mw.Upload.prototype.setFile
137 * @method
138 * @param {HTMLInputElement|File|Blob} file
139 */
140 UP.setFile = function ( file ) {
141 this.file = file;
142 };
143
144 /**
145 * Set whether the file should be watchlisted after upload.
146 *
147 * @name mw.Upload.prototype.setWatchlist
148 * @method
149 * @param {boolean} watchlist
150 */
151 UP.setWatchlist = function ( watchlist ) {
152 this.watchlist = watchlist;
153 };
154
155 /**
156 * Set the edit comment for the upload.
157 *
158 * @name mw.Upload.prototype.setComment
159 * @method
160 * @param {string} comment
161 */
162 UP.setComment = function ( comment ) {
163 this.comment = comment;
164 };
165
166 /**
167 * Get the text of the file page, to be created on file upload.
168 *
169 * @name mw.Upload.prototype.getText
170 * @method
171 * @return {string}
172 */
173 UP.getText = function () {
174 return this.text;
175 };
176
177 /**
178 * Get the filename, to be finalized on upload.
179 *
180 * @name mw.Upload.prototype.getFilename
181 * @method
182 * @return {string}
183 */
184 UP.getFilename = function () {
185 return this.filename;
186 };
187
188 /**
189 * Get the file being uploaded.
190 *
191 * @name mw.Upload.prototype.getFile
192 * @method
193 * @return {HTMLInputElement|File|Blob}
194 */
195 UP.getFile = function () {
196 return this.file;
197 };
198
199 /**
200 * Get the boolean for whether the file will be watchlisted after upload.
201 *
202 * @name mw.Upload.prototype.getWatchlist
203 * @method
204 * @return {boolean}
205 */
206 UP.getWatchlist = function () {
207 return this.watchlist;
208 };
209
210 /**
211 * Get the current value of the edit comment for the upload.
212 *
213 * @name mw.Upload.prototype.getComment
214 * @method
215 * @return {string}
216 */
217 UP.getComment = function () {
218 return this.comment;
219 };
220
221 /**
222 * Gets the base filename from a path name.
223 *
224 * @name mw.Upload.prototype.getBasename
225 * @method
226 * @param {string} path
227 * @return {string}
228 */
229 UP.getBasename = function ( path ) {
230 if ( path === undefined || path === null ) {
231 return '';
232 }
233
234 // Find the index of the last path separator in the
235 // path, and add 1. Then, take the entire string after that.
236 return path.slice(
237 Math.max(
238 path.lastIndexOf( '/' ),
239 path.lastIndexOf( '\\' )
240 ) + 1
241 );
242 };
243
244 /**
245 * Sets the state and state details (if any) of the upload.
246 *
247 * @name mw.Upload.prototype.setState
248 * @method
249 * @param {mw.Upload.State} state
250 * @param {Object} stateDetails
251 */
252 UP.setState = function ( state, stateDetails ) {
253 this.state = state;
254 this.stateDetails = stateDetails;
255 };
256
257 /**
258 * Gets the state of the upload.
259 *
260 * @name mw.Upload.prototype.getState
261 * @method
262 * @return {mw.Upload.State}
263 */
264 UP.getState = function () {
265 return this.state;
266 };
267
268 /**
269 * Gets details of the current state.
270 *
271 * @name mw.Upload.prototype.getStateDetails
272 * @method
273 * @return {string}
274 */
275 UP.getStateDetails = function () {
276 return this.stateDetails;
277 };
278
279 /**
280 * Get the imageinfo object for the finished upload.
281 * Only available once the upload is finished! Don't try to get it
282 * beforehand.
283 *
284 * @name mw.Upload.prototype.getImageInfo
285 * @method
286 * @return {Object|undefined}
287 */
288 UP.getImageInfo = function () {
289 return this.imageinfo;
290 };
291
292 /**
293 * Upload the file directly.
294 *
295 * @name mw.Upload.prototype.upload
296 * @method
297 * @return {jQuery.Promise}
298 */
299 UP.upload = function () {
300 const upload = this;
301
302 if ( !this.getFile() ) {
303 return $.Deferred().reject( 'No file to upload. Call setFile to add one.' );
304 }
305
306 if ( !this.getFilename() ) {
307 return $.Deferred().reject( 'No filename set. Call setFilename to add one.' );
308 }
309
310 this.setState( Upload.State.UPLOADING );
311
312 return this.api.chunkedUpload( this.getFile(), {
313 watchlist: ( this.getWatchlist() ) ? 1 : undefined,
314 comment: this.getComment(),
315 filename: this.getFilename(),
316 text: this.getText()
317 } ).then( ( result ) => {
318 upload.setState( Upload.State.UPLOADED );
319 upload.imageinfo = result.upload.imageinfo;
320 return result;
321 }, ( errorCode, result ) => {
322 if ( result && result.upload && result.upload.warnings ) {
323 upload.setState( Upload.State.WARNING, result );
324 } else {
325 upload.setState( Upload.State.ERROR, result );
326 }
327 return $.Deferred().reject( errorCode, result );
328 } );
329 };
330
331 /**
332 * Upload the file to the stash to be completed later.
333 *
334 * @name mw.Upload.prototype.uploadToStash
335 * @method
336 * @return {jQuery.Promise}
337 */
338 UP.uploadToStash = function () {
339 const upload = this;
340
341 if ( !this.getFile() ) {
342 return $.Deferred().reject( 'No file to upload. Call setFile to add one.' );
343 }
344
345 if ( !this.getFilename() ) {
346 this.setFilenameFromFile();
347 }
348
349 this.setState( Upload.State.UPLOADING );
350
351 this.stashPromise = this.api.chunkedUploadToStash( this.getFile(), {
352 ignorewarnings: true,
353 filename: this.getFilename()
354 } ).then( ( finishStash ) => {
355 upload.setState( Upload.State.STASHED );
356 return finishStash;
357 }, ( errorCode, result ) => {
358 if ( result && result.upload && result.upload.warnings ) {
359 upload.setState( Upload.State.WARNING, result );
360 } else {
361 upload.setState( Upload.State.ERROR, result );
362 }
363 return $.Deferred().reject( errorCode, result );
364 } );
365
366 return this.stashPromise;
367 };
368
369 /**
370 * Finish a stash upload.
371 *
372 * @name mw.Upload.prototype.finishStashUpload
373 * @method
374 * @return {jQuery.Promise}
375 */
376 UP.finishStashUpload = function () {
377 const upload = this;
378
379 if ( !this.stashPromise ) {
380 return $.Deferred().reject( 'This upload has not been stashed, please upload it to the stash first.' );
381 }
382
383 return this.stashPromise.then( ( finishStash ) => {
384 upload.setState( Upload.State.UPLOADING );
385
386 return finishStash( {
387 ignorewarnings: false,
388 watchlist: ( upload.getWatchlist() ) ? 1 : undefined,
389 comment: upload.getComment(),
390 filename: upload.getFilename(),
391 text: upload.getText()
392 } ).then( ( result ) => {
393 upload.setState( Upload.State.UPLOADED );
394 upload.imageinfo = result.upload.imageinfo;
395 return result;
396 }, ( errorCode, result ) => {
397 if ( result && result.upload && result.upload.warnings ) {
398 upload.setState( Upload.State.WARNING, result );
399 } else {
400 upload.setState( Upload.State.ERROR, result );
401 }
402 return $.Deferred().reject( errorCode, result );
403 } );
404 } );
405 };
406
407 mw.Upload = Upload;
408
409 /**
410 * @enum
411 *
412 * State of uploads represented in simple terms.
413 */
414 mw.Upload.State = {
415 /** Upload not yet started */
416 NEW: 0,
417
418 /** Upload finished, but there was a warning */
419 WARNING: 1,
420
421 /** Upload finished, but there was an error */
422 ERROR: 2,
423
424 /** Upload in progress */
425 UPLOADING: 3,
426
427 /** Upload finished, but not published, call #finishStashUpload */
428 STASHED: 4,
429
430 /** Upload finished and published */
431 UPLOADED: 5
432 };
433 }() );