2 * JavaScript for Special:Upload
3 * Note that additional code still lives in skins/common/upload.js
7 * Add a preview to the upload form
9 $( document ).ready( function () {
11 * Is the FileAPI available with sufficient functionality?
13 function hasFileAPI() {
14 return window.FileReader !== undefined;
18 * Check if this is a recognizable image type...
19 * Also excludes files over 10M to avoid going insane on memory usage.
21 * @todo is there a way we can ask the browser what's supported in <img>s?
22 * @todo put SVG back after working around Firefox 7 bug <https://bugzilla.wikimedia.org/show_bug.cgi?id=31643>
27 function fileIsPreviewable( file ) {
28 var known = ['image/png', 'image/gif', 'image/jpeg', 'image/svg+xml'],
29 tooHuge = 10 * 1024 * 1024;
30 return ( $.inArray( file.type, known ) !== -1 ) && file.size > 0 && file.size < tooHuge;
34 * Show a thumbnail preview of PNG, JPEG, GIF, and SVG files prior to upload
35 * in browsers supporting HTML5 FileAPI.
37 * As of this writing, known good:
39 * - Chrome 7.something
41 * @todo check file size limits and warn of likely failures
45 function showPreview( file ) {
50 thumb = $( '<div id="mw-upload-thumbnail" class="thumb tright">' +
51 '<div class="thumbinner">' +
52 '<div class="mw-small-spinner" style="width: 180px; height: 180px"></div>' +
53 '<div class="thumbcaption"><div class="filename"></div><div class="fileinfo"></div></div>' +
57 thumb.find( '.filename' ).text( file.name ).end()
58 .find( '.fileinfo' ).text( prettySize( file.size ) ).end();
60 $canvas = $('<canvas width="' + previewSize + '" height="' + previewSize + '" ></canvas>');
61 ctx = $canvas[0].getContext( '2d' );
62 $( '#mw-htmlform-source' ).parent().prepend( thumb );
64 fetchPreview( file, function ( dataURL ) {
65 var img = new Image(),
68 if ( meta && meta.tiff && meta.tiff.Orientation ) {
69 rotation = ( 360 - ( function () {
70 // See includes/media/Bitmap.php
71 switch ( meta.tiff.Orientation.value ) {
84 img.onload = function () {
85 var info, width, height, x, y, dx, dy, logicalWidth, logicalHeight;
87 // Fit the image within the previewSizexpreviewSize box
88 if ( img.width > img.height ) {
90 height = img.height / img.width * previewSize;
93 width = img.width / img.height * previewSize;
95 // Determine the offset required to center the image
96 dx = (180 - width) / 2;
97 dy = (180 - height) / 2;
99 // If a rotation is applied, the direction of the axis
100 // changes as well. You can derive the values below by
101 // drawing on paper an axis system, rotate it and see
102 // where the positive axis direction is
106 logicalWidth = img.width;
107 logicalHeight = img.height;
112 y = dy - previewSize;
113 logicalWidth = img.height;
114 logicalHeight = img.width;
117 x = dx - previewSize;
118 y = dy - previewSize;
119 logicalWidth = img.width;
120 logicalHeight = img.height;
123 x = dx - previewSize;
125 logicalWidth = img.height;
126 logicalHeight = img.width;
130 ctx.clearRect( 0, 0, 180, 180 );
131 ctx.rotate( rotation / 180 * Math.PI );
132 ctx.drawImage( img, x, y, width, height );
133 thumb.find('.mw-small-spinner').replaceWith($canvas);
136 info = mw.msg( 'widthheight', logicalWidth, logicalHeight ) +
137 ', ' + prettySize( file.size );
139 $( '#mw-upload-thumbnail .fileinfo' ).text( info );
142 }, mw.config.get( 'wgFileCanRotate' ) ? function ( data ) {
143 /*jshint camelcase: false, nomen: false */
145 meta = mw.libs.jpegmeta( data, file.fileName );
146 meta._binary_data = null;
154 * Start loading a file into memory; when complete, pass it as a
155 * data URL to the callback function. If the callbackBinary is set it will
156 * first be read as binary and afterwards as data URL. Useful if you want
157 * to do preprocessing on the binary data first.
160 * @param {function} callback
161 * @param {function} callbackBinary
163 function fetchPreview( file, callback, callbackBinary ) {
164 var reader = new FileReader();
165 if ( callbackBinary && 'readAsBinaryString' in reader ) {
166 // To fetch JPEG metadata we need a binary string; start there.
168 reader.onload = function() {
169 callbackBinary( reader.result );
171 // Now run back through the regular code path.
172 fetchPreview( file, callback );
174 reader.readAsBinaryString( file );
175 } else if ( callbackBinary && 'readAsArrayBuffer' in reader ) {
176 // readAsArrayBuffer replaces readAsBinaryString
177 // However, our JPEG metadata library wants a string.
178 // So, this is going to be an ugly conversion.
179 reader.onload = function() {
181 buffer = new Uint8Array( reader.result ),
183 for ( i = 0; i < buffer.byteLength; i++ ) {
184 string += String.fromCharCode( buffer[i] );
186 callbackBinary( string );
188 // Now run back through the regular code path.
189 fetchPreview( file, callback );
191 reader.readAsArrayBuffer( file );
192 } else if ( 'URL' in window && 'createObjectURL' in window.URL ) {
193 // Supported in Firefox 4.0 and above <https://developer.mozilla.org/en/DOM/window.URL.createObjectURL>
194 // WebKit has it in a namespace for now but that's ok. ;)
196 // Lifetime of this URL is until document close, which is fine
197 // for Special:Upload -- if this code gets used on longer-running
198 // pages, add a revokeObjectURL() when it's no longer needed.
200 // Prefer this over readAsDataURL for Firefox 7 due to bug reading
201 // some SVG files from data URIs <https://bugzilla.mozilla.org/show_bug.cgi?id=694165>
202 callback( window.URL.createObjectURL( file ) );
204 // This ends up decoding the file to base-64 and back again, which
205 // feels horribly inefficient.
206 reader.onload = function () {
207 callback( reader.result );
209 reader.readAsDataURL( file );
214 * Format a file size attractively.
215 * @todo match numeric formatting
220 function prettySize( s ) {
221 var sizeMsgs = ['size-bytes', 'size-kilobytes', 'size-megabytes', 'size-gigabytes'];
222 while ( s >= 1024 && sizeMsgs.length > 1 ) {
224 sizeMsgs = sizeMsgs.slice( 1 );
226 return mw.msg( sizeMsgs[0], Math.round( s ) );
230 * Clear the file upload preview area.
232 function clearPreview() {
233 $( '#mw-upload-thumbnail' ).remove();
237 * Check if the file does not exceed the maximum size
239 function checkMaxUploadSize( file ) {
242 function getMaxUploadSize( type ) {
243 var sizes = mw.config.get( 'wgMaxUploadSize' );
245 if ( sizes[type] !== undefined ) {
251 $( '.mw-upload-source-error' ).remove();
253 maxSize = getMaxUploadSize( 'file' );
254 if ( file.size > maxSize ) {
255 $error = $( '<p class="error mw-upload-source-error" id="wpSourceTypeFile-error">' +
256 mw.message( 'largefileserver', file.size, maxSize ).escaped() + '</p>' );
258 $( '#wpUploadFile' ).after( $error );
270 if ( hasFileAPI() ) {
271 // Update thumbnail when the file selection control is updated.
272 $( '#wpUploadFile' ).change( function () {
274 if ( this.files && this.files.length ) {
275 // Note: would need to be updated to handle multiple files.
276 var file = this.files[0];
278 if ( !checkMaxUploadSize( file ) ) {
282 if ( fileIsPreviewable( file ) ) {
291 * Disable all upload source fields except the selected one
293 $( document ).ready( function () {
295 $rows = $( '.mw-htmlform-field-UploadSourceField' );
297 function createHandler( $currentRow ) {
299 * @param {jQuery.Event}
302 $( '.mw-upload-source-error' ).remove();
303 if ( this.checked ) {
304 // Disable all inputs
305 $rows.find( 'input[name!="wpSourceType"]' ).prop( 'disabled', true );
306 // Re-enable the current one
307 $currentRow.find( 'input' ).prop( 'disabled', false );
312 for ( i = $rows.length; i; i-- ) {
313 $row = $rows.eq(i - 1);
315 .find( 'input[name="wpSourceType"]' )
316 .change( createHandler( $row ) );
320 }( mediaWiki, jQuery ) );