MediaWiki master
MWFileProps.php
Go to the documentation of this file.
1<?php
9namespace MediaWiki\Utils;
10
11use MediaHandler;
15use Wikimedia\Mime\MimeAnalyzer;
16
24 private $magic;
25
26 public function __construct( MimeAnalyzer $magic ) {
27 $this->magic = $magic;
28 }
29
55 public function getPropsFromPath( $path, $ext ) {
56 $fsFile = new FSFile( $path );
57
58 $info = $this->newPlaceholderProps();
59 $info['fileExists'] = $fsFile->exists();
60 if ( $info['fileExists'] ) {
61 $info['size'] = $fsFile->getSize(); // bytes
62 $info['sha1'] = $fsFile->getSha1Base36();
63
64 # MIME type according to file contents
65 $info['file-mime'] = $this->magic->guessMimeType( $path, false );
66 # Logical MIME type
67 $ext = ( $ext === true ) ? FileBackend::extensionFromPath( $path ) : (string)$ext;
68
69 # XXX: MimeAnalyzer::improveTypeFromExtension() may return null (T253483, T413926).
70 # Unclear if callers of this method expect that.
71 $info['mime'] = $this->magic->improveTypeFromExtension( $info['file-mime'], $ext );
72
73 [ $info['major_mime'], $info['minor_mime'] ] = File::splitMime( $info['mime'] );
74 $info['media_type'] = $this->magic->getMediaType( $path, $info['mime'] );
75
76 # Height, width and metadata
77 $handler = $info['mime'] !== null ? MediaHandler::getHandler( $info['mime'] ) : null;
78 if ( $handler ) {
79 $sizeAndMetadata = $handler->getSizeAndMetadataWithFallback( $fsFile, $path );
80 if ( $sizeAndMetadata ) {
81 $info = $sizeAndMetadata + $info;
82 }
83 }
84 }
85
86 return $info;
87 }
88
109 public function newPlaceholderProps() {
110 return FSFile::placeholderProps() + [
111 'metadata' => [],
112 'width' => 0,
113 'height' => 0,
114 'bits' => 0,
115 'media_type' => MEDIATYPE_UNKNOWN
116 ];
117 }
118}
119
121class_alias( MWFileProps::class, 'MWFileProps' );
const MEDIATYPE_UNKNOWN
Definition defines.php:13
Implements some public methods and some protected utility functions which are required by multiple ch...
Definition File.php:80
MimeMagic helper wrapper.
__construct(MimeAnalyzer $magic)
newPlaceholderProps()
Empty place holder props for non-existing files.
getPropsFromPath( $path, $ext)
Get an associative array containing information about a file with the given storage path.
Class representing a non-directory file on the file system.
Definition FSFile.php:20
Base class for all file backend classes (including multi-write backends).
static extensionFromPath( $path, $case='lowercase')
Get the final extension from a storage or FS path.
Copyright (C) 2017 Kunal Mehta legoktm@debian.org