summaryrefslogtreecommitdiff
path: root/resources/src/mediawiki/mediawiki.Upload.js
blob: 4f8789de7aae7283f91922f0001d3b297c19ab35 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
( function ( mw, $ ) {
	var UP;

	/**
	 * @class mw.Upload
	 *
	 * Used to represent an upload in progress on the frontend.
	 * Most of the functionality is implemented in mw.Api.plugin.upload,
	 * but this model class will tie it together as well as let you perform
	 * actions in a logical way.
	 *
	 * A simple example:
	 *
	 *     var file = new OO.ui.SelectFileWidget(),
	 *       button = new OO.ui.ButtonWidget( { label: 'Save' } ),
	 *       upload = new mw.Upload;
	 *
	 *     button.on( 'click', function () {
	 *       upload.setFile( file.getValue() );
	 *       upload.setFilename( file.getValue().name );
	 *       upload.upload();
	 *     } );
	 *
	 *     $( 'body' ).append( file.$element, button.$element );
	 *
	 * You can also choose to {@link #uploadToStash stash the upload} and
	 * {@link #finishStashUpload finalize} it later:
	 *
	 *     var file, // Some file object
	 *       upload = new mw.Upload,
	 *       stashPromise = $.Deferred();
	 *
	 *     upload.setFile( file );
	 *     upload.uploadToStash().then( function () {
	 *       stashPromise.resolve();
	 *     } );
	 *
	 *     stashPromise.then( function () {
	 *       upload.setFilename( 'foo' );
	 *       upload.setText( 'bar' );
	 *       upload.finishStashUpload().then( function () {
	 *         console.log( 'Done!' );
	 *       } );
	 *     } );
	 *
	 * @constructor
	 * @param {Object|mw.Api} [apiconfig] A mw.Api object (or subclass), or configuration
	 *     to pass to the constructor of mw.Api.
	 */
	function Upload( apiconfig ) {
		this.api = ( apiconfig instanceof mw.Api ) ? apiconfig : new mw.Api( apiconfig );

		this.watchlist = false;
		this.text = '';
		this.comment = '';
		this.filename = null;
		this.file = null;
		this.setState( Upload.State.NEW );

		this.imageinfo = undefined;
	}

	UP = Upload.prototype;

	/**
	 * Set the text of the file page, to be created on file upload.
	 *
	 * @param {string} text
	 */
	UP.setText = function ( text ) {
		this.text = text;
	};

	/**
	 * Set the filename, to be finalized on upload.
	 *
	 * @param {string} filename
	 */
	UP.setFilename = function ( filename ) {
		this.filename = filename;
	};

	/**
	 * Sets the filename based on the filename as it was on the upload.
	 */
	UP.setFilenameFromFile = function () {
		var file = this.getFile();
		if ( !file ) {
			return;
		}
		if ( file.nodeType && file.nodeType === Node.ELEMENT_NODE ) {
			// File input element, use getBasename to cut out the path
			this.setFilename( this.getBasename( file.value ) );
		} else if ( file.name ) {
			// HTML5 FileAPI File object, but use getBasename to be safe
			this.setFilename( this.getBasename( file.name ) );
		} else {
			// If we ever implement uploading files from clipboard, they might not have a name
			this.setFilename( '?' );
		}
	};

	/**
	 * Set the file to be uploaded.
	 *
	 * @param {HTMLInputElement|File} file
	 */
	UP.setFile = function ( file ) {
		this.file = file;
	};

	/**
	 * Set whether the file should be watchlisted after upload.
	 *
	 * @param {boolean} watchlist
	 */
	UP.setWatchlist = function ( watchlist ) {
		this.watchlist = watchlist;
	};

	/**
	 * Set the edit comment for the upload.
	 *
	 * @param {string} comment
	 */
	UP.setComment = function ( comment ) {
		this.comment = comment;
	};

	/**
	 * Get the text of the file page, to be created on file upload.
	 *
	 * @return {string}
	 */
	UP.getText = function () {
		return this.text;
	};

	/**
	 * Get the filename, to be finalized on upload.
	 *
	 * @return {string}
	 */
	UP.getFilename = function () {
		return this.filename;
	};

	/**
	 * Get the file being uploaded.
	 *
	 * @return {HTMLInputElement|File}
	 */
	UP.getFile = function () {
		return this.file;
	};

	/**
	 * Get the boolean for whether the file will be watchlisted after upload.
	 *
	 * @return {boolean}
	 */
	UP.getWatchlist = function () {
		return this.watchlist;
	};

	/**
	 * Get the current value of the edit comment for the upload.
	 *
	 * @return {string}
	 */
	UP.getComment = function () {
		return this.comment;
	};

	/**
	 * Gets the base filename from a path name.
	 *
	 * @param {string} path
	 * @return {string}
	 */
	UP.getBasename = function ( path ) {
		if ( path === undefined || path === null ) {
			return '';
		}

		// Find the index of the last path separator in the
		// path, and add 1. Then, take the entire string after that.
		return path.slice(
			Math.max(
				path.lastIndexOf( '/' ),
				path.lastIndexOf( '\\' )
			) + 1
		);
	};

	/**
	 * Sets the state and state details (if any) of the upload.
	 *
	 * @param {mw.Upload.State} state
	 * @param {Object} stateDetails
	 */
	UP.setState = function ( state, stateDetails ) {
		this.state = state;
		this.stateDetails = stateDetails;
	};

	/**
	 * Gets the state of the upload.
	 *
	 * @return {mw.Upload.State}
	 */
	UP.getState = function () {
		return this.state;
	};

	/**
	 * Gets details of the current state.
	 *
	 * @return {string}
	 */
	UP.getStateDetails = function () {
		return this.stateDetails;
	};

	/**
	 * Get the imageinfo object for the finished upload.
	 * Only available once the upload is finished! Don't try to get it
	 * beforehand.
	 *
	 * @return {Object|undefined}
	 */
	UP.getImageInfo = function () {
		return this.imageinfo;
	};

	/**
	 * Upload the file directly.
	 *
	 * @return {jQuery.Promise}
	 */
	UP.upload = function () {
		var upload = this;

		if ( !this.getFile() ) {
			return $.Deferred().reject( 'No file to upload. Call setFile to add one.' );
		}

		if ( !this.getFilename() ) {
			return $.Deferred().reject( 'No filename set. Call setFilename to add one.' );
		}

		this.setState( Upload.State.UPLOADING );

		return this.api.upload( this.getFile(), {
			watchlist: ( this.getWatchlist() ) ? 1 : undefined,
			comment: this.getComment(),
			filename: this.getFilename(),
			text: this.getText()
		} ).then( function ( result ) {
			upload.setState( Upload.State.UPLOADED );
			upload.imageinfo = result.upload.imageinfo;
			return result;
		}, function ( errorCode, result ) {
			if ( result && result.upload && result.upload.warnings ) {
				upload.setState( Upload.State.WARNING, result );
			} else {
				upload.setState( Upload.State.ERROR, result );
			}
			return $.Deferred().reject( errorCode, result );
		} );
	};

	/**
	 * Upload the file to the stash to be completed later.
	 *
	 * @return {jQuery.Promise}
	 */
	UP.uploadToStash = function () {
		var upload = this;

		if ( !this.getFile() ) {
			return $.Deferred().reject( 'No file to upload. Call setFile to add one.' );
		}

		if ( !this.getFilename() ) {
			this.setFilenameFromFile();
		}

		this.setState( Upload.State.UPLOADING );

		this.stashPromise = this.api.uploadToStash( this.getFile(), {
			filename: this.getFilename()
		} ).then( function ( finishStash ) {
			upload.setState( Upload.State.STASHED );
			return finishStash;
		}, function ( errorCode, result ) {
			if ( result && result.upload && result.upload.warnings ) {
				upload.setState( Upload.State.WARNING, result );
			} else {
				upload.setState( Upload.State.ERROR, result );
			}
			return $.Deferred().reject( errorCode, result );
		} );

		return this.stashPromise;
	};

	/**
	 * Finish a stash upload.
	 *
	 * @return {jQuery.Promise}
	 */
	UP.finishStashUpload = function () {
		var upload = this;

		if ( !this.stashPromise ) {
			return $.Deferred().reject( 'This upload has not been stashed, please upload it to the stash first.' );
		}

		return this.stashPromise.then( function ( finishStash ) {
			upload.setState( Upload.State.UPLOADING );

			return finishStash( {
				watchlist: ( upload.getWatchlist() ) ? 1 : undefined,
				comment: upload.getComment(),
				filename: upload.getFilename(),
				text: upload.getText()
			} ).then( function ( result ) {
				upload.setState( Upload.State.UPLOADED );
				upload.imageinfo = result.upload.imageinfo;
				return result;
			}, function ( errorCode, result ) {
				if ( result && result.upload && result.upload.warnings ) {
					upload.setState( Upload.State.WARNING, result );
				} else {
					upload.setState( Upload.State.ERROR, result );
				}
				return $.Deferred().reject( errorCode, result );
			} );
		} );
	};

	/**
	 * @enum mw.Upload.State
	 * State of uploads represented in simple terms.
	 */
	Upload.State = {
		/** Upload not yet started */
		NEW: 0,

		/** Upload finished, but there was a warning */
		WARNING: 1,

		/** Upload finished, but there was an error */
		ERROR: 2,

		/** Upload in progress */
		UPLOADING: 3,

		/** Upload finished, but not published, call #finishStashUpload */
		STASHED: 4,

		/** Upload finished and published */
		UPLOADED: 5
	};

	mw.Upload = Upload;
}( mediaWiki, jQuery ) );