1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace MediaMonks\SonataMediaBundle\Controller; |
4
|
|
|
|
5
|
|
|
use MediaMonks\SonataMediaBundle\ParameterBag\DownloadParameterBag; |
6
|
|
|
use MediaMonks\SonataMediaBundle\ParameterBag\ImageParameterBag; |
7
|
|
|
use MediaMonks\SonataMediaBundle\Model\MediaInterface; |
8
|
|
|
use MediaMonks\SonataMediaBundle\Utility\DownloadUtility; |
9
|
|
|
use MediaMonks\SonataMediaBundle\Utility\ImageUtility; |
10
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\Controller; |
11
|
|
|
use Symfony\Component\HttpFoundation\RedirectResponse; |
12
|
|
|
use Symfony\Component\HttpFoundation\Request; |
13
|
|
|
use Symfony\Component\HttpFoundation\StreamedResponse; |
14
|
|
|
|
15
|
|
|
class MediaController extends Controller |
16
|
|
|
{ |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @param Request $request |
20
|
|
|
* @param int $id |
21
|
|
|
* @param int $width |
22
|
|
|
* @param int $height |
23
|
|
|
* |
24
|
|
|
* @return RedirectResponse |
25
|
|
|
*/ |
26
|
4 |
|
public function imageRedirectAction( |
27
|
|
|
Request $request, |
28
|
|
|
int $id, |
29
|
|
|
int $width, |
30
|
|
|
int $height |
31
|
|
|
): RedirectResponse { |
32
|
4 |
|
return $this->get(ImageUtility::class)->getRedirectResponse( |
33
|
4 |
|
$this->getMediaById($id), |
34
|
4 |
|
new ImageParameterBag($width, $height, $request->query->all()) |
35
|
|
|
); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @param Request $request |
40
|
|
|
* @param int $id |
41
|
|
|
* |
42
|
|
|
* @return StreamedResponse |
43
|
|
|
*/ |
44
|
1 |
|
public function downloadAction(Request $request, int $id): StreamedResponse |
45
|
|
|
{ |
46
|
1 |
|
return $this->get(DownloadUtility::class)->getStreamedResponse( |
47
|
1 |
|
$this->getMediaById($id), |
48
|
1 |
|
new DownloadParameterBag($request->query->all()) |
49
|
|
|
); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @param int $id |
54
|
|
|
* |
55
|
|
|
* @return MediaInterface |
56
|
|
|
*/ |
57
|
5 |
|
protected function getMediaById(int $id): MediaInterface |
58
|
|
|
{ |
59
|
5 |
|
return $this->getDoctrine()->getManager()->find( |
60
|
5 |
|
$this->getParameter('mediamonks.sonata_media.entity.class'), |
61
|
5 |
|
$id |
62
|
|
|
); |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|