3 * MimeMagic helper functions for detecting and dealing with MIME types.
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.
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.
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
24 * MimeMagic helper wrapper
33 * @param MimeMagic $magic
35 public function __construct( MimeMagic $magic ) {
36 $this->magic = $magic;
40 * Get an associative array containing information about
41 * a file with the given storage path.
43 * Resulting array fields include:
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)
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.
63 public function getPropsFromPath( $path, $ext ) {
64 $fsFile = new FSFile( $path );
66 $info = $this->newPlaceholderProps();
67 $info['fileExists'] = $fsFile->exists();
68 if ( $info['fileExists'] ) {
69 $info['size'] = $fsFile->getSize(); // bytes
70 $info['sha1'] = $fsFile->getSha1Base36();
72 # MIME type according to file contents
73 $info['file-mime'] = $this->magic->guessMimeType( $path, false );
75 $ext = ( $ext === true ) ? FileBackend::extensionFromPath( $path ) : $ext;
76 $info['mime'] = $this->magic->improveTypeFromExtension( $info['file-mime'], $ext );
78 list( $info['major_mime'], $info['minor_mime'] ) = File::splitMime( $info['mime'] );
79 $info['media_type'] = $this->magic->getMediaType( $path, $info['mime'] );
81 # Height, width and metadata
82 $handler = MediaHandler::getHandler( $info['mime'] );
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;
97 * Exract image size information
102 private function extractImageSizeInfo( array $gis ) {
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'];
117 * Empty place holder props for non-existing files
119 * Resulting array fields include:
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)
136 public function newPlaceholderProps() {
137 return FSFile::placeholderProps() + [
142 'media_type' => MEDIATYPE_UNKNOWN