| 1 | /* |
| 2 | * This file is part of the MediaWiki extension MultimediaViewer. |
| 3 | * |
| 4 | * MultimediaViewer is free software: you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License as published by |
| 6 | * the Free Software Foundation, either version 2 of the License, or |
| 7 | * (at your option) any later version. |
| 8 | * |
| 9 | * MultimediaViewer is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with MultimediaViewer. If not, see <http://www.gnu.org/licenses/>. |
| 16 | */ |
| 17 | |
| 18 | const config = require( '../config.json' ); |
| 19 | |
| 20 | /** |
| 21 | * Loads an image. |
| 22 | */ |
| 23 | class ImageProvider { |
| 24 | /** |
| 25 | * @param {string} imageQueryParameter When defined, is a query parameter to add to every image request |
| 26 | */ |
| 27 | constructor( imageQueryParameter = config.imageQueryParameter ) { |
| 28 | this.imageQueryParameter = imageQueryParameter; |
| 29 | |
| 30 | /** |
| 31 | * AJAX call cache. |
| 32 | * |
| 33 | * @property {Object.<string, jQuery.Promise>} cache |
| 34 | * @protected |
| 35 | */ |
| 36 | this.cache = {}; |
| 37 | } |
| 38 | |
| 39 | /** |
| 40 | * Loads an image and returns it. When the browser supports it, the image is loaded as an AJAX |
| 41 | * request. |
| 42 | * |
| 43 | * @param {string} url |
| 44 | * @return {jQuery.Promise.<HTMLImageElement>} A promise which resolves to the image object. |
| 45 | * When loaded via AJAX, it has progress events, which return an array with the content loaded |
| 46 | * so far and with the progress as a floating-point number between 0 and 100. |
| 47 | */ |
| 48 | get( url ) { |
| 49 | const cacheKey = url; |
| 50 | |
| 51 | if ( this.imageQueryParameter ) { |
| 52 | try { |
| 53 | const uri = new URL( url, location ); |
| 54 | uri.searchParams.set( this.imageQueryParameter, '' ); |
| 55 | url = uri.toString(); |
| 56 | } catch ( error ) { |
| 57 | return $.Deferred().reject( error.message ); |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | if ( !this.cache[ cacheKey ] ) { |
| 62 | this.cache[ cacheKey ] = this.rawGet( url, this.imagePreloadingSupported() ); |
| 63 | this.cache[ cacheKey ].fail( ( error ) => { |
| 64 | mw.log( `${ this.constructor.name } provider failed to load: `, error ); |
| 65 | } ); |
| 66 | } |
| 67 | |
| 68 | return this.cache[ cacheKey ]; |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * Internal version of get(): no caching, no performance metrics. |
| 73 | * |
| 74 | * @param {string} url |
| 75 | * @param {boolean} [cors] if true, use CORS for preloading |
| 76 | * @return {jQuery.Promise.<HTMLImageElement>} a promise which resolves to the image object |
| 77 | */ |
| 78 | rawGet( url, cors ) { |
| 79 | const img = new window.Image(); |
| 80 | const deferred = $.Deferred(); |
| 81 | |
| 82 | // This attribute is necessary in Firefox, which needs it for the image request after |
| 83 | // the XHR to hit the cache by being a proper CORS request. |
| 84 | if ( cors ) { |
| 85 | img.crossOrigin = 'anonymous'; |
| 86 | } |
| 87 | |
| 88 | img.onload = () => deferred.resolve( img ); |
| 89 | img.onerror = () => deferred.reject( `could not load image from ${ url }` ); |
| 90 | |
| 91 | img.src = url; |
| 92 | |
| 93 | return deferred; |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * Checks whether the current browser supports AJAX preloading of images. |
| 98 | * This means that: |
| 99 | * - the browser supports CORS requests (large wiki farms usually host images on a |
| 100 | * separate domain) and |
| 101 | * - either AJAX and normal image loading uses the same cache (when an image is used by a CORS |
| 102 | * request, and then normally by setting img.src, it is only loaded once) |
| 103 | * - or (as is the case with Firefox) they are cached separately, but that can be changed by |
| 104 | * setting the crossOrigin attribute |
| 105 | * |
| 106 | * @return {boolean} |
| 107 | */ |
| 108 | imagePreloadingSupported() { |
| 109 | // This checks if the browser supports CORS requests in XHRs |
| 110 | return window.XMLHttpRequest !== undefined && 'withCredentials' in new XMLHttpRequest(); |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | module.exports = ImageProvider; |