Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature] Reverse lookup #24

Merged
merged 26 commits into from
Aug 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
807f91b
add reverse matching of mimetype to possible extensions
maglnet Oct 20, 2021
dd3985a
fixes missing typehints
maglnet Oct 20, 2021
a526a36
Upgrade PHP-CS-Fixer to 3.x
villfa Oct 6, 2021
88f3804
Updated types
frankdejonge Nov 21, 2021
67fb7c5
Register test script
frankdejonge Nov 21, 2021
ef383a2
Prepare for 1.9.0
frankdejonge Nov 21, 2021
0a7c93c
fix 1.8.0 heading in CHANGELOG.md
phil-davis Nov 21, 2021
1520a3a
Inherit PHPUnit's TestCase instead of PHPStan's
rgson Nov 26, 2021
4e61edc
Update license year
Awilum Jan 2, 2022
cfc1698
Update mime-map
frankdejonge Apr 11, 2022
205cd2c
Expand inconclusive mime-type list and make it configurable.
frankdejonge Apr 11, 2022
ea7c49d
Prep changelog
frankdejonge Apr 11, 2022
d0119ae
Added test
frankdejonge Apr 17, 2022
8df8545
Added phar as application/octet-stream
frankdejonge Apr 17, 2022
1271db7
Updated mimetypes.
Sep 5, 2022
a436a00
update year
Awilum Jan 1, 2023
b6fb35e
Add stp
BlackbitDevs Sep 21, 2022
10f74d7
Add step file extension
BlackbitDevs Sep 21, 2022
838489b
add .stp + .step for application/STEP
BlackbitDevs Sep 29, 2022
3c457a1
Updated mime-times.
frankdejonge Aug 3, 2023
d1bb20e
Prep changelog
frankdejonge Aug 3, 2023
32796d9
Provide capability to lookup extensions for mime-types.
frankdejonge Aug 5, 2023
86216ae
Added missing file, make extension based implementation also use the …
frankdejonge Aug 5, 2023
56b1195
Updated phpunit and remove very old php versions.
frankdejonge Aug 5, 2023
9bdcb30
Use 7.4 as lowest version instead of 7.2
frankdejonge Aug 5, 2023
7c26dd1
Fixed #23: Corrected aac mimetype
frankdejonge Aug 5, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
/phpstan.neon export-ignore
/bin export-ignore
/.github export-ignore
/.php_cs.dist export-ignore
/.php-cs-fixer.dist.php export-ignore
/.scrutinizer.yml export-ignore
/src/Generation export-ignore
/test_files export-ignore
Expand Down
6 changes: 3 additions & 3 deletions .github/workflows/quality-assurance.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
php: [ '7.2', '7.3', '7.4', '8.0' ]
php: [ '7.4', '8.0', '8.1', '8.2' ]
composer-flags: [ '' ]
upgrade-aws-sdk: [ 'no' ]
phpunit-flags: [ '--coverage-text' ]
include:
- php: '7.2'
- php: '7.4'
composer-flags: '--prefer-lowest'
phpunit-flags: '--no-coverage'
steps:
Expand All @@ -39,6 +39,6 @@ jobs:
- run: COMPOSER_OPTS='${{ matrix.composer-flags }}' vendor/bin/phpunit ${{ matrix.phpunit-flags }}
- run: vendor/bin/phpstan analyse -l 6 src
if: ${{ matrix.php == '7.4' }}
- run: vendor/bin/php-cs-fixer fix --dry-run
- run: vendor/bin/php-cs-fixer fix --diff --dry-run
if: ${{ matrix.php == '7.4' }}

3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/vendor/
/coverage/
/.phpunit.result.cache
/.php_cs.cache
/.php-cs-fixer.php
/.php-cs-fixer.cache
/phpunit.xml
/composer.lock
4 changes: 2 additions & 2 deletions .php_cs.dist → .php-cs-fixer.dist.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@
->in([__DIR__ . '/src'])
;

return PhpCsFixer\Config::create()
return (new PhpCsFixer\Config())
->setRules([
'@PSR2' => true,
'array_syntax' => ['syntax' => 'short'],
'binary_operator_spaces' => true,
'single_line_after_imports' => true,
'blank_line_before_return' => true,
'blank_line_before_statement' => ['statements' => ['return']],
'cast_spaces' => true,
'concat_space' => ['spacing' => 'one'],
'no_singleline_whitespace_before_semicolons' => true,
Expand Down
24 changes: 24 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,29 @@
# Changelog

## 1.12.0 - 2022-08-03

### Updated

- Updated lookup

## 1.11.0 - 2022-04-17

### Updated

- Updated lookup

## 1.10.0 - 2022-04-11

### Fixed

- Added Flysystem v1 inconclusive mime-types and made it configurable as a constructor parameter.

## 1.9.0 - 2021-11-21

### Updated

- Updated lookup

## 1.8.0 - 2021-09-25

### Added
Expand Down
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Copyright (c) 2013-2020 Frank de Jonge
Copyright (c) 2013-2023 Frank de Jonge

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
9 changes: 5 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,17 @@
}
],
"scripts": {
"test": "vendor/bin/phpunit",
"phpstan": "vendor/bin/phpstan analyse -l 6 src"
},
"require": {
"php": "^7.2 || ^8.0",
"php": "^7.4 || ^8.0",
"ext-fileinfo": "*"
},
"require-dev": {
"phpunit/phpunit": "^8.5.8 || ^9.3",
"phpunit/phpunit": "^8.5.8 || ^9.3 || ^10.0",
"phpstan/phpstan": "^0.12.68",
"friendsofphp/php-cs-fixer": "^2.18"
"friendsofphp/php-cs-fixer": "^3.2"
},
"autoload": {
"psr-4": {
Expand All @@ -27,7 +28,7 @@
},
"config": {
"platform": {
"php": "7.2.0"
"php": "7.4.0"
}
}
}
14 changes: 14 additions & 0 deletions src/ExtensionLookup.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php
declare(strict_types=1);

