Skip to content

Commit

Permalink
Fix #19243: Handle finfo_open for tar.xz as `application/octet-stre…
Browse files Browse the repository at this point in the history
…am` on PHP 8.1
  • Loading branch information
longthanhtran authored Feb 19, 2022
1 parent 9bf7c96 commit 7b8c29d
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 10 deletions.
5 changes: 5 additions & 0 deletions build/controllers/MimeTypeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,11 @@ private function generateMimeTypesFile($outFile, $content)
$mimeMap = array_merge($mimeMap, $this->additionalMimeTypes);
ksort($mimeMap);
$array = VarDumper::export($mimeMap);

if (PHP_VERSION_ID >= 80100) {
$array = array_replace($array, array('xz' => 'application/octet-stream'));
}

$content = <<<EOD
<?php
/**
Expand Down
3 changes: 2 additions & 1 deletion framework/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ Yii Framework 2 Change Log
2.0.46 under development
------------------------

- no changes in this release.
- Bug #19243: Handle `finfo_open` for tar.xz as `application/octet-stream` on PHP 8.1 (longthanhtran)
- Bug #19235: Fix return type compatibility of `yii\web\SessionIterator` class methods for PHP 8.1 (virtual-designer)


2.0.45 February 11, 2022
------------------------

Expand Down
4 changes: 0 additions & 4 deletions framework/helpers/BaseFileHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -162,10 +162,6 @@ public static function getMimeType($file, $magicFile = null, $checkExtension = t
throw new InvalidConfigException('The fileinfo PHP extension is not installed.');
}

if (PHP_VERSION_ID >= 80100) {
return static::getMimeTypeByExtension($file, $magicFile);
}

$info = finfo_open(FILEINFO_MIME_TYPE, $magicFile);

if ($info) {
Expand Down
8 changes: 7 additions & 1 deletion framework/helpers/mimeTypes.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
* http://svn.apache.org/viewvc/httpd/httpd/trunk/docs/conf/mime.types?view=markup
* This file has been placed in the public domain for unlimited redistribution.
*/
return [
$mimeTypes = [
'3dml' => 'text/vnd.in3d.3dml',
'3ds' => 'image/x-3ds',
'3g2' => 'video/3gpp2',
Expand Down Expand Up @@ -995,3 +995,9 @@
'zirz' => 'application/vnd.zul',
'zmm' => 'application/vnd.handheld-entertainment+xml',
];

if (PHP_VERSION_ID >= 80100) {
$mimeTypes = array_replace($mimeTypes, array('xz' => 'application/octet-stream'));
}

return $mimeTypes;
18 changes: 18 additions & 0 deletions tests/framework/helpers/FileHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -717,6 +717,24 @@ public function testGetMimeType()
$this->assertTrue(in_array(FileHelper::getMimeType($file), ['application/json', 'text/plain']));
}

public function testGetUploadedImageMimeTypes()
{
$ds = DIRECTORY_SEPARATOR;
$phpunitPath = Yii::getAlias('@yiiunit');
$runtimeLocation = Yii::getAlias('@yiiunit/runtime');
$resourceSourceLocation = "{$phpunitPath}{$ds}framework{$ds}validators{$ds}data{$ds}mimeType";

$pngFile = "{$runtimeLocation}{$ds}php1234";
copy("{$resourceSourceLocation}{$ds}test.png", $pngFile);

$this->assertEquals('image/png', FileHelper::getMimeType($pngFile));

$jpgFile = "{$runtimeLocation}{$ds}php4567";
copy("{$resourceSourceLocation}{$ds}test.jpg", $jpgFile);

$this->assertEquals('image/jpeg', FileHelper::getMimeType($jpgFile));
}

public function testNormalizePath()
{
$ds = DIRECTORY_SEPARATOR;
Expand Down
12 changes: 9 additions & 3 deletions tests/framework/helpers/MimeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ public function testMimeAliases()

public function testMimeTypes()
{
$this->assertSame(
[

$coreMimeTypes = [
'3dml' => 'text/vnd.in3d.3dml',
'3ds' => 'image/x-3ds',
'3g2' => 'video/3gpp2',
Expand Down Expand Up @@ -1024,7 +1024,13 @@ public function testMimeTypes()
'zir' => 'application/vnd.zul',
'zirz' => 'application/vnd.zul',
'zmm' => 'application/vnd.handheld-entertainment+xml',
],
];

if (PHP_VERSION_ID >= 80100) {
$coreMimeTypes = array_replace($coreMimeTypes, array('xz' => 'application/octet-stream'));
}

$this->assertSame($coreMimeTypes,
require __DIR__ . '/../../../framework/helpers/mimeTypes.php'
);
}
Expand Down
10 changes: 9 additions & 1 deletion tests/framework/validators/FileValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -535,7 +535,7 @@ public function testValidateMimeTypeMaskInvalid($fileName, $mask)

public function validMimeTypes()
{
return array_filter([
$validMimeTypes = array_filter([
['test.svg', 'image/*', 'svg'],
['test.jpg', 'image/*', 'jpg'],
['test.png', 'image/*', 'png'],
Expand All @@ -545,6 +545,14 @@ public function validMimeTypes()
['test.odt', 'application/vnd*', 'odt'],
['test.tar.xz', 'application/x-xz', 'tar.xz'],
]);

if (PHP_VERSION_ID >= 80100) {
$v81_zx = ['test.tar.xz', 'application/octet-stream', 'tar.xz'];
array_pop($validMimeTypes);
$validMimeTypes[] = $v81_zx;
}

return $validMimeTypes;
}

public function invalidMimeTypes()
Expand Down

0 comments on commit 7b8c29d

Please sign in to comment.