-
Notifications
You must be signed in to change notification settings - Fork 108
Allow the client to specify the image type using Accept header #982
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
Merged
Merged
Changes from 6 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
9c7598e
Create parser for Accept header
christianlupus 80a49f2
Only serve SVG if requested/accepted
christianlupus ed65b4b
Fix API documentation
christianlupus c3105ea
Update unit tests
christianlupus 30593f6
Update changelog
christianlupus efd53c1
Removed obsolete private method from new class
christianlupus 68a3171
Apply suggestions from code review
christianlupus File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,109 @@ | ||
| <?php | ||
|
|
||
| namespace OCA\Cookbook\Helper; | ||
|
|
||
| /** | ||
| * This class parses the Accepts header from a HTTP request and returns an array of accepted file extensions. | ||
| * | ||
| * The return values is a list of extensions that the client is willing to accept. | ||
|
christianlupus marked this conversation as resolved.
Outdated
|
||
| * Higher priorities are sorted first in the array. | ||
| */ | ||
| class AcceptHeaderParsingHelper { | ||
|
|
||
| /** | ||
| * Parse the content of a header and generate the list of valid file extensions the client will accept. | ||
| * | ||
| * The entries in the return value will be sorted according to the priority given by the sender. | ||
| * Higher priority entries are sorted first. | ||
| * | ||
| * @param string $header The value of the Accept header to be parsed | ||
| * @return array The sorted list of file extensions that are valid | ||
| */ | ||
| public function parseHeader(string $header): array { | ||
| $parts = explode(',', $header); | ||
| $parts = array_map(function ($x) { | ||
| return trim($x); | ||
| }, $parts); | ||
|
|
||
| // $this->sortParts($parts); | ||
| $weightedParts = $this->sortAndWeightParts($parts); | ||
|
|
||
| $extensions = []; | ||
|
|
||
| foreach ($weightedParts as $wp) { | ||
| $ex = $this->getFileTypes($wp['type']); | ||
|
|
||
| foreach ($ex as $e) { | ||
| if (array_search($e, $extensions) === false) { | ||
| $extensions[] = $e; | ||
| } | ||
| } | ||
| } | ||
| return $extensions; | ||
| } | ||
|
|
||
| /** | ||
| * Return the list of all supported file extensions by the app. | ||
| * The return value in the same format as with the parseHeader function | ||
| * | ||
| * @return array The list of supported file extensions by the app | ||
| */ | ||
| public function getDefaultExtensions(): array { | ||
| return ['jpg']; | ||
| } | ||
|
|
||
| private function sortAndWeightParts(array $parts): array { | ||
| $weightedParts = array_map(function ($x) { | ||
| return $this->parsePart($x); | ||
| }, $parts); | ||
|
|
||
| usort($weightedParts, function ($a, $b) { | ||
| $tmp = $a['weight'] - $b['weight']; | ||
| if ($tmp < - 0.001) { | ||
| return -1; | ||
| } elseif ($tmp > 0.001) { | ||
| return 1; | ||
| } else { | ||
| return 0; | ||
| } | ||
| }); | ||
| $weightedParts = array_reverse($weightedParts); | ||
|
|
||
| return $weightedParts; | ||
| } | ||
|
|
||
| private function parsePart($part): array { | ||
| if (preg_match('/\s*(.+?)\s*;q=([0-9.]+)\s*$/', $part, $matches) === 0) { | ||
| // No qualifier was found | ||
| $mime = trim($part); | ||
| $weight = 1; | ||
| } else { | ||
| // Separate qualifier and part | ||
| $mime = trim($matches[1]); | ||
| $weight = $matches[2]; | ||
| } | ||
|
|
||
| return [ | ||
| 'type' => $mime, | ||
| 'weight' => $weight, | ||
| ]; | ||
| } | ||
|
|
||
| private function getFileTypes(string $mime): array { | ||
| $parts = explode(';', $mime, 2); | ||
| switch ($parts[0]) { | ||
| case 'image/jpeg': | ||
| case 'image/jpg': | ||
| return ['jpg']; | ||
| case 'image/png': | ||
| return ['png']; | ||
| case 'image/svg+xml': | ||
| return ['svg']; | ||
| case 'image/*': | ||
| return ['jpg', 'png', 'svg']; | ||
| case '*/*': | ||
| return ['jpg', 'png', 'svg']; | ||
| } | ||
| return []; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| <?php | ||
|
|
||
| namespace OCA\Cookbook\tests\Unit\Helper; | ||
|
|
||
| use OCA\Cookbook\Helper\AcceptHeaderParsingHelper; | ||
| use PHPUnit\Framework\TestCase; | ||
|
|
||
| class AcceptHeaderParsingHelperTest extends TestCase { | ||
|
|
||
| /** | ||
| * @var AcceptHeaderParsingHelper | ||
| */ | ||
| private $dut; | ||
|
|
||
| protected function setUp(): void { | ||
| parent::setUp(); | ||
|
|
||
| $this->dut = new AcceptHeaderParsingHelper(); | ||
| } | ||
|
|
||
| public function testDefaultExtensions() { | ||
| $ret = $this->dut->getDefaultExtensions(); | ||
|
|
||
| $this->assertEquals(['jpg'], $ret); | ||
| } | ||
|
|
||
| public function dataProvider() { | ||
| yield ['image/jpeg', ['jpg']]; | ||
| yield ['image/jpg', ['jpg']]; | ||
| yield ['image/png', ['png']]; | ||
| yield ['image/svg+xml', ['svg']]; | ||
| yield ['image/*', ['jpg', 'png', 'svg']]; | ||
| yield ['*/*', ['jpg', 'png', 'svg']]; | ||
| yield ['image/jpeg, image/*', ['jpg', 'png', 'svg']]; | ||
| yield ['image/jpeg;q=0.9, image/*;q=0.5', ['jpg', 'png', 'svg']]; | ||
| yield ['image/webn, image/jpeg', ['jpg']]; | ||
| yield ['image/webn', []]; | ||
| yield ['image/png;q=0.5, image/jpg', ['jpg', 'png']]; | ||
| yield ['image/png;q=0.5, image/jpeg;q=0.3, image/svg+xml', ['svg', 'png', 'jpg']]; | ||
| yield ['image/png, image/jpeg;q=0.3, image/svg+xml;q=0.9', ['png', 'svg', 'jpg']]; | ||
| } | ||
|
|
||
| /** | ||
| * @dataProvider dataProvider | ||
| */ | ||
| public function testParseHeader($header, $expected) { | ||
| $this->assertEquals($expected, $this->dut->parseHeader($header)); | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.