Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
60.00% |
15 / 25 |
|
0.00% |
0 / 5 |
CRAP | |
0.00% |
0 / 1 |
TiffHandler | |
60.00% |
15 / 25 |
|
0.00% |
0 / 5 |
14.18 | |
0.00% |
0 / 1 |
canRender | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
mustRender | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getThumbType | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
getSizeAndMetadata | |
93.75% |
15 / 16 |
|
0.00% |
0 / 1 |
4.00 | |||
isExpensiveToThumbnail | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 |
1 | <?php |
2 | /** |
3 | * Handler for Tiff images. |
4 | * |
5 | * This program is free software; you can redistribute it and/or modify |
6 | * it under the terms of the GNU General Public License as published by |
7 | * the Free Software Foundation; either version 2 of the License, or |
8 | * (at your option) any later version. |
9 | * |
10 | * This program is distributed in the hope that it will be useful, |
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
13 | * GNU General Public License for more details. |
14 | * |
15 | * You should have received a copy of the GNU General Public License along |
16 | * with this program; if not, write to the Free Software Foundation, Inc., |
17 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
18 | * http://www.gnu.org/copyleft/gpl.html |
19 | * |
20 | * @file |
21 | * @ingroup Media |
22 | */ |
23 | |
24 | use MediaWiki\MainConfigNames; |
25 | use MediaWiki\MediaWikiServices; |
26 | |
27 | /** |
28 | * Handler for Tiff images. |
29 | * |
30 | * @ingroup Media |
31 | */ |
32 | class TiffHandler extends ExifBitmapHandler { |
33 | /** |
34 | * TIFF files over 10M are considered expensive to thumbnail |
35 | */ |
36 | private const EXPENSIVE_SIZE_LIMIT = 10_485_760; |
37 | |
38 | /** |
39 | * Conversion to PNG for inline display can be disabled here... |
40 | * Note scaling should work with ImageMagick, but may not with GD scaling. |
41 | * |
42 | * Files pulled from an another MediaWiki instance via ForeignAPIRepo / |
43 | * InstantCommons will have thumbnails managed from the remote instance, |
44 | * so we can skip this check. |
45 | * |
46 | * @param File $file |
47 | * @return bool |
48 | */ |
49 | public function canRender( $file ) { |
50 | $tiffThumbnailType = MediaWikiServices::getInstance()->getMainConfig() |
51 | ->get( MainConfigNames::TiffThumbnailType ); |
52 | |
53 | return (bool)$tiffThumbnailType |
54 | || $file->getRepo() instanceof ForeignAPIRepo; |
55 | } |
56 | |
57 | /** |
58 | * Browsers don't support TIFF inline generally... |
59 | * For inline display, we need to convert to PNG. |
60 | * |
61 | * @param File $file |
62 | * @return bool |
63 | */ |
64 | public function mustRender( $file ) { |
65 | return true; |
66 | } |
67 | |
68 | /** |
69 | * @param string $ext |
70 | * @param string $mime |
71 | * @param array|null $params |
72 | * @return array |
73 | */ |
74 | public function getThumbType( $ext, $mime, $params = null ) { |
75 | $tiffThumbnailType = MediaWikiServices::getInstance()->getMainConfig() |
76 | ->get( MainConfigNames::TiffThumbnailType ); |
77 | |
78 | return $tiffThumbnailType; |
79 | } |
80 | |
81 | public function getSizeAndMetadata( $state, $filename ) { |
82 | $showEXIF = MediaWikiServices::getInstance()->getMainConfig()->get( MainConfigNames::ShowEXIF ); |
83 | |
84 | try { |
85 | $meta = BitmapMetadataHandler::Tiff( $filename ); |
86 | if ( !is_array( $meta ) ) { |
87 | // This should never happen, but doesn't hurt to be paranoid. |
88 | throw new InvalidTiffException( 'Metadata array is not an array' ); |
89 | } |
90 | $info = [ |
91 | 'width' => $meta['ImageWidth'] ?? 0, |
92 | 'height' => $meta['ImageLength'] ?? 0, |
93 | ]; |
94 | $info = $this->applyExifRotation( $info, $meta ); |
95 | if ( $showEXIF ) { |
96 | $meta['MEDIAWIKI_EXIF_VERSION'] = Exif::version(); |
97 | $info['metadata'] = $meta; |
98 | } |
99 | return $info; |
100 | } catch ( InvalidTiffException $e ) { |
101 | // BitmapMetadataHandler throws an exception in certain exceptional |
102 | // cases like if file does not exist. |
103 | wfDebug( __METHOD__ . ': ' . $e->getMessage() ); |
104 | |
105 | return [ 'metadata' => [ '_error' => ExifBitmapHandler::BROKEN_FILE ] ]; |
106 | } |
107 | } |
108 | |
109 | public function isExpensiveToThumbnail( $file ) { |
110 | return $file->getSize() > static::EXPENSIVE_SIZE_LIMIT; |
111 | } |
112 | } |