Debian Codegraph @

includes/utils/MWFileProps.php

raw symbols SCIP
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 /**
37 * @param MimeAnalyzer $magic
38 */
39 public function __construct( MimeAnalyzer $magic ) {
40 $this->magic = $magic;
41 }
42
43 /**
44 * Get an associative array containing information about
45 * a file with the given storage path.
46 *
47 * Resulting array fields include:
48 * - fileExists
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)
53 * - sha1 (in base 36)
54 * - width
55 * - height
56 * - bits (bitrate)
57 * - file-mime
58 * - major_mime
59 * - minor_mime
60 *
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
64 * stashed.
65 * @return array
66 * @since 1.28
67 */
68 public function getPropsFromPath( $path, $ext ) {
69 $fsFile = new FSFile( $path );
70
71 $info = $this->newPlaceholderProps();
72 $info['fileExists'] = $fsFile->exists();
73 if ( $info['fileExists'] ) {
74 $info['size'] = $fsFile->getSize(); // bytes
75 $info['sha1'] = $fsFile->getSha1Base36();
76
77 # MIME type according to file contents
78 $info['file-mime'] = $this->magic->guessMimeType( $path, false );
79 # Logical MIME type
80 $ext = ( $ext === true ) ? FileBackend::extensionFromPath( $path ) : (string)$ext;
81
82 # XXX: MimeAnalyzer::improveTypeFromExtension() may return null (T253483, T413926).
83 # Unclear if callers of this method expect that.
84 $info['mime'] = $this->magic->improveTypeFromExtension( $info['file-mime'], $ext );
85
86 [ $info['major_mime'], $info['minor_mime'] ] = File::splitMime( $info['mime'] );
87 $info['media_type'] = $this->magic->getMediaType( $path, $info['mime'] );
88
89 # Height, width and metadata
90 $handler = $info['mime'] !== null ? MediaHandler::getHandler( $info['mime'] ) : null;
91 if ( $handler ) {
92 $sizeAndMetadata = $handler->getSizeAndMetadataWithFallback( $fsFile, $path );
93 if ( $sizeAndMetadata ) {
94 $info = $sizeAndMetadata + $info;
95 }
96 }
97 }
98
99 return $info;
100 }
101
102 /**
103 * Empty place holder props for non-existing files
104 *
105 * Resulting array fields include:
106 * - fileExists
107 * - size (filesize in bytes)
108 * - mime (as major/minor)
109 * - media_type (value to be used with the MEDIATYPE_xxx constants)
110 * - metadata (handler specific)
111 * - sha1 (in base 36)
112 * - width
113 * - height
114 * - bits (bitrate)
115 * - file-mime
116 * - major_mime
117 * - minor_mime
118 *
119 * @return array
120 * @since 1.28
121 */
122 public function newPlaceholderProps() {
123 return FSFile::placeholderProps() + [
124 'metadata' => [],
125 'width' => 0,
126 'height' => 0,
127 'bits' => 0,
128 'media_type' => MEDIATYPE_UNKNOWN
129 ];
130 }
131 }