aboutsummaryrefslogtreecommitdiffstats
path: root/includes/utils/MWFileProps.php
blob: c4a40cfa485f638a2ea3bdc3baabf9437d827f4a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
<?php
/**
 * MimeMagic helper functions for detecting and dealing with MIME types.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 * http://www.gnu.org/copyleft/gpl.html
 *
 * @file
 */

use Wikimedia\FileBackend\FileBackend;

/**
 * MimeMagic helper wrapper
 *
 * @since 1.28
 */
class MWFileProps {
	/** @var MimeAnalyzer */
	private $magic;

	/**
	 * @param MimeAnalyzer $magic
	 */
	public function __construct( MimeAnalyzer $magic ) {
		$this->magic = $magic;
	}

	/**
	 * Get an associative array containing information about
	 * a file with the given storage path.
	 *
	 * Resulting array fields include:
	 *   - fileExists
	 *   - size (filesize in bytes)
	 *   - mime (as major/minor)
	 *   - media_type (value to be used with the MEDIATYPE_xxx constants)
	 *   - metadata (handler specific)
	 *   - sha1 (in base 36)
	 *   - width
	 *   - height
	 *   - bits (bitrate)
	 *   - file-mime
	 *   - major_mime
	 *   - minor_mime
	 *
	 * @param string $path Filesystem path to a file
	 * @param string|bool|null $ext The file extension, or true to extract it from the filename.
	 *  Set it to false to ignore the extension. Might be null in case the file is going to be
	 *  stashed.
	 * @return array
	 * @since 1.28
	 */
	public function getPropsFromPath( $path, $ext ) {
		$fsFile = new FSFile( $path );

		$info = $this->newPlaceholderProps();
		$info['fileExists'] = $fsFile->exists();
		if ( $info['fileExists'] ) {
			$info['size'] = $fsFile->getSize(); // bytes
			$info['sha1'] = $fsFile->getSha1Base36();

			# MIME type according to file contents
			$info['file-mime'] = $this->magic->guessMimeType( $path, false );
			# Logical MIME type
			$ext = ( $ext === true ) ? FileBackend::extensionFromPath( $path ) : (string)$ext;

			# XXX: MimeAnalyzer::improveTypeFromExtension() may return null (T253483).
			# Unclear if callers of this method expect that.
			$info['mime'] = $this->magic->improveTypeFromExtension( $info['file-mime'], $ext );

			[ $info['major_mime'], $info['minor_mime'] ] = File::splitMime( $info['mime'] );
			$info['media_type'] = $this->magic->getMediaType( $path, $info['mime'] );

			# Height, width and metadata
			// @phan-suppress-next-line PhanTypeMismatchArgumentNullable See XXX above
			$handler = MediaHandler::getHandler( $info['mime'] );
			if ( $handler ) {
				$sizeAndMetadata = $handler->getSizeAndMetadataWithFallback( $fsFile, $path );
				if ( $sizeAndMetadata ) {
					$info = $sizeAndMetadata + $info;
				}
			}
		}

		return $info;
	}

	/**
	 * Empty place holder props for non-existing files
	 *
	 * Resulting array fields include:
	 *   - fileExists
	 *   - size (filesize in bytes)
	 *   - mime (as major/minor)
	 *   - media_type (value to be used with the MEDIATYPE_xxx constants)
	 *   - metadata (handler specific)
	 *   - sha1 (in base 36)
	 *   - width
	 *   - height
	 *   - bits (bitrate)
	 *   - file-mime
	 *   - major_mime
	 *   - minor_mime
	 *
	 * @return array
	 * @since 1.28
	 */
	public function newPlaceholderProps() {
		return FSFile::placeholderProps() + [
			'metadata' => [],
			'width' => 0,
			'height' => 0,
			'bits' => 0,
			'media_type' => MEDIATYPE_UNKNOWN
		];
	}
}