mime: Update usage of MimeAnalyzer methods
Follow-up to I93bd71ec1. Bug: T252228 Change-Id: I45c9fc592c9e41e0868e7d965206d4c04f4f92e1
This commit is contained in:
parent
7e01e86e09
commit
19931e069f
12 changed files with 27 additions and 32 deletions
|
|
@ -122,7 +122,7 @@ class StreamFile {
|
|||
// Use the extension only, rather than magic numbers, to avoid opening
|
||||
// up vulnerabilities due to uploads of files with allowed extensions
|
||||
// but disallowed types.
|
||||
$type = $magic->guessTypesForExtension( $ext );
|
||||
$type = $magic->getMimeTypeFromExtensionOrNull( $ext );
|
||||
|
||||
/**
|
||||
* Double-check some security settings that were done on upload but might
|
||||
|
|
|
|||
|
|
@ -75,9 +75,9 @@ abstract class ApiFormatBase extends ApiBase {
|
|||
} elseif ( $this->getIsHtml() ) {
|
||||
return 'api-result.html';
|
||||
} else {
|
||||
$exts = MediaWikiServices::getInstance()->getMimeAnalyzer()
|
||||
->getExtensionsForType( $this->getMimeType() );
|
||||
$ext = $exts ? strtok( $exts, ' ' ) : strtolower( $this->mFormat );
|
||||
$mimeAnalyzer = MediaWikiServices::getInstance()->getMimeAnalyzer();
|
||||
$ext = $mimeAnalyzer->getExtensionFromMimeTypeOrNull( $this->getMimeType() )
|
||||
?? strtolower( $this->mFormat );
|
||||
return "api-result.$ext";
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -91,9 +91,8 @@ abstract class ChannelFeed extends FeedItem {
|
|||
header( "Content-type: $mimetype; charset=UTF-8" );
|
||||
|
||||
// Set a sane filename
|
||||
$exts = MediaWiki\MediaWikiServices::getInstance()->getMimeAnalyzer()
|
||||
->getExtensionsForType( $mimetype );
|
||||
$ext = $exts ? strtok( $exts, ' ' ) : 'xml';
|
||||
$mimeAnalyzer = MediaWiki\MediaWikiServices::getInstance()->getMimeAnalyzer();
|
||||
$ext = $mimeAnalyzer->getExtensionFromMimeTypeOrNull( $mimetype ) ?? 'xml';
|
||||
header( "Content-Disposition: inline; filename=\"feed.{$ext}\"" );
|
||||
|
||||
if ( $wgVaryOnXFP ) {
|
||||
|
|
|
|||
|
|
@ -291,7 +291,7 @@ class FileBackendGroup {
|
|||
public function guessMimeInternal( $storagePath, $content, $fsPath ) {
|
||||
// Trust the extension of the storage path (caller must validate)
|
||||
$ext = FileBackend::extensionFromPath( $storagePath );
|
||||
$type = $this->mimeAnalyzer->guessTypesForExtension( $ext );
|
||||
$type = $this->mimeAnalyzer->getMimeTypeFromExtensionOrNull( $ext );
|
||||
// For files without a valid extension (or one at all), inspect the contents
|
||||
if ( !$type && $fsPath ) {
|
||||
$type = $this->mimeAnalyzer->guessMimeType( $fsPath, false );
|
||||
|
|
|
|||
|
|
@ -292,7 +292,7 @@ class ForeignAPIFile extends File {
|
|||
public function getMimeType() {
|
||||
if ( !isset( $this->mInfo['mime'] ) ) {
|
||||
$magic = MediaWiki\MediaWikiServices::getInstance()->getMimeAnalyzer();
|
||||
$this->mInfo['mime'] = $magic->guessTypesForExtension( $this->getExtension() );
|
||||
$this->mInfo['mime'] = $magic->getMimeTypeFromExtensionOrNull( $this->getExtension() );
|
||||
}
|
||||
|
||||
return $this->mInfo['mime'];
|
||||
|
|
|
|||
|
|
@ -362,7 +362,7 @@ class DjVuHandler extends ImageHandler {
|
|||
static $mime;
|
||||
if ( !isset( $mime ) ) {
|
||||
$magic = MediaWiki\MediaWikiServices::getInstance()->getMimeAnalyzer();
|
||||
$mime = $magic->guessTypesForExtension( $wgDjvuOutputExtension );
|
||||
$mime = $magic->getMimeTypeFromExtensionOrNull( $wgDjvuOutputExtension );
|
||||
}
|
||||
|
||||
return [ $wgDjvuOutputExtension, $mime ];
|
||||
|
|
|
|||
|
|
@ -297,9 +297,9 @@ abstract class MediaHandler {
|
|||
if ( !$ext || $magic->isMatchingExtension( $ext, $mime ) === false ) {
|
||||
// The extension is not valid for this MIME type and we do
|
||||
// recognize the MIME type
|
||||
$extensions = $magic->getExtensionsForType( $mime );
|
||||
if ( $extensions ) {
|
||||
return [ strtok( $extensions, ' ' ), $mime ];
|
||||
$knownExt = $magic->getExtensionFromMimeTypeOrNull( $mime );
|
||||
if ( $knownExt !== null ) {
|
||||
return [ $knownExt, $mime ];
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -241,17 +241,15 @@ class SpecialMediaStatistics extends QueryPage {
|
|||
*/
|
||||
private function getExtensionList( $mime ) {
|
||||
$exts = MediaWiki\MediaWikiServices::getInstance()->getMimeAnalyzer()
|
||||
->getExtensionsForType( $mime );
|
||||
if ( $exts === null ) {
|
||||
->getExtensionsFromMimeType( $mime );
|
||||
if ( !$exts ) {
|
||||
return '';
|
||||
}
|
||||
$extArray = explode( ' ', $exts );
|
||||
$extArray = array_unique( $extArray );
|
||||
foreach ( $extArray as &$ext ) {
|
||||
foreach ( $exts as &$ext ) {
|
||||
$ext = htmlspecialchars( '.' . $ext );
|
||||
}
|
||||
|
||||
return $this->getLanguage()->commaList( $extArray );
|
||||
return $this->getLanguage()->commaList( $exts );
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -444,7 +444,7 @@ abstract class UploadBase {
|
|||
fclose( $fp );
|
||||
|
||||
$magic = MediaWiki\MediaWikiServices::getInstance()->getMimeAnalyzer();
|
||||
$extMime = $magic->guessTypesForExtension( $this->mFinalExtension );
|
||||
$extMime = $magic->getMimeTypeFromExtensionOrNull( $this->mFinalExtension );
|
||||
$ieTypes = $magic->getIEMimeTypes( $this->mTempPath, $chunk, $extMime );
|
||||
foreach ( $ieTypes as $ieType ) {
|
||||
if ( $this->checkFileExtension( $ieType, $wgMimeTypeBlacklist ) ) {
|
||||
|
|
@ -1021,10 +1021,10 @@ abstract class UploadBase {
|
|||
$mime = $magic->guessMimeType( $this->mTempPath );
|
||||
if ( $mime !== 'unknown/unknown' ) {
|
||||
# Get a space separated list of extensions
|
||||
$extList = $magic->getExtensionsForType( $mime );
|
||||
if ( $extList ) {
|
||||
$mimeExt = $magic->getExtensionFromMimeTypeOrNull( $mime );
|
||||
if ( $mimeExt !== null ) {
|
||||
# Set the extension to the canonical extension
|
||||
$this->mFinalExtension = strtok( $extList, ' ' );
|
||||
$this->mFinalExtension = $mimeExt;
|
||||
|
||||
# Fix up the other variables
|
||||
$this->mFilteredName .= ".{$this->mFinalExtension}";
|
||||
|
|
|
|||
|
|
@ -485,10 +485,7 @@ class UploadStash {
|
|||
// If not, assume that it should be related to the MIME type of the original file.
|
||||
$magic = MediaWiki\MediaWikiServices::getInstance()->getMimeAnalyzer();
|
||||
$mimeType = $magic->guessMimeType( $path );
|
||||
$extensions = $magic->getExtensionsForType( $mimeType );
|
||||
if ( $extensions !== null ) {
|
||||
$extension = strtok( $extensions, ' ' );
|
||||
}
|
||||
$extension = $magic->getExtensionFromMimeTypeOrNull( $mimeType );
|
||||
}
|
||||
|
||||
if ( $extension === null ) {
|
||||
|
|
|
|||
|
|
@ -208,7 +208,7 @@ class MimeAnalyzerTest extends PHPUnit\Framework\TestCase {
|
|||
* The empty string is not a MIME type and should not be mapped to a file extension.
|
||||
*/
|
||||
public function testNoEmptyStringMimeType() {
|
||||
$this->assertNull( $this->mimeAnalyzer->getExtensionsForType( '' ) );
|
||||
$this->assertSame( [], $this->mimeAnalyzer->getExtensionsFromMimeType( '' ) );
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -227,7 +227,8 @@ class MimeAnalyzerTest extends PHPUnit\Framework\TestCase {
|
|||
},
|
||||
] );
|
||||
$this->assertSame( MEDIATYPE_OFFICE, $mimeAnalyzer->findMediaType( '.fake_extension' ) );
|
||||
$this->assertSame( 'fake/mime', $mimeAnalyzer->getTypesForExtension( 'no_such_extension' ) );
|
||||
$this->assertSame(
|
||||
'fake/mime', $mimeAnalyzer->getMimeTypeFromExtensionOrNull( 'no_such_extension' ) );
|
||||
}
|
||||
|
||||
public function testGetMimeTypesFromExtension() {
|
||||
|
|
|
|||
|
|
@ -389,7 +389,7 @@ trait FileBackendGroupTestTrait {
|
|||
* @param string|null $content
|
||||
* @param string|null $fsPath
|
||||
* @param string|null $expectedExtensionType Expected return of
|
||||
* MimeAnalyzer::guessTypesForExtension
|
||||
* MimeAnalyzer::getMimeTypeFromExtensionOrNull
|
||||
* @param string|null $expectedGuessedMimeType Expected return value of
|
||||
* MimeAnalyzer::guessMimeType (null if expected not to be called)
|
||||
*/
|
||||
|
|
@ -401,7 +401,7 @@ trait FileBackendGroupTestTrait {
|
|||
$expectedGuessedMimeType
|
||||
) {
|
||||
$mimeAnalyzer = $this->createMock( MimeAnalyzer::class );
|
||||
$mimeAnalyzer->expects( $this->once() )->method( 'guessTypesForExtension' )
|
||||
$mimeAnalyzer->expects( $this->once() )->method( 'getMimeTypeFromExtensionOrNull' )
|
||||
->willReturn( $expectedExtensionType );
|
||||
$tmpFileFactory = $this->createMock( TempFSFileFactory::class );
|
||||
|
||||
|
|
@ -425,7 +425,7 @@ trait FileBackendGroupTestTrait {
|
|||
}
|
||||
|
||||
$mimeAnalyzer->expects( $this->never() )
|
||||
->method( $this->anythingBut( 'guessTypesForExtension', 'guessMimeType' ) );
|
||||
->method( $this->anythingBut( 'getMimeTypeFromExtensionOrNull', 'guessMimeType' ) );
|
||||
$tmpFileFactory->expects( $this->never() )
|
||||
->method( $this->anythingBut( 'newTempFSFile' ) );
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue