Index: trunk/phase3/resources/mediawiki.special/mediawiki.special.upload.js |
— | — | @@ -25,7 +25,7 @@ |
26 | 26 | * @return boolean |
27 | 27 | */ |
28 | 28 | function fileIsPreviewable( file ) { |
29 | | - var known = ['image/png', 'image/gif', 'image/jpeg'], |
| 29 | + var known = ['image/png', 'image/gif', 'image/jpeg', 'image/svg+xml'], |
30 | 30 | tooHuge = 10 * 1024 * 1024; |
31 | 31 | return ( $.inArray( file.type, known ) !== -1 ) && file.size > 0 && file.size < tooHuge; |
32 | 32 | } |
— | — | @@ -160,20 +160,33 @@ |
161 | 161 | */ |
162 | 162 | function fetchPreview( file, callback, callbackBinary ) { |
163 | 163 | var reader = new FileReader(); |
164 | | - reader.onload = function() { |
165 | | - if ( callbackBinary ) { |
| 164 | + if ( callbackBinary ) { |
| 165 | + // To fetch JPEG metadata we need a binary string; start there. |
| 166 | + // todo: |
| 167 | + reader.onload = function() { |
166 | 168 | callbackBinary( reader.result ); |
167 | | - reader.onload = function() { |
168 | | - callback( reader.result ); |
169 | | - }; |
170 | | - reader.readAsDataURL( file ); |
171 | | - } else { |
172 | | - callback( reader.result ); |
173 | | - } |
174 | | - }; |
175 | | - if ( callbackBinary ) { |
| 169 | + |
| 170 | + // Now run back through the regular code path. |
| 171 | + fetchPreview(file, callback ); |
| 172 | + }; |
176 | 173 | reader.readAsBinaryString( file ); |
| 174 | + } else if ('URL' in window && 'createObjectURL' in window.URL) { |
| 175 | + // Supported in Firefox 4.0 and above <https://developer.mozilla.org/en/DOM/window.URL.createObjectURL> |
| 176 | + // WebKit has it in a namespace for now but that's ok. ;) |
| 177 | + // |
| 178 | + // Lifetime of this URL is until document close, which is fine |
| 179 | + // for Special:Upload -- if this code gets used on longer-running |
| 180 | + // pages, add a revokeObjectURL() when it's no longer needed. |
| 181 | + // |
| 182 | + // Prefer this over readAsDataURL for Firefox 7 due to bug reading |
| 183 | + // some SVG files from data URIs <https://bugzilla.mozilla.org/show_bug.cgi?id=694165> |
| 184 | + callback(window.URL.createObjectURL(file)); |
177 | 185 | } else { |
| 186 | + // This ends up decoding the file to base-64 and back again, which |
| 187 | + // feels horribly inefficient. |
| 188 | + reader.onload = function() { |
| 189 | + callback( reader.result ); |
| 190 | + }; |
178 | 191 | reader.readAsDataURL( file ); |
179 | 192 | } |
180 | 193 | } |