namespace League\MimeTypeDetection;

interface ExtensionLookup
{
public function lookupExtension(string $mimetype): ?string;

/**
* @return string[]
*/
public function lookupAllExtensions(string $mimetype): array;
}
16 changes: 15 additions & 1 deletion src/ExtensionMimeTypeDetector.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

use const PATHINFO_EXTENSION;

class ExtensionMimeTypeDetector implements MimeTypeDetector
class ExtensionMimeTypeDetector implements MimeTypeDetector, ExtensionLookup
{
/**
* @var ExtensionToMimeTypeMap
Expand Down Expand Up @@ -39,4 +39,18 @@ public function detectMimeTypeFromBuffer(string $contents): ?string
{
return null;
}

public function lookupExtension(string $mimetype): ?string
{
return $this->extensions instanceof ExtensionLookup
? $this->extensions->lookupExtension($mimetype)
: null;
}

public function lookupAllExtensions(string $mimetype): array
{
return $this->extensions instanceof ExtensionLookup
? $this->extensions->lookupAllExtensions($mimetype)
: [];
}
}
3 changes: 2 additions & 1 deletion src/ExtensionMimeTypeDetectorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class ExtensionMimeTypeDetectorTest extends TestCase
{
/**
* @test
*
* @dataProvider expectedLookupResults
*/
public function looking_up_mimetype(string $path, ?string $expectedMimeType): void
Expand All @@ -35,7 +36,7 @@ public function detecting_from_bugger_always_returns_null(): void
$this->assertNull($mimeType);
}

public function expectedLookupResults(): Generator
public static function expectedLookupResults(): Generator
{
yield ['thing.jpg', 'image/jpeg'];
yield ['file.svg', 'image/svg+xml'];
Expand Down
35 changes: 31 additions & 4 deletions src/FinfoMimeTypeDetector.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,15 @@
use const PATHINFO_EXTENSION;
use finfo;

class FinfoMimeTypeDetector implements MimeTypeDetector
class FinfoMimeTypeDetector implements MimeTypeDetector, ExtensionLookup
{
private const INCONCLUSIVE_MIME_TYPES = ['application/x-empty', 'text/plain', 'text/x-asm'];
private const INCONCLUSIVE_MIME_TYPES = [
'application/x-empty',
'text/plain',
'text/x-asm',
'application/octet-stream',
'inode/x-empty',
];

/**
* @var finfo
Expand All @@ -28,14 +34,21 @@ class FinfoMimeTypeDetector implements MimeTypeDetector
*/
private $bufferSampleSize;

/**
* @var array<string>
*/
private $inconclusiveMimetypes;

public function __construct(
string $magicFile = '',
ExtensionToMimeTypeMap $extensionMap = null,
?int $bufferSampleSize = null
?int $bufferSampleSize = null,
array $inconclusiveMimetypes = self::INCONCLUSIVE_MIME_TYPES
) {
$this->finfo = new finfo(FILEINFO_MIME_TYPE, $magicFile);
$this->extensionMap = $extensionMap ?: new GeneratedExtensionToMimeTypeMap();
$this->bufferSampleSize = $bufferSampleSize;
$this->inconclusiveMimetypes = $inconclusiveMimetypes;
}

public function detectMimeType(string $path, $contents): ?string
Expand All @@ -44,7 +57,7 @@ public function detectMimeType(string $path, $contents): ?string
? (@$this->finfo->buffer($this->takeSample($contents)) ?: null)
: null;

if ($mimeType !== null && ! in_array($mimeType, self::INCONCLUSIVE_MIME_TYPES)) {
if ($mimeType !== null && ! in_array($mimeType, $this->inconclusiveMimetypes)) {
return $mimeType;
}

Expand Down Expand Up @@ -76,4 +89,18 @@ private function takeSample(string $contents): string

return (string) substr($contents, 0, $this->bufferSampleSize);
}

public function lookupExtension(string $mimetype): ?string
{
return $this->extensionMap instanceof ExtensionLookup
? $this->extensionMap->lookupExtension($mimetype)
: null;
}

public function lookupAllExtensions(string $mimetype): array
{
return $this->extensionMap instanceof ExtensionLookup
? $this->extensionMap->lookupAllExtensions($mimetype)
: [];
}
}
10 changes: 10 additions & 0 deletions src/FinfoMimeTypeDetectorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,16 @@ protected function setUp(): void
$this->detector = new FinfoMimeTypeDetector();
}

/**
* @test
*/
public function detecting_a_csv(): void
{
$mimeType = $this->detector->detectMimeType('something.csv', '');

$this->assertEquals('text/csv', $mimeType);
}

/**
* @test
*/
Expand Down
Loading
Loading