| 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 Api = require( './mmv.provider.Api.js' ); |
| 19 | const Thumbnail = require( '../model/mmv.model.Thumbnail.js' ); |
| 20 | |
| 21 | /** |
| 22 | * Gets thumbnail information. |
| 23 | * |
| 24 | * See https://www.mediawiki.org/wiki/API:Properties#imageinfo_.2F_ii |
| 25 | */ |
| 26 | class ThumbnailInfo extends Api { |
| 27 | /** |
| 28 | * @param {mw.Api} api |
| 29 | * @param {Object} [options] |
| 30 | * @param {number} [options.maxage] cache expiration time, in seconds |
| 31 | * Will be used for both client-side cache (maxage) and reverse proxies (s-maxage) |
| 32 | */ |
| 33 | constructor( api, options ) { |
| 34 | super( api, options ); |
| 35 | } |
| 36 | |
| 37 | /** |
| 38 | * Runs an API GET request to get the thumbnail info for the specified size. |
| 39 | * The thumbnail always has the same aspect ratio as the full image. |
| 40 | * One of width or height can be null; if both are set, the API will return the largest |
| 41 | * thumbnail which fits into a width x height bounding box (or the full-sized image - whichever |
| 42 | * is smaller). |
| 43 | * |
| 44 | * @param {mw.Title} file |
| 45 | * @param {string} sampleUrl a thumbnail URL for the same file (but with different size). |
| 46 | * @param {number} width thumbnail width in pixels |
| 47 | * @param {number} height thumbnail height in pixels |
| 48 | * @return {jQuery.Promise.<Thumbnail>} |
| 49 | */ |
| 50 | get( file, sampleUrl, width, height ) { |
| 51 | const match = sampleUrl.match( /(lang|page)([\d\-a-z]+)-(\d+)px/ ); // multi lingual SVG or PDF page |
| 52 | const iiurlparam = match ? `${ match[ 1 ] }${ match[ 2 ] }-${ width }px` : undefined; |
| 53 | const cacheKey = [ file.getPrefixedDb(), width || '', height || '', iiurlparam || '' ].join(); |
| 54 | |
| 55 | return this.getCachedPromise( cacheKey, () => this.apiGetWithMaxAge( { |
| 56 | formatversion: 2, |
| 57 | action: 'query', |
| 58 | prop: 'imageinfo', |
| 59 | titles: file.getPrefixedDb(), |
| 60 | iiprop: 'url', |
| 61 | iiurlparam, |
| 62 | iiurlwidth: width, |
| 63 | iiurlheight: height |
| 64 | } ).then( ( data ) => this.getQueryPage( data ) ).then( ( page ) => { |
| 65 | let imageInfo; |
| 66 | if ( page.imageinfo && page.imageinfo[ 0 ] ) { |
| 67 | imageInfo = page.imageinfo[ 0 ]; |
| 68 | if ( imageInfo.thumburl && imageInfo.thumbwidth && imageInfo.thumbheight ) { |
| 69 | return $.Deferred().resolve( |
| 70 | new Thumbnail( |
| 71 | imageInfo.thumburl, |
| 72 | imageInfo.thumbwidth, |
| 73 | imageInfo.thumbheight |
| 74 | ) |
| 75 | ); |
| 76 | } else { |
| 77 | return $.Deferred().reject( 'error in provider, thumb info not found' ); |
| 78 | } |
| 79 | } else if ( page.missing === true && page.imagerepository === '' ) { |
| 80 | return $.Deferred().reject( `file does not exist: ${ file.getPrefixedDb() }` ); |
| 81 | } else { |
| 82 | return $.Deferred().reject( 'unknown error' ); |
| 83 | } |
| 84 | } ) ); |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | module.exports = ThumbnailInfo; |