MediaWiki master
MWFileProps.php
Go to the documentation of this file.
1<?php
25use Wikimedia\Mime\MimeAnalyzer;
26
34 private $magic;
35
39 public function __construct( MimeAnalyzer $magic ) {
40 $this->magic = $magic;
41 }
42
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).
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 // @phan-suppress-next-line PhanTypeMismatchArgumentNullable See XXX above
91 $handler = MediaHandler::getHandler( $info['mime'] );
92 if ( $handler ) {
93 $sizeAndMetadata = $handler->getSizeAndMetadataWithFallback( $fsFile, $path );
94 if ( $sizeAndMetadata ) {
95 $info = $sizeAndMetadata + $info;
96 }
97 }
98 }
99
100 return $info;
101 }
102
123 public function newPlaceholderProps() {
124 return FSFile::placeholderProps() + [
125 'metadata' => [],
126 'width' => 0,
127 'height' => 0,
128 'bits' => 0,
129 'media_type' => MEDIATYPE_UNKNOWN
130 ];
131 }
132}
MimeMagic helper wrapper.
getPropsFromPath( $path, $ext)
Get an associative array containing information about a file with the given storage path.
newPlaceholderProps()
Empty place holder props for non-existing files.
__construct(MimeAnalyzer $magic)
Class representing a non-directory file on the file system.
Definition FSFile.php:34
Base class for all file backend classes (including multi-write backends).
const MEDIATYPE_UNKNOWN
Definition defines.php:27