-
Notifications
You must be signed in to change notification settings - Fork 19
IBX-12042: Deprecated filename-less download route in favor of filename-validated variant #775
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
Open
wiewiurdp
wants to merge
2
commits into
5.0
Choose a base branch
from
IBX-12042-Deprecate-filename-less-download-route-content-download-contentId-fieldId-and-replace-it-with-a-filename-validated-variant
base: 5.0
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+150
−8
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -49,8 +49,23 @@ public function __construct( | |
| * @throws \Ibexa\Contracts\Core\Repository\Exceptions\NotFoundException If the content is trashed, or can't be found. | ||
| * @throws \Ibexa\Contracts\Core\Repository\Exceptions\UnauthorizedException If the user has no access to read content and in case of un-published content: read versions. | ||
| */ | ||
| public function downloadBinaryFileByIdAction(Request $request, int $contentId, int $fieldId): BinaryStreamResponse | ||
| { | ||
| public function downloadBinaryFileByIdAction( | ||
| Request $request, | ||
| int $contentId, | ||
| int $fieldId, | ||
| ?string $filename = null | ||
| ): BinaryStreamResponse { | ||
| if ($filename === null) { | ||
| trigger_deprecation( | ||
| 'ibexa/core', | ||
| '5.0', | ||
| 'The "ibexa.content.download.field_id" route (/content/download/{contentId}/{fieldId}) is deprecated' | ||
| . ' and will be removed in 6.0.' | ||
| . ' Use the "ibexa.content.download.field_id.filename" route' | ||
| . ' (/content/download/{contentId}/{fieldId}/{filename}) instead.' | ||
| ); | ||
| } | ||
|
|
||
| $versionNo = $request->query->has('version') ? $request->query->getInt('version') : null; | ||
| $language = $request->query->has('inLanguage') ? $request->query->get('inLanguage') : null; | ||
|
|
||
|
|
@@ -65,6 +80,10 @@ public function downloadBinaryFileByIdAction(Request $request, int $contentId, i | |
| throw new NotFoundException('File', $fieldId); | ||
| } | ||
|
|
||
| if ($filename !== null && $field->value->fileName !== $filename) { | ||
| throw new NotFoundException('File', $filename); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is it worth to add test for that exception? |
||
| } | ||
|
|
||
| return $this->downloadBinaryFileAction($contentId, $field->fieldDefIdentifier, $field->value->fileName, $request); | ||
| } | ||
|
|
||
|
|
||
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 |
|---|---|---|
|
|
@@ -14,11 +14,9 @@ | |
|
|
||
| class ContentDownloadUrlGenerator implements RouteAwarePathGenerator | ||
| { | ||
| /** @var \Symfony\Component\Routing\RouterInterface */ | ||
| private $router; | ||
| private const string ROUTE = 'ibexa.content.download.field_id.filename'; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we need that const if use only in one place in this class? |
||
|
|
||
| /** @var string */ | ||
| private $route = 'ibexa.content.download.field_id'; | ||
| private RouterInterface $router; | ||
|
|
||
| public function __construct(RouterInterface $router) | ||
| { | ||
|
|
@@ -27,7 +25,10 @@ public function __construct(RouterInterface $router) | |
|
|
||
| public function getStoragePathForField(Field $field, VersionInfo $versionInfo): string | ||
| { | ||
| return $this->generate($this->route, $this->getParameters($field, $versionInfo)); | ||
| return $this->generate( | ||
| $this->getRoute($field, $versionInfo), | ||
| $this->getParameters($field, $versionInfo) | ||
| ); | ||
| } | ||
|
|
||
| public function generate(string $route, ?array $parameters = []): string | ||
|
|
@@ -37,7 +38,7 @@ public function generate(string $route, ?array $parameters = []): string | |
|
|
||
| public function getRoute(Field $field, VersionInfo $versionInfo): string | ||
| { | ||
| return $this->route; | ||
| return self::ROUTE; | ||
| } | ||
|
|
||
| public function getParameters(Field $field, VersionInfo $versionInfo): array | ||
|
|
@@ -46,6 +47,7 @@ public function getParameters(Field $field, VersionInfo $versionInfo): array | |
| 'contentId' => $versionInfo->contentInfo->id, | ||
| 'fieldId' => $field->id, | ||
| 'version' => $versionInfo->versionNo, | ||
| 'filename' => $field->value->externalData['fileName'] ?? '', | ||
| ]; | ||
| } | ||
| } | ||
103 changes: 103 additions & 0 deletions
103
tests/lib/MVC/Symfony/FieldType/BinaryBase/ContentDownloadUrlGeneratorTest.php
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,103 @@ | ||
| <?php | ||
|
|
||
| /** | ||
| * @copyright Copyright (C) Ibexa AS. All rights reserved. | ||
| * @license For full copyright and license information view LICENSE file distributed with this source code. | ||
| */ | ||
| declare(strict_types=1); | ||
|
|
||
| namespace Ibexa\Tests\Core\MVC\Symfony\FieldType\BinaryBase; | ||
|
|
||
| use Ibexa\Contracts\Core\Persistence\Content\ContentInfo; | ||
| use Ibexa\Contracts\Core\Persistence\Content\Field; | ||
| use Ibexa\Contracts\Core\Persistence\Content\FieldValue; | ||
| use Ibexa\Contracts\Core\Persistence\Content\VersionInfo; | ||
| use Ibexa\Core\MVC\Symfony\FieldType\BinaryBase\ContentDownloadUrlGenerator; | ||
| use PHPUnit\Framework\MockObject\MockObject; | ||
| use PHPUnit\Framework\TestCase; | ||
| use Symfony\Component\Routing\RouterInterface; | ||
|
|
||
| /** | ||
| * @covers \Ibexa\Core\MVC\Symfony\FieldType\BinaryBase\ContentDownloadUrlGenerator | ||
| */ | ||
| final class ContentDownloadUrlGeneratorTest extends TestCase | ||
| { | ||
| private const string ROUTE = 'ibexa.content.download.field_id.filename'; | ||
|
|
||
| private RouterInterface & MockObject $router; | ||
|
|
||
| private ContentDownloadUrlGenerator $generator; | ||
|
|
||
| protected function setUp(): void | ||
| { | ||
| parent::setUp(); | ||
|
|
||
| $this->router = $this->createMock(RouterInterface::class); | ||
| $this->generator = new ContentDownloadUrlGenerator($this->router); | ||
| } | ||
|
|
||
| public function testGetRouteReturnsFilenameAwareRoute(): void | ||
| { | ||
| self::assertSame( | ||
| self::ROUTE, | ||
| $this->generator->getRoute($this->createField(), $this->createVersionInfo()) | ||
| ); | ||
| } | ||
|
|
||
| public function testGetParametersContainFilename(): void | ||
| { | ||
| self::assertSame( | ||
| [ | ||
| 'contentId' => 42, | ||
| 'fieldId' => 7, | ||
| 'version' => 2, | ||
| 'filename' => 'Test-file.pdf', | ||
| ], | ||
| $this->generator->getParameters($this->createField(), $this->createVersionInfo()) | ||
| ); | ||
| } | ||
|
|
||
| public function testGetStoragePathForFieldGeneratesFilenameAwareUrl(): void | ||
| { | ||
| $this->router | ||
| ->expects(self::once()) | ||
| ->method('generate') | ||
| ->with( | ||
| self::ROUTE, | ||
| [ | ||
| 'contentId' => 42, | ||
| 'fieldId' => 7, | ||
| 'version' => 2, | ||
| 'filename' => 'Test-file.pdf', | ||
| ] | ||
| ) | ||
| ->willReturn('/content/download/42/7/Test-file.pdf?version=2'); | ||
|
|
||
| self::assertSame( | ||
| '/content/download/42/7/Test-file.pdf?version=2', | ||
| $this->generator->getStoragePathForField($this->createField(), $this->createVersionInfo()) | ||
| ); | ||
| } | ||
|
|
||
| private function createField(): Field | ||
| { | ||
| return new Field([ | ||
| 'id' => 7, | ||
| 'value' => new FieldValue([ | ||
| 'externalData' => [ | ||
| 'fileName' => 'Test-file.pdf', | ||
| ], | ||
| ]), | ||
| ]); | ||
| } | ||
|
|
||
| private function createVersionInfo(): VersionInfo | ||
| { | ||
| return new VersionInfo([ | ||
| 'versionNo' => 2, | ||
| 'contentInfo' => new ContentInfo([ | ||
| 'id' => 42, | ||
| ]), | ||
| ]); | ||
| } | ||
| } |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We could add requirement for filename as well here and use
https://github.com/symfony/routing/blob/7.4/Requirement/Requirement.phpas their value source, eg. replace\d+withRequirement::DIGITSThe same for missing requirements in
ibexa.content.download. This way, I think we'll be secure about route ordering here as their expected argument types won't conflict with each other