X-Git-Url: https://git.heureux-cyclage.org/?p=lhc%2Fweb%2Fwiklou.git;a=blobdiff_plain;f=resources%2Fsrc%2Fmediawiki.special%2Fmediawiki.special.upload.js;h=de5ab87732f4660dc876e911272a0442b5fe235a;hp=9af7ab2a8f544b076b030d6e719ededf0b4654a0;hb=a2c8c2969420a0f150c03f76e3a0bf9028fcda43;hpb=74426f3cf796b149f1ae445e41815bbe148640b2 diff --git a/resources/src/mediawiki.special/mediawiki.special.upload.js b/resources/src/mediawiki.special/mediawiki.special.upload.js index 9af7ab2a8f..de5ab87732 100644 --- a/resources/src/mediawiki.special/mediawiki.special.upload.js +++ b/resources/src/mediawiki.special/mediawiki.special.upload.js @@ -6,7 +6,6 @@ * @singleton */ -/* eslint-disable no-use-before-define */ /* global Uint8Array */ ( function ( mw, $ ) { @@ -310,6 +309,73 @@ return mw.msg( sizeMsgs[ 0 ], Math.round( s ) ); } + /** + * Start loading a file into memory; when complete, pass it as a + * data URL to the callback function. If the callbackBinary is set it will + * first be read as binary and afterwards as data URL. Useful if you want + * to do preprocessing on the binary data first. + * + * @param {File} file + * @param {Function} callback + * @param {Function} callbackBinary + */ + function fetchPreview( file, callback, callbackBinary ) { + var reader = new FileReader(); + if ( callbackBinary && 'readAsBinaryString' in reader ) { + // To fetch JPEG metadata we need a binary string; start there. + // TODO + reader.onload = function () { + callbackBinary( reader.result ); + + // Now run back through the regular code path. + fetchPreview( file, callback ); + }; + reader.readAsBinaryString( file ); + } else if ( callbackBinary && 'readAsArrayBuffer' in reader ) { + // readAsArrayBuffer replaces readAsBinaryString + // However, our JPEG metadata library wants a string. + // So, this is going to be an ugly conversion. + reader.onload = function () { + var i, + buffer = new Uint8Array( reader.result ), + string = ''; + for ( i = 0; i < buffer.byteLength; i++ ) { + string += String.fromCharCode( buffer[ i ] ); + } + callbackBinary( string ); + + // Now run back through the regular code path. + fetchPreview( file, callback ); + }; + reader.readAsArrayBuffer( file ); + } else if ( 'URL' in window && 'createObjectURL' in window.URL ) { + // Supported in Firefox 4.0 and above + // WebKit has it in a namespace for now but that's ok. ;) + // + // Lifetime of this URL is until document close, which is fine + // for Special:Upload -- if this code gets used on longer-running + // pages, add a revokeObjectURL() when it's no longer needed. + // + // Prefer this over readAsDataURL for Firefox 7 due to bug reading + // some SVG files from data URIs + callback( window.URL.createObjectURL( file ) ); + } else { + // This ends up decoding the file to base-64 and back again, which + // feels horribly inefficient. + reader.onload = function () { + callback( reader.result ); + }; + reader.readAsDataURL( file ); + } + } + + /** + * Clear the file upload preview area. + */ + function clearPreview() { + $( '#mw-upload-thumbnail' ).remove(); + } + /** * Show a thumbnail preview of PNG, JPEG, GIF, and SVG files prior to upload * in browsers supporting HTML5 FileAPI. @@ -434,73 +500,6 @@ } : null ); } - /** - * Start loading a file into memory; when complete, pass it as a - * data URL to the callback function. If the callbackBinary is set it will - * first be read as binary and afterwards as data URL. Useful if you want - * to do preprocessing on the binary data first. - * - * @param {File} file - * @param {Function} callback - * @param {Function} callbackBinary - */ - function fetchPreview( file, callback, callbackBinary ) { - var reader = new FileReader(); - if ( callbackBinary && 'readAsBinaryString' in reader ) { - // To fetch JPEG metadata we need a binary string; start there. - // TODO - reader.onload = function () { - callbackBinary( reader.result ); - - // Now run back through the regular code path. - fetchPreview( file, callback ); - }; - reader.readAsBinaryString( file ); - } else if ( callbackBinary && 'readAsArrayBuffer' in reader ) { - // readAsArrayBuffer replaces readAsBinaryString - // However, our JPEG metadata library wants a string. - // So, this is going to be an ugly conversion. - reader.onload = function () { - var i, - buffer = new Uint8Array( reader.result ), - string = ''; - for ( i = 0; i < buffer.byteLength; i++ ) { - string += String.fromCharCode( buffer[ i ] ); - } - callbackBinary( string ); - - // Now run back through the regular code path. - fetchPreview( file, callback ); - }; - reader.readAsArrayBuffer( file ); - } else if ( 'URL' in window && 'createObjectURL' in window.URL ) { - // Supported in Firefox 4.0 and above - // WebKit has it in a namespace for now but that's ok. ;) - // - // Lifetime of this URL is until document close, which is fine - // for Special:Upload -- if this code gets used on longer-running - // pages, add a revokeObjectURL() when it's no longer needed. - // - // Prefer this over readAsDataURL for Firefox 7 due to bug reading - // some SVG files from data URIs - callback( window.URL.createObjectURL( file ) ); - } else { - // This ends up decoding the file to base-64 and back again, which - // feels horribly inefficient. - reader.onload = function () { - callback( reader.result ); - }; - reader.readAsDataURL( file ); - } - } - - /** - * Clear the file upload preview area. - */ - function clearPreview() { - $( '#mw-upload-thumbnail' ).remove(); - } - /** * Check if the file does not exceed the maximum size *