Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
25 / 25 |
|
100.00% |
3 / 3 |
CRAP | |
100.00% |
1 / 1 |
MWFileProps | |
100.00% |
25 / 25 |
|
100.00% |
3 / 3 |
7 | |
100.00% |
1 / 1 |
__construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getPropsFromPath | |
100.00% |
17 / 17 |
|
100.00% |
1 / 1 |
5 | |||
newPlaceholderProps | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
1 |
1 | <?php |
2 | /** |
3 | * MimeMagic helper functions for detecting and dealing with MIME types. |
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 | */ |
22 | |
23 | use Wikimedia\FileBackend\FileBackend; |
24 | |
25 | /** |
26 | * MimeMagic helper wrapper |
27 | * |
28 | * @since 1.28 |
29 | */ |
30 | class MWFileProps { |
31 | /** @var MimeAnalyzer */ |
32 | private $magic; |
33 | |
34 | /** |
35 | * @param MimeAnalyzer $magic |
36 | */ |
37 | public function __construct( MimeAnalyzer $magic ) { |
38 | $this->magic = $magic; |
39 | } |
40 | |
41 | /** |
42 | * Get an associative array containing information about |
43 | * a file with the given storage path. |
44 | * |
45 | * Resulting array fields include: |
46 | * - fileExists |
47 | * - size (filesize in bytes) |
48 | * - mime (as major/minor) |
49 | * - media_type (value to be used with the MEDIATYPE_xxx constants) |
50 | * - metadata (handler specific) |
51 | * - sha1 (in base 36) |
52 | * - width |
53 | * - height |
54 | * - bits (bitrate) |
55 | * - file-mime |
56 | * - major_mime |
57 | * - minor_mime |
58 | * |
59 | * @param string $path Filesystem path to a file |
60 | * @param string|bool|null $ext The file extension, or true to extract it from the filename. |
61 | * Set it to false to ignore the extension. Might be null in case the file is going to be |
62 | * stashed. |
63 | * @return array |
64 | * @since 1.28 |
65 | */ |
66 | public function getPropsFromPath( $path, $ext ) { |
67 | $fsFile = new FSFile( $path ); |
68 | |
69 | $info = $this->newPlaceholderProps(); |
70 | $info['fileExists'] = $fsFile->exists(); |
71 | if ( $info['fileExists'] ) { |
72 | $info['size'] = $fsFile->getSize(); // bytes |
73 | $info['sha1'] = $fsFile->getSha1Base36(); |
74 | |
75 | # MIME type according to file contents |
76 | $info['file-mime'] = $this->magic->guessMimeType( $path, false ); |
77 | # Logical MIME type |
78 | $ext = ( $ext === true ) ? FileBackend::extensionFromPath( $path ) : (string)$ext; |
79 | |
80 | # XXX: MimeAnalyzer::improveTypeFromExtension() may return null (T253483). |
81 | # Unclear if callers of this method expect that. |
82 | $info['mime'] = $this->magic->improveTypeFromExtension( $info['file-mime'], $ext ); |
83 | |
84 | [ $info['major_mime'], $info['minor_mime'] ] = File::splitMime( $info['mime'] ); |
85 | $info['media_type'] = $this->magic->getMediaType( $path, $info['mime'] ); |
86 | |
87 | # Height, width and metadata |
88 | // @phan-suppress-next-line PhanTypeMismatchArgumentNullable See XXX above |
89 | $handler = MediaHandler::getHandler( $info['mime'] ); |
90 | if ( $handler ) { |
91 | $sizeAndMetadata = $handler->getSizeAndMetadataWithFallback( $fsFile, $path ); |
92 | if ( $sizeAndMetadata ) { |
93 | $info = $sizeAndMetadata + $info; |
94 | } |
95 | } |
96 | } |
97 | |
98 | return $info; |
99 | } |
100 | |
101 | /** |
102 | * Empty place holder props for non-existing files |
103 | * |
104 | * Resulting array fields include: |
105 | * - fileExists |
106 | * - size (filesize in bytes) |
107 | * - mime (as major/minor) |
108 | * - media_type (value to be used with the MEDIATYPE_xxx constants) |
109 | * - metadata (handler specific) |
110 | * - sha1 (in base 36) |
111 | * - width |
112 | * - height |
113 | * - bits (bitrate) |
114 | * - file-mime |
115 | * - major_mime |
116 | * - minor_mime |
117 | * |
118 | * @return array |
119 | * @since 1.28 |
120 | */ |
121 | public function newPlaceholderProps() { |
122 | return FSFile::placeholderProps() + [ |
123 | 'metadata' => [], |
124 | 'width' => 0, |
125 | 'height' => 0, |
126 | 'bits' => 0, |
127 | 'media_type' => MEDIATYPE_UNKNOWN |
128 | ]; |
129 | } |
130 | } |