(bug 26995) File size is now checked before uploading in HTML5 browsers
authorBryan Tong Minh <btongminh@users.mediawiki.org>
Fri, 4 Mar 2011 15:38:37 +0000 (15:38 +0000)
committerBryan Tong Minh <btongminh@users.mediawiki.org>
Fri, 4 Mar 2011 15:38:37 +0000 (15:38 +0000)
* Exposed $wgMaxUploadSize to JS
* Exposed message largefileserver to JS

RELEASE-NOTES
includes/specials/SpecialUpload.php
includes/upload/UploadBase.php
resources/Resources.php
resources/mediawiki.special/mediawiki.special.upload.js

index f6aa977..e39753e 100644 (file)
@@ -86,6 +86,7 @@ PHP if you have not done so prior to upgrading MediaWiki.
   potentially dangerous Java applets. This allows applets to be blocked
   specifically, rather than all ZIP files being blocked.
 * (bug 2429) Allow selection of associated namespace in recent changes
+* (bug 26995) File size is now checked before uploading in HTML5 browsers
 
 === Bug fixes in 1.18 ===
 * (bug 23119) WikiError class and subclasses are now marked as deprecated
index 8525e52..52bee73 100644 (file)
@@ -771,6 +771,8 @@ class UploadForm extends HTMLForm {
        protected $mTextAfterSummary;
 
        protected $mSourceIds;
+       
+       protected $mMaxFileSize = array();
 
        public function __construct( $options = array() ) {
                $this->mWatch = !empty( $options['watch'] );
@@ -851,6 +853,10 @@ class UploadForm extends HTMLForm {
                        );
                }
 
+               $this->mMaxUploadSize['file'] = min( 
+                       wfShorthandToInteger( ini_get( 'upload_max_filesize' ) ), 
+                       UploadBase::getMaxUploadSize( 'file' ) );
+                       
                $descriptor['UploadFile'] = array(
                        'class' => 'UploadSourceField',
                        'section' => 'source',
@@ -861,17 +867,12 @@ class UploadForm extends HTMLForm {
                        'radio' => &$radio,
                        'help' => wfMsgExt( 'upload-maxfilesize',
                                        array( 'parseinline', 'escapenoentities' ),
-                                       $wgLang->formatSize(
-                                               wfShorthandToInteger( min( 
-                                                       wfShorthandToInteger(
-                                                               ini_get( 'upload_max_filesize' )
-                                                       ), UploadBase::getMaxUploadSize( 'file' )
-                                               ) )
-                                       )
+                                       $wgLang->formatSize( $this->mMaxUploadSize['file'] )
                                ) . ' ' . wfMsgHtml( 'upload_source_file' ),
                        'checked' => $selectedSourceType == 'file',
                );
                if ( $canUploadByUrl ) {
+                       $this->mMaxUploadSize['url'] = UploadBase::getMaxUploadSize( 'url' );
                        $descriptor['UploadFileURL'] = array(
                                'class' => 'UploadSourceField',
                                'section' => 'source',
@@ -881,7 +882,7 @@ class UploadForm extends HTMLForm {
                                'radio' => &$radio,
                                'help' => wfMsgExt( 'upload-maxfilesize',
                                                array( 'parseinline', 'escapenoentities' ),
-                                               $wgLang->formatSize( UploadBase::getMaxUploadSize( 'url' ) )
+                                               $wgLang->formatSize( $this->mMaxUploadSize['url'] )
                                        ) . ' ' . wfMsgHtml( 'upload_source_url' ),
                                'checked' => $selectedSourceType == 'url',
                        );
@@ -1095,6 +1096,7 @@ class UploadForm extends HTMLForm {
 
                $useAjaxDestCheck = $wgUseAjax && $wgAjaxUploadDestCheck;
                $useAjaxLicensePreview = $wgUseAjax && $wgAjaxLicensePreview && $wgEnableAPI;
+               $this->mMaxUploadSize['*'] = UploadBase::getMaxUploadSize();
 
                $scriptVars = array(
                        'wgAjaxUploadDestCheck' => $useAjaxDestCheck,
@@ -1106,6 +1108,7 @@ class UploadForm extends HTMLForm {
                        'wgUploadSourceIds' => $this->mSourceIds,
                        'wgStrictFileExtensions' => $wgStrictFileExtensions,
                        'wgCapitalizeUploads' => MWNamespace::isCapitalized( NS_FILE ),
+                       'wgMaxUploadSize' => $this->mMaxUploadSize,
                );
 
                $wgOut->addScript( Skin::makeVariablesScript( $scriptVars ) );
index fdbd0fc..b485949 100644 (file)
@@ -1310,7 +1310,7 @@ abstract class UploadBase {
                global $wgMaxUploadSize;
                
                if ( is_array( $wgMaxUploadSize ) ) {
-                       if ( !is_null( $forType) && isset( $wgMaxUploadSize[$forType] ) ) {
+                       if ( !is_null( $forType ) && isset( $wgMaxUploadSize[$forType] ) ) {
                                return $wgMaxUploadSize[$forType];
                        } else {
                                return $wgMaxUploadSize['*'];
index e8947b9..a9b1002 100644 (file)
@@ -436,6 +436,7 @@ return array(
                        'size-kilobytes',
                        'size-megabytes',
                        'size-gigabytes',
+                       'largefileserver',
                ),
                'dependencies' => array( 'mediawiki.util.jpegmeta' ),
        ),
index bfb65a0..ee86ea9 100644 (file)
@@ -191,7 +191,33 @@ jQuery( function( $ ) {
                $( '#mw-upload-thumbnail' ).remove();
        }
        
-
+       /**
+        * Check if the file does not exceed the maximum size
+        */
+       function checkMaxUploadSize( file ) {
+               function getMaxUploadSize( type ) {
+                       sizes = mw.config.get( 'wgMaxUploadSize' );
+                       if ( sizes[type] !== undefined ) {
+                               return sizes[type];
+                       }
+                       return sizes['*'];
+               }
+               $( '.mw-upload-source-error' ).remove();
+               
+               maxSize = getMaxUploadSize( 'file' ); 
+               if ( file.size > maxSize ) {
+                       error = $( '<p class="error mw-upload-source-error" id="wpSourceTypeFile-error">' + 
+                                       mw.msg( 'largefileserver', file.size, maxSize ) + '</p>' );
+                       $( '#wpUploadFile' ).after( error );
+                       return false;
+               }
+               return true;
+       }
+       
+       
+       /**
+        * Initialization
+        */
        if ( hasFileAPI() ) {
                // Update thumbnail when the file selection control is updated.
                $( '#wpUploadFile' ).change( function() {
@@ -199,6 +225,11 @@ jQuery( function( $ ) {
                        if ( this.files && this.files.length ) {
                                // Note: would need to be updated to handle multiple files.
                                var file = this.files[0];
+                               
+                               if ( !checkMaxUploadSize( file ) ) {
+                                       return;
+                               }
+                               
                                if ( fileIsPreviewable( file ) ) {
                                        showPreview( file );
                                }
@@ -217,6 +248,7 @@ jQuery( function ( $ ) {
                $( 'input[name="wpSourceType"]', row ).change( function () {
                        var currentRow = row; // Store current row in our own scope
                        return function () {
+                               $( '.mw-upload-source-error' ).remove();
                                if ( this.checked ) {
                                        // Disable all inputs
                                        $( 'input[name!="wpSourceType"]', rows ).attr( 'disabled', true );
@@ -227,3 +259,4 @@ jQuery( function ( $ ) {
                }() );
        }
 } );
+