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