Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f84f103
commit 12d483d
Showing
7 changed files
with
264 additions
and
91 deletions.
There are no files selected for viewing
This file contains 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,73 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of the Sonata Project package. | ||
* | ||
* (c) Thomas Rabaix <thomas.rabaix@sonata-project.org> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Sonata\MediaBundle\Action; | ||
|
||
use Sonata\MediaBundle\Model\MediaManagerInterface; | ||
use Sonata\MediaBundle\Provider\MediaProviderInterface; | ||
use Sonata\MediaBundle\Provider\Pool; | ||
use Symfony\Component\HttpFoundation\BinaryFileResponse; | ||
use Symfony\Component\HttpFoundation\Request; | ||
use Symfony\Component\HttpFoundation\Response; | ||
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; | ||
use Symfony\Component\Security\Core\Exception\AccessDeniedException; | ||
|
||
final class MediaDownloadAction | ||
{ | ||
/** | ||
* @var MediaManagerInterface | ||
*/ | ||
private $mediaManager; | ||
|
||
/** | ||
* @var Pool | ||
*/ | ||
private $pool; | ||
|
||
public function __construct(MediaManagerInterface $mediaManager, Pool $pool) | ||
{ | ||
$this->mediaManager = $mediaManager; | ||
$this->pool = $pool; | ||
} | ||
|
||
/** | ||
* @param int|string $id | ||
* | ||
* @throws NotFoundHttpException | ||
* @throws AccessDeniedException | ||
*/ | ||
public function __invoke(Request $request, $id, string $format = MediaProviderInterface::FORMAT_REFERENCE): Response | ||
{ | ||
$media = $this->mediaManager->find($id); | ||
|
||
if (null === $media) { | ||
throw new NotFoundHttpException(sprintf('unable to find the media with the id : %s', $id)); | ||
} | ||
|
||
if (!$this->pool->getDownloadStrategy($media)->isGranted($media, $request)) { | ||
throw new AccessDeniedException(); | ||
} | ||
|
||
$response = $this->pool->getProvider($media->getProviderName())->getDownloadResponse( | ||
$media, | ||
$format, | ||
$this->pool->getDownloadMode($media) | ||
); | ||
|
||
if ($response instanceof BinaryFileResponse) { | ||
$response->prepare($request); | ||
} | ||
|
||
return $response; | ||
} | ||
} |
This file contains 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 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 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,9 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<container xmlns="http://symfony.com/schema/dic/services" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd"> | ||
<services> | ||
<service id="sonata.media.action.media_download" class="Sonata\MediaBundle\Action\MediaDownloadAction" public="true"> | ||
<argument type="service" id="sonata.media.manager.media"/> | ||
<argument type="service" id="sonata.media.pool"/> | ||
</service> | ||
</services> | ||
</container> |
This file contains 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 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,112 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of the Sonata Project package. | ||
* | ||
* (c) Thomas Rabaix <thomas.rabaix@sonata-project.org> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Sonata\MediaBundle\Tests\Controller; | ||
|
||
use PHPUnit\Framework\MockObject\MockObject; | ||
use PHPUnit\Framework\TestCase; | ||
use Sonata\MediaBundle\Action\MediaDownloadAction; | ||
use Sonata\MediaBundle\Model\MediaManagerInterface; | ||
use Sonata\MediaBundle\Provider\MediaProviderInterface; | ||
use Sonata\MediaBundle\Provider\Pool; | ||
use Sonata\MediaBundle\Security\DownloadStrategyInterface; | ||
use Sonata\MediaBundle\Tests\App\Entity\Media; | ||
use Symfony\Component\HttpFoundation\BinaryFileResponse; | ||
use Symfony\Component\HttpFoundation\Request; | ||
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; | ||
use Symfony\Component\Security\Core\Exception\AccessDeniedException; | ||
|
||
final class MediaDownloadActionTest extends TestCase | ||
{ | ||
/** | ||
* @var MockObject&MediaManagerInterface | ||
*/ | ||
private $mediaManager; | ||
|
||
/** | ||
* @var Pool | ||
*/ | ||
private $pool; | ||
|
||
/** | ||
* @var MediaDownloadAction | ||
*/ | ||
private $mediaDownloadAction; | ||
|
||
protected function setUp(): void | ||
{ | ||
$this->mediaManager = $this->createMock(MediaManagerInterface::class); | ||
$this->pool = new Pool('default_context'); | ||
|
||
$this->mediaDownloadAction = new MediaDownloadAction($this->mediaManager, $this->pool); | ||
} | ||
|
||
public function testDownloadActionWithNotFoundMedia(): void | ||
{ | ||
$this->expectException(NotFoundHttpException::class); | ||
|
||
$this->mediaDownloadAction->__invoke(new Request(), 1); | ||
} | ||
|
||
public function testDownloadActionAccessDenied(): void | ||
{ | ||
$this->expectException(AccessDeniedException::class); | ||
|
||
$request = new Request(); | ||
$media = new Media(); | ||
$media->setContext('default_context'); | ||
|
||
$this->configureDownloadSecurity($media, $request, false); | ||
$this->mediaManager->method('find')->with(1)->willReturn($media); | ||
|
||
$this->mediaDownloadAction->__invoke($request, 1); | ||
} | ||
|
||
public function testDownloadActionBinaryFile(): void | ||
{ | ||
$media = new Media(); | ||
$media->setContext('default_context'); | ||
$media->setProviderName('provider'); | ||
|
||
$request = new Request(); | ||
|
||
$provider = $this->createMock(MediaProviderInterface::class); | ||
$response = $this->createMock(BinaryFileResponse::class); | ||
|
||
$this->configureDownloadSecurity($media, $request, true); | ||
|
||
$this->pool->addProvider('provider', $provider); | ||
$this->mediaManager->method('find')->with(1)->willReturn($media); | ||
$provider->method('getDownloadResponse')->with($media, 'format', 'mode')->willReturn($response); | ||
$response->expects(static::once())->method('prepare')->with($request); | ||
|
||
$result = $this->mediaDownloadAction->__invoke($request, 1, 'format'); | ||
|
||
static::assertSame($response, $result); | ||
} | ||
|
||
private function configureDownloadSecurity( | ||
Media $media, | ||
Request $request, | ||
bool $isGranted | ||
): void { | ||
$strategy = $this->createMock(DownloadStrategyInterface::class); | ||
$strategy->method('isGranted')->with($media, $request)->willReturn($isGranted); | ||
|
||
$this->pool->addContext('default_context', [], [], [ | ||
'mode' => 'mode', | ||
'strategy' => 'download_strategy', | ||
]); | ||
$this->pool->addDownloadStrategy('download_strategy', $strategy); | ||
} | ||
} |
Oops, something went wrong.