2 * JavaScript for Special:Upload
3 * Note that additional code still lives in skins/common/upload.js
6 jQuery( function( $ ) {
8 * Is the FileAPI available with sufficient functionality?
10 function hasFileAPI(){
11 return typeof window
.FileReader
!== 'undefined';
15 * Check if this is a recognizable image type...
16 * Also excludes files over 10M to avoid going insane on memory usage.
18 * @todo is there a way we can ask the browser what's supported in <img>s?
23 function fileIsPreviewable( file
) {
24 var known
= ['image/png', 'image/gif', 'image/jpeg', 'image/svg+xml'],
25 tooHuge
= 10 * 1024 * 1024;
26 return ( $.inArray( file
.type
, known
) !== -1 ) && file
.size
> 0 && file
.size
< tooHuge
;
30 * Show a thumbnail preview of PNG, JPEG, GIF, and SVG files prior to upload
31 * in browsers supporting HTML5 FileAPI.
33 * As of this writing, known good:
35 * - Chrome 7.something
37 * @todo check file size limits and warn of likely failures
41 function showPreview( file
) {
42 var previewSize
= 180,
43 thumb
= $( '<div id="mw-upload-thumbnail" class="thumb tright">' +
44 '<div class="thumbinner">' +
45 '<canvas width="' + previewSize
+ '" height="' + previewSize
+ '" ></canvas>' +
46 '<div class="thumbcaption"><div class="filename"></div><div class="fileinfo"></div></div>' +
49 thumb
.find( '.filename' ).text( file
.name
).end()
50 .find( '.fileinfo' ).text( prettySize( file
.size
) ).end();
52 var ctx
= thumb
.find( 'canvas' )[0].getContext( '2d' ),
53 spinner
= new Image();
54 spinner
.onload = function() {
55 ctx
.drawImage( spinner
, (previewSize
- spinner
.width
) / 2,
56 (previewSize
- spinner
.height
) / 2 );
58 spinner
.src
= mw
.config
.get( 'wgScriptPath' ) + '/skins/common/images/spinner.gif';
59 $( '#mw-htmlform-source' ).parent().prepend( thumb
);
62 fetchPreview( file
, function( dataURL
) {
63 var img
= new Image(),
66 if ( meta
&& meta
.tiff
.Orientation
) {
67 rotation
= (360 - function () {
68 // See includes/media/Bitmap.php
69 switch ( meta
.tiff
.Orientation
.value
) {
82 img
.onload = function() {
83 // Fit the image within the previewSizexpreviewSize box
84 if ( img
.width
> img
.height
) {
86 height
= img
.height
/ img
.width
* previewSize
;
89 width
= img
.width
/ img
.height
* previewSize
;
91 // Determine the offset required to center the image
92 dx
= (180 - width
) / 2;
93 dy
= (180 - height
) / 2;
95 // If a rotation is applied, the direction of the axis
96 // changes as well. You can derive the values below by
97 // drawing on paper an axis system, rotate it and see
98 // where the positive axis direction is
106 y
= dy
- previewSize
;
109 x
= dx
- previewSize
;
110 y
= dy
- previewSize
;
113 x
= dx
- previewSize
;
118 ctx
.clearRect( 0, 0, 180, 180 );
119 ctx
.rotate( rotation
/ 180 * Math
.PI
);
120 ctx
.drawImage( img
, x
, y
, width
, height
);
123 var info
= mw
.msg( 'widthheight', img
.width
, img
.height
) +
124 ', ' + prettySize( file
.size
);
125 $( '#mw-upload-thumbnail .fileinfo' ).text( info
);
128 }, mediaWiki
.config
.get( 'wgFileCanRotate' ) ? function ( data
) {
130 meta
= mediaWiki
.util
.jpegmeta( data
, file
.fileName
);
131 meta
._binary_data
= null;
139 * Start loading a file into memory; when complete, pass it as a
140 * data URL to the callback function. If the callbackBinary is set it will
141 * first be read as binary and afterwards as data URL. Useful if you want
142 * to do preprocessing on the binary data first.
145 * @param {function} callback
146 * @param {function} callbackBinary
148 function fetchPreview( file
, callback
, callbackBinary
) {
149 var reader
= new FileReader();
150 reader
.onload = function() {
151 if ( callbackBinary
) {
152 callbackBinary( reader
.result
);
153 reader
.onload = function() {
154 callback( reader
.result
);
156 reader
.readAsDataURL( file
);
158 callback( reader
.result
);
161 if ( callbackBinary
) {
162 reader
.readAsBinaryString( file
);
164 reader
.readAsDataURL( file
);
169 * Format a file size attractively.
170 * @todo match numeric formatting
175 function prettySize( s
) {
176 var sizes
= ['size-bytes', 'size-kilobytes', 'size-megabytes', 'size-gigabytes'];
177 while ( s
>= 1024 && sizes
.length
> 1 ) {
179 sizes
= sizes
.slice( 1 );
181 return mw
.msg( sizes
[0], Math
.round( s
) );
185 * Clear the file upload preview area.
187 function clearPreview() {
188 $( '#mw-upload-thumbnail' ).remove();
192 if ( hasFileAPI() ) {
193 // Update thumbnail when the file selection control is updated.
194 $( '#wpUploadFile' ).change( function() {
196 if ( this.files
&& this.files
.length
) {
197 // Note: would need to be updated to handle multiple files.
198 var file
= this.files
[0];
199 if ( fileIsPreviewable( file
) ) {