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
23 use Wikimedia\FileBackend\FileBackend
;
24 use Wikimedia\FileBackend\FSFile\FSFile
;
25 use Wikimedia\Mime\MimeAnalyzer
;
28 * MimeMagic helper wrapper
33 /** @var MimeAnalyzer */
37 * @param MimeAnalyzer $magic
39 public function __construct( MimeAnalyzer
$magic ) {
40 $this->magic
= $magic;
44 * Get an associative array containing information about
45 * a file with the given storage path.
47 * Resulting array fields include:
49 * - size (filesize in bytes)
50 * - mime (as major/minor)
51 * - media_type (value to be used with the MEDIATYPE_xxx constants)
52 * - metadata (handler specific)
61 * @param string $path Filesystem path to a file
62 * @param string|bool|null $ext The file extension, or true to extract it from the filename.
63 * Set it to false to ignore the extension. Might be null in case the file is going to be
68 public function getPropsFromPath( $path, $ext ) {
69 $fsFile = new FSFile( $path );
71 $info = $this->newPlaceholderProps();
72 $info['fileExists'] = $fsFile->exists();
73 if ( $info['fileExists'] ) {
74 $info['size'] = $fsFile->getSize(); // bytes
75 $info['sha1'] = $fsFile->getSha1Base36();
77 # MIME type according to file contents
78 $info['file-mime'] = $this->magic
->guessMimeType( $path, false );
80 $ext = ( $ext === true ) ? FileBackend
::extensionFromPath( $path ) : (string)$ext;
82 # XXX: MimeAnalyzer::improveTypeFromExtension() may return null (T253483).
83 # Unclear if callers of this method expect that.
84 $info['mime'] = $this->magic
->improveTypeFromExtension( $info['file-mime'], $ext );
86 [ $info['major_mime'], $info['minor_mime'] ] = File
::splitMime( $info['mime'] );
87 $info['media_type'] = $this->magic
->getMediaType( $path, $info['mime'] );
89 # Height, width and metadata
90 // @phan-suppress-next-line PhanTypeMismatchArgumentNullable See XXX above
91 $handler = MediaHandler
::getHandler( $info['mime'] );
93 $sizeAndMetadata = $handler->getSizeAndMetadataWithFallback( $fsFile, $path );
94 if ( $sizeAndMetadata ) {
95 $info = $sizeAndMetadata +
$info;
104 * Empty place holder props for non-existing files
106 * Resulting array fields include:
108 * - size (filesize in bytes)
109 * - mime (as major/minor)
110 * - media_type (value to be used with the MEDIATYPE_xxx constants)
111 * - metadata (handler specific)
112 * - sha1 (in base 36)
123 public function newPlaceholderProps() {
124 return FSFile
::placeholderProps() +
[
129 'media_type' => MEDIATYPE_UNKNOWN