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
|
|
|
/** |
24
|
|
|
* MimeMagic helper wrapper |
25
|
|
|
* |
26
|
|
|
* @since 1.28 |
27
|
|
|
*/ |
28
|
|
|
class MWFileProps { |
29
|
|
|
/** @var MimeMagic */ |
30
|
|
|
private $magic; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @param MimeMagic $magic |
34
|
|
|
*/ |
35
|
|
|
public function __construct( MimeMagic $magic ) { |
36
|
|
|
$this->magic = $magic; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Get an associative array containing information about |
41
|
|
|
* a file with the given storage path. |
42
|
|
|
* |
43
|
|
|
* Resulting array fields include: |
44
|
|
|
* - fileExists |
45
|
|
|
* - size (filesize in bytes) |
46
|
|
|
* - mime (as major/minor) |
47
|
|
|
* - media_type (value to be used with the MEDIATYPE_xxx constants) |
48
|
|
|
* - metadata (handler specific) |
49
|
|
|
* - sha1 (in base 36) |
50
|
|
|
* - width |
51
|
|
|
* - height |
52
|
|
|
* - bits (bitrate) |
53
|
|
|
* - file-mime |
54
|
|
|
* - major_mime |
55
|
|
|
* - minor_mime |
56
|
|
|
* |
57
|
|
|
* @param string $path Filesystem path to a file |
58
|
|
|
* @param string|bool $ext The file extension, or true to extract it from the filename. |
59
|
|
|
* Set it to false to ignore the extension. |
60
|
|
|
* @return array |
61
|
|
|
* @since 1.28 |
62
|
|
|
*/ |
63
|
|
|
public function getPropsFromPath( $path, $ext ) { |
64
|
|
|
$fsFile = new FSFile( $path ); |
65
|
|
|
|
66
|
|
|
$info = $this->newPlaceholderProps(); |
67
|
|
|
$info['fileExists'] = $fsFile->exists(); |
68
|
|
|
if ( $info['fileExists'] ) { |
69
|
|
|
$info['size'] = $fsFile->getSize(); // bytes |
70
|
|
|
$info['sha1'] = $fsFile->getSha1Base36(); |
71
|
|
|
|
72
|
|
|
# MIME type according to file contents |
73
|
|
|
$info['file-mime'] = $this->magic->guessMimeType( $path, false ); |
74
|
|
|
# Logical MIME type |
75
|
|
|
$ext = ( $ext === true ) ? FileBackend::extensionFromPath( $path ) : $ext; |
76
|
|
|
$info['mime'] = $this->magic->improveTypeFromExtension( $info['file-mime'], $ext ); |
|
|
|
|
77
|
|
|
|
78
|
|
|
list( $info['major_mime'], $info['minor_mime'] ) = File::splitMime( $info['mime'] ); |
79
|
|
|
$info['media_type'] = $this->magic->getMediaType( $path, $info['mime'] ); |
80
|
|
|
|
81
|
|
|
# Height, width and metadata |
82
|
|
|
$handler = MediaHandler::getHandler( $info['mime'] ); |
83
|
|
|
if ( $handler ) { |
84
|
|
|
$info['metadata'] = $handler->getMetadata( $fsFile, $path ); |
85
|
|
|
/** @noinspection PhpMethodParametersCountMismatchInspection */ |
86
|
|
|
$gis = $handler->getImageSize( $fsFile, $path, $info['metadata'] ); |
87
|
|
|
if ( is_array( $gis ) ) { |
88
|
|
|
$info = $this->extractImageSizeInfo( $gis ) + $info; |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
return $info; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Exract image size information |
98
|
|
|
* |
99
|
|
|
* @param array $gis |
100
|
|
|
* @return array |
101
|
|
|
*/ |
102
|
|
|
private function extractImageSizeInfo( array $gis ) { |
103
|
|
|
$info = []; |
104
|
|
|
# NOTE: $gis[2] contains a code for the image type. This is no longer used. |
105
|
|
|
$info['width'] = $gis[0]; |
106
|
|
|
$info['height'] = $gis[1]; |
107
|
|
|
if ( isset( $gis['bits'] ) ) { |
108
|
|
|
$info['bits'] = $gis['bits']; |
109
|
|
|
} else { |
110
|
|
|
$info['bits'] = 0; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
return $info; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Empty place holder props for non-existing files |
118
|
|
|
* |
119
|
|
|
* Resulting array fields include: |
120
|
|
|
* - fileExists |
121
|
|
|
* - size (filesize in bytes) |
122
|
|
|
* - mime (as major/minor) |
123
|
|
|
* - media_type (value to be used with the MEDIATYPE_xxx constants) |
124
|
|
|
* - metadata (handler specific) |
125
|
|
|
* - sha1 (in base 36) |
126
|
|
|
* - width |
127
|
|
|
* - height |
128
|
|
|
* - bits (bitrate) |
129
|
|
|
* - file-mime |
130
|
|
|
* - major_mime |
131
|
|
|
* - minor_mime |
132
|
|
|
* |
133
|
|
|
* @return array |
134
|
|
|
* @since 1.28 |
135
|
|
|
*/ |
136
|
|
|
public function newPlaceholderProps() { |
137
|
|
|
return FSFile::placeholderProps() + [ |
138
|
|
|
'metadata' => '', |
139
|
|
|
'width' => 0, |
140
|
|
|
'height' => 0, |
141
|
|
|
'bits' => 0, |
142
|
|
|
'media_type' => MEDIATYPE_UNKNOWN |
143
|
|
|
]; |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
|
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.