summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/UploadWizard/resources/controller/uw.controller.Upload.js
blob: aae6c791a6e8b3b511317c1c25ce3ab838713677 (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
/*
 * This file is part of the MediaWiki extension UploadWizard.
 *
 * UploadWizard is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * UploadWizard is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with UploadWizard.  If not, see <http://www.gnu.org/licenses/>.
 */

( function ( mw, uw, $, OO ) {

	/**
	 * Upload step controller.
	 *
	 * @class
	 * @extends uw.controller.Step
	 * @param {mw.Api} api
	 * @param {Object} config UploadWizard config object.
	 */
	uw.controller.Upload = function UWControllerUpload( api, config ) {
		var step = this;

		uw.controller.Step.call(
			this,
			new uw.ui.Upload( config )
				.connect( this, {
					retry: 'retry'
				} ),
			api,
			config
		);

		this.stepName = 'file';
		this.finishState = 'stashed';

		this.queue = new uw.ConcurrentQueue( {
			count: this.config.maxSimultaneousConnections,
			action: this.transitionOne.bind( this )
		} );
		this.queue.on( 'complete', this.showNext.bind( this ) );

		this.ui.on( 'files-added', function ( files ) {
			var totalFiles = files.length + step.uploads.length,
				tooManyFiles = totalFiles > step.config.maxUploads;

			if ( tooManyFiles ) {
				step.ui.showTooManyFilesError( totalFiles );
			} else {
				step.addFiles( files );
			}
		} );
	};

	OO.inheritClass( uw.controller.Upload, uw.controller.Step );

	/**
	 * Updates the upload step data when a file is added or removed.
	 */
	uw.controller.Upload.prototype.updateFileCounts = function () {
		var fewerThanMax, haveUploads,
			max = this.config.maxUploads;

		haveUploads = this.uploads.length > 0;
		fewerThanMax = this.uploads.length < max;

		this.updateProgressBarCount( this.uploads.length );
		this.ui.updateFileCounts( haveUploads, fewerThanMax );
	};

	uw.controller.Upload.prototype.load = function ( uploads ) {
		var controller = this;

		uw.controller.Step.prototype.load.call( this, uploads );
		this.updateFileCounts();
		this.startProgressBar();

		// make sure queue is empty before starting this step
		this.queue.abortExecuting();

		if ( uploads.length > 0 ) {
			/*
			 * If we have uploads already, we'll want to to update the "next"
			 * buttons accordingly. showNext() does that, but relies on upload
			 * state being set correctly.
			 * Since every step overwrites the upload state, we'll need to reset
			 * it to reflect the correct upload success state.
			 * If other files are to be added, the showNext() callback will deal
			 * with new uploads, and still understand the existing files that
			 * we've just reset the state for.
			 */
			$.each( uploads, function ( i, upload ) {
				upload.state = upload.fileKey === undefined ? 'error' : controller.finishState;
			} );

			this.showNext();
		}
	};

	uw.controller.Upload.prototype.moveNext = function () {
		this.removeErrorUploads();

		uw.controller.Step.prototype.moveNext.call( this );
	};

	/**
	 * Starts the upload progress bar.
	 */
	uw.controller.Upload.prototype.startProgressBar = function () {
		this.ui.showProgressBar();
		this.progressBar = new mw.GroupProgressBar( this.ui.$progress,
			this.uploads,
			[ 'stashed' ],
			[ 'error' ],
			'transportProgress',
			'transportWeight' );
		this.progressBar.start();
	};

	/**
	 * Starts progress bar if there's not an existing one.
	 */
	uw.controller.Upload.prototype.maybeStartProgressBar = function () {
		if ( this.progressBarEmptyOrFinished() ) {
			this.startProgressBar();
		}
	};

	/**
	 * Check if there is a vacancy for a new progress bar.
	 *
	 * @return {boolean}
	 */
	uw.controller.Upload.prototype.progressBarEmptyOrFinished = function () {
		return !this.progressBar || this.progressBar.finished === true;
	};

	/**
	 * Update success count on the progress bar.
	 *
	 * @param {number} okCount
	 */
	uw.controller.Upload.prototype.updateProgressBarCount = function ( okCount ) {
		if ( this.progressBar ) {
			this.progressBar.showCount( okCount );
		}
	};

	uw.controller.Upload.prototype.canTransition = function ( upload ) {
		return (
			uw.controller.Step.prototype.canTransition.call( this, upload ) &&
			upload.state === 'new'
		);
	};

	/**
	 * Perform this step's changes on one upload.
	 *
	 * @param {mw.UploadWizardUpload} upload
	 * @return {jQuery.Promise}
	 */
	uw.controller.Upload.prototype.transitionOne = function ( upload ) {
		var promise = upload.start();
		this.maybeStartProgressBar();
		return promise;
	};

	/**
	 * Queue an upload object to be uploaded.
	 *
	 * @param {mw.UploadWizardUpload} upload
	 */
	uw.controller.Upload.prototype.queueUpload = function ( upload ) {
		if ( this.canTransition( upload ) ) {
			this.queue.addItem( upload );
		}
	};

	/**
	 * Kick off the upload processes.
	 */
	uw.controller.Upload.prototype.startQueuedUploads = function () {
		this.queue.startExecuting();
	};

	uw.controller.Upload.prototype.retry = function () {
		var controller = this;
		uw.eventFlowLogger.logEvent( 'retry-uploads-button-clicked' );

		$.each( this.uploads, function ( i, upload ) {
			if ( upload.state === 'error' ) {
				// reset any uploads in error state back to be shiny & new
				upload.state = 'new';
				upload.ui.clearIndicator();
				upload.ui.clearStatus();
				// and queue them
				controller.queueUpload( upload );
			}
		} );

		this.startQueuedUploads();
	};

	/**
	 * Create the upload interface, a handler to transport it to the server, and UI for the upload
	 * itself; and immediately fill it with a file and add it to the list of uploads.
	 *
	 * @param {File} file
	 * @return {UploadWizardUpload|false} The new upload, or false if it can't be added
	 */
	uw.controller.Upload.prototype.addFile = function ( file ) {
		var upload;

		if ( this.uploads.length >= this.config.maxUploads ) {
			return false;
		}

		upload = new mw.UploadWizardUpload( this, file );

		if ( !this.validateFile( upload ) ) {
			return false;
		}

		upload.fileChangedOk();

		// attach controller-specific event handlers (they're automatically
		// bound on load already, but we've only just added these files...)
		this.bindUploadHandlers( upload );

		this.setUploadFilled( upload );

		return upload;
	};

	/**
	 * Do everything that needs to be done to start uploading a file. Calls #addFile, then appends
	 * each mw.UploadWizardUploadInterface to the DOM and queues thumbnails to be generated.
	 *
	 * @param {File[]} files
	 */
	uw.controller.Upload.prototype.addFiles = function ( files ) {
		var
			uploadObj,
			uploadObjs = [],
			controller = this;

		$.each( files, function ( i, file ) {
			uploadObj = controller.addFile( file );
			if ( uploadObj ) {
				uploadObjs.push( uploadObj );
			}
		} );

		this.ui.displayUploads( uploadObjs );

		uw.eventFlowLogger.logUploadEvent( 'uploads-added', { quantity: files.length } );
	};

	/**
	 * Remove an upload from our array of uploads, and the HTML UI
	 * We can remove the HTML UI directly, as jquery will just get the parent.
	 * We need to grep through the array of uploads, since we don't know the current index.
	 * We need to update file counts for obvious reasons.
	 *
	 * @param {mw.UploadWizardUpload} upload
	 */
	uw.controller.Upload.prototype.removeUpload = function ( upload ) {
		uw.controller.Step.prototype.removeUpload.call( this, upload );

		this.queue.removeItem( upload );

		this.updateFileCounts();

		// check all uploads, if they're complete, show the next button
		this.showNext();
	};

	/**
	 * When an upload is filled with a real file, accept it in the list of uploads
	 * and set up some other interfaces
	 *
	 * @param {mw.UploadWizardUpload} upload
	 */
	uw.controller.Upload.prototype.setUploadFilled = function ( upload ) {
		this.addUpload( upload );
		this.updateFileCounts();
		// Start uploads now, no reason to wait--leave the remove button alone
		this.queueUpload( upload );
		this.startQueuedUploads();
	};

	/**
	 * Checks for file validity.
	 *
	 * @param {mw.UploadWizardUpload} upload
	 * @return {boolean} Error in [code, info] format, or empty [] for no errors
	 */
	uw.controller.Upload.prototype.validateFile = function ( upload ) {
		var extension,
			i,
			actualMaxSize = mw.UploadWizard.config.maxMwUploadSize,

			// Check if filename is acceptable
			// TODO sanitize filename
			filename = upload.getFilename(),
			basename = upload.getBasename();

		// check to see if this file has already been selected for upload
		for ( i = 0; i < this.uploads.length; i++ ) {
			if ( upload !== this.uploads[ i ] && filename === this.uploads[ i ].getFilename() ) {
				this.ui.showDuplicateError( filename, basename );
				return false;
			}
		}

		// check if the filename is valid
		upload.setTitle( basename );
		if ( !upload.title ) {
			if ( basename.indexOf( '.' ) === -1 ) {
				this.ui.showMissingExtensionError( filename );
				return false;
			} else {
				this.ui.showUnparseableFilenameError( filename );
				return false;
			}
		}

		// check if extension is acceptable
		extension = upload.title.getExtension();
		if ( !extension ) {
			this.ui.showMissingExtensionError( filename );
			return false;
		}

		if (
			mw.UploadWizard.config.fileExtensions !== null &&
			$.inArray( extension.toLowerCase(), mw.UploadWizard.config.fileExtensions ) === -1
		) {
			this.ui.showBadExtensionError( filename, extension );
			return false;
		}

		// make sure the file isn't too large
		// TODO need a way to find the size of the Flickr image
		if ( upload.file.size ) {
			upload.transportWeight = upload.file.size;
			if ( upload.transportWeight > actualMaxSize ) {
				this.ui.showFileTooLargeError( actualMaxSize, upload.transportWeight );
				return false;
			}
		}

		return true;
	};

}( mediaWiki, mediaWiki.uploadWizard, jQuery, OO ) );