Debian Codegraph @

extensions/MultimediaViewer/resources/mmv/provider/mmv.provider.GuessedThumbnailInfo.js

raw symbols SCIP
1 /*
2 * This file is part of the MediaWiki extension MediaViewer.
3 *
4 * MediaViewer 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 * MediaViewer 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 MediaViewer. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18 const Thumbnail = require( '../model/mmv.model.Thumbnail.js' );
19
20 /**
21 * This provider is similar to ThumbnailInfo, but instead of making an API call
22 * to get the thumbnail URL, it tries to guess it. There are two failure modes:
23 * - known failure: in the given situation it does not seem possible or safe to guess the URL.
24 * It is up to the caller to obtain it by falling back to the normal provider.
25 * - unexpected failure: we guess an URL but it does not work. The current implementation is
26 * conservative so at least on WMF wikis this probably won't happen, but should be reckoned
27 * with anyway. On other wikis (especially ones which do not generate thumbnails on demand
28 * via the 404 handler) this could be more frequent. Again, it is the caller's responsibility
29 * to handle this by detecting image loading errors and falling back to the normal provider.
30 */
31 class GuessedThumbnailInfo {
32
33 /**
34 * File extensions which are vector types (as opposed to bitmap).
35 * Thumbnails of vector types can be larger than the original file.
36 *
37 * @return {Object.<string, number>}
38 */
39 get vectorExtensions() {
40 return {
41 svg: 1
42 };
43 }
44
45 /**
46 * File extensions which can be displayed in the browser.
47 * Other file types need to be thumbnailed even if the size of the original file would be right.
48 *
49 * @return {Object.<string, number>}
50 */
51 get displayableExtensions() {
52 return {
53 png: 1,
54 jpg: 1,
55 jpeg: 1,
56 gif: 1
57 };
58 }
59
60 /**
61 * Try to guess the thumbnailinfo for a thumbnail without doing an API request.
62 * An existing thumbnail URL is required.
63 *
64 * There is no guarantee this function will be successful - in some cases, it is impossible
65 * to guess how the URL would look. If that's the case, the promise just rejects.
66 *
67 * @param {mw.Title} file
68 * @param {string} sampleUrl a thumbnail URL for the same file (but with different size).
69 * @param {number} width thumbnail width in pixels
70 * @param {number} originalWidth width of original image in pixels
71 * @param {number} originalHeight height of original image in pixels
72 * @return {jQuery.Promise.<Thumbnail>}
73 */
74 get( file, sampleUrl, width, originalWidth, originalHeight ) {
75 const url = this.getUrl( file, sampleUrl, width, originalWidth );
76 if ( url ) {
77 return $.Deferred().resolve( new Thumbnail(
78 url,
79 this.guessWidth( file, width, originalWidth ),
80 this.guessHeight( file, width, originalWidth, originalHeight )
81 ) );
82 } else {
83 return $.Deferred().reject( 'Could not guess thumbnail URL' );
84 }
85 }
86
87 /**
88 * Try to guess the URL of a thumbnail without doing an API request.
89 * See #get().
90 *
91 * @param {mw.Title} file
92 * @param {string} sampleUrl a thumbnail URL for the same file (but with different size)
93 * @param {number} width thumbnail width in pixels
94 * @param {number} originalWidth width of original image in pixels
95 * @return {string|undefined} a thumbnail URL or nothing
96 */
97 getUrl( file, sampleUrl, width, originalWidth ) {
98 const needsFullSize = this.needsOriginal( file, width, originalWidth );
99 const sampleIsFullSize = this.isFullSizeUrl( sampleUrl, file );
100
101 if ( sampleIsFullSize && needsFullSize ) {
102 // sample thumbnail uses full size, and we need full size as well - the sample URL
103 // happens to be just the right one for us
104 return sampleUrl;
105 } else if ( !sampleIsFullSize && !needsFullSize ) {
106 // need to convert a scaled thumbnail URL to another scaled thumbnail URL
107 return this.replaceSize( file, sampleUrl, width );
108 } else if ( !sampleIsFullSize && needsFullSize ) {
109 if ( this.canBeDisplayedInBrowser( file ) ) {
110 // the size requested is larger than the original - we need to return an URL
111 // to the original file instead
112 return this.guessFullUrl( file, sampleUrl );
113 } else {
114 // the size requested is larger than the original, but this file type cannot
115 // be displayed by all browsers, so needs to be thumbnailed anyway,
116 // but the thumbnail still cannot be larger than the original file
117 return this.replaceSize( file, sampleUrl, originalWidth );
118 }
119 } else { // sampleIsFullSize && !needsOriginal
120 return this.guessThumbUrl( file, sampleUrl, width );
121 }
122 }
123
124 /**
125 * True if the original image needs to be used as a thumbnail.
126 *
127 * @protected
128 * @param {mw.Title} file
129 * @param {number} width thumbnail width in pixels
130 * @param {number} originalWidth width of original image in pixels
131 * @return {boolean}
132 */
133 needsOriginal( file, width, originalWidth ) {
134 return width >= originalWidth && !this.canHaveLargerThumbnailThanOriginal( file );
135 }
136
137 /**
138 * Checks if a given thumbnail URL is full-size (the original image) or scaled
139 *
140 * @protected
141 * @param {string} url a thumbnail URL
142 * @param {mw.Title} file
143 * @return {boolean}
144 */
145 isFullSizeUrl( url, file ) {
146 return !this.obscureFilename( url, file ).match( '/thumb/' );
147 }
148
149 /**
150 * Removes the filename in a reversible way. This is useful because the filename can be nearly
151 * anything and could cause false positives when looking for patterns.
152 *
153 * @protected
154 * @param {string} url a thumbnail URL
155 * @param {mw.Title} file
156 * @return {string} thumbnail URL with occurrences of the filename replaced by `<filename>`
157 */
158 obscureFilename( url, file ) {
159 // corresponds to File::getUrlRel() which uses rawurlencode()
160 const filenameInUrl = mw.util.rawurlencode( file.getMain() );
161
162 // In the URL to the original file the filename occurs once. In a thumbnail URL it usually
163 // occurs twice, but can occur once if it is too short. We replace twice, can't hurt.
164 return url.replace( filenameInUrl, '<filename>' ).replace( filenameInUrl, '<filename>' );
165 }
166
167 /**
168 * Undoes #obscureFilename().
169 *
170 * @protected
171 * @param {string} url a thumbnail URL (with obscured filename)
172 * @param {mw.Title} file
173 * @return {string} original thumbnail URL
174 */
175 restoreFilename( url, file ) {
176 // corresponds to File::getUrlRel() which uses rawurlencode()
177 const filenameInUrl = mw.util.rawurlencode( file.getMain() );
178
179 // <> cannot be used in titles, so this is safe
180 return url.replace( '<filename>', filenameInUrl ).replace( '<filename>', filenameInUrl );
181 }
182
183 /**
184 * True if the file is of a type for which the thumbnail can be scaled beyond the original size.
185 *
186 * @protected
187 * @param {mw.Title} file
188 * @return {boolean}
189 */
190 canHaveLargerThumbnailThanOriginal( file ) {
191 return ( file.getExtension().toLowerCase() in this.vectorExtensions );
192 }
193
194 /**
195 * True if the file type can be displayed in most browsers, false if it needs thumbnailing
196 *
197 * @protected
198 * @param {mw.Title} file
199 * @return {boolean}
200 */
201 canBeDisplayedInBrowser( file ) {
202 return ( file.getExtension().toLowerCase() in this.displayableExtensions );
203 }
204
205 /**
206 * Guess what will be the width of the thumbnail. (Thumbnails for most file formats cannot be
207 * larger than the original file so this might be smaller than the requested width.)
208 *
209 * @protected
210 * @param {mw.Title} file
211 * @param {number} width thumbnail width in pixels
212 * @param {number} originalWidth width of original image in pixels
213 * @return {number} guessed width
214 */
215 guessWidth( file, width, originalWidth ) {
216 if ( width >= originalWidth && !this.canHaveLargerThumbnailThanOriginal( file ) ) {
217 return originalWidth;
218 } else {
219 return width;
220 }
221 }
222
223 /**
224 * Guess what will be the height of the thumbnail, given its width.
225 *
226 * @protected
227 * @param {mw.Title} file
228 * @param {number} width thumbnail width in pixels
229 * @param {number} originalWidth width of original image in pixels
230 * @param {number} originalHeight height of original image in pixels
231 * @return {number} guessed height
232 */
233 guessHeight( file, width, originalWidth, originalHeight ) {
234 if ( width >= originalWidth && !this.canHaveLargerThumbnailThanOriginal( file ) ) {
235 return originalHeight;
236 } else {
237 // might be off 1px due to rounding (we don't know what exact scaling method the
238 // backend uses) but that should not cause any issues
239 return Math.round( width * ( originalHeight / originalWidth ) );
240 }
241 }
242
243 /**
244 * Given a thumbnail URL with a wrong size, returns one with the right size.
245 *
246 * @protected
247 * @param {mw.Title} file
248 * @param {string} sampleUrl a thumbnail URL for the same file (but with different size)
249 * @param {number} width thumbnail width in pixels
250 * @return {string|undefined} thumbnail URL with the correct size
251 */
252 replaceSize( file, sampleUrl, width ) {
253 let url = this.obscureFilename( sampleUrl, file );
254 const sizeRegexp = /\b\d{1,5}px\b/;
255
256 // this should never happen, but let's play it safe - returning the sample URL and believing
257 // it is the resized one would be bad. Returning a wrong filename is not catastrophic
258 // as long as we return a non-working wrong filename, which would not be the case here.
259 if ( !url.match( sizeRegexp ) ) {
260 return undefined;
261 }
262
263 // we are assuming here that the other thumbnail parameters do not look like a size
264 url = url.replace( sizeRegexp, `${ width }px` );
265
266 return this.restoreFilename( url, file );
267 }
268
269 /**
270 * Try to guess the original URL to the file, from a thumb URL.
271 *
272 * @protected
273 * @param {mw.Title} file
274 * @param {string} thumbnailUrl
275 * @return {string} URL of the original file
276 */
277 guessFullUrl( file, thumbnailUrl ) {
278 let url = this.obscureFilename( thumbnailUrl, file );
279
280 if ( url === thumbnailUrl ) {
281 // Did not find the filename, maybe due to URL encoding issues. Bail out.
282 return undefined;
283 }
284
285 // this depends on some config settings, but will work with default or WMF settings.
286 url = url.replace( /<filename>.*/, '<filename>' );
287 url = url.replace( '/thumb', '' );
288
289 return this.restoreFilename( url, file );
290 }
291
292 /**
293 * Hardest version: try to guess thumbnail URL from original
294 *
295 * @protected
296 * @param {mw.Title} file
297 * @param {string} originalUrl URL for the original file
298 * @param {number} width thumbnail width in pixels
299 * @return {string|undefined} thumbnail URL
300 */
301 guessThumbUrl() {
302 // Not implemented. This can be very complicated (the thumbnail might have other
303 // parameters than the size, which are impossible to guess, might be converted to some
304 // other format, might have a special shortened format depending on the length of the
305 // filename) and it is unlikely to be useful - it would be only called when we need
306 // a thumbnail that is smaller than the sample (the thumbnail which is already on the page).
307 return undefined;
308 }
309 }
310
311 module.exports = GuessedThumbnailInfo;