forked from sonata-project/SonataMediaBundle
/
MediaController.php
149 lines (124 loc) · 4.44 KB
/
MediaController.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
<?php
/*
* 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\Controller;
use Sonata\MediaBundle\Model\MediaInterface;
use Sonata\MediaBundle\Provider\MediaProviderInterface;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
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;
class MediaController extends Controller
{
/**
* @param MediaInterface $media
*
* @return MediaProviderInterface
*/
public function getProvider(MediaInterface $media)
{
return $this->get('sonata.media.pool')->getProvider($media->getProviderName());
}
/**
* @param string $id
*
* @return MediaInterface
*/
public function getMedia($id)
{
return $this->get('sonata.media.manager.media')->find($id);
}
/**
* @throws NotFoundHttpException
*
* @param Request $request
* @param string $id
* @param string $format
*
* @return Response
*/
public function downloadAction(Request $request, $id, $format = 'reference')
{
$media = $this->getMedia($id);
if (!$media) {
throw new NotFoundHttpException(sprintf('unable to find the media with the id : %s', $id));
}
if (!$this->get('sonata.media.pool')->getDownloadSecurity($media)->isGranted($media, $request)) {
throw new AccessDeniedException();
}
$response = $this->getProvider($media)->getDownloadResponse($media, $format, $this->get('sonata.media.pool')->getDownloadMode($media));
if ($response instanceof BinaryFileResponse) {
$response->prepare($request);
}
return $response;
}
/**
* @throws NotFoundHttpException
*
* @param Request $request
* @param string $id
* @param string $format
*
* @return Response
*/
public function viewAction(Request $request, $id, $format = 'reference')
{
$media = $this->getMedia($id);
if (!$media) {
throw new NotFoundHttpException(sprintf('unable to find the media with the id : %s', $id));
}
if (!$this->get('sonata.media.pool')->getDownloadSecurity($media)->isGranted($media, $request)) {
throw new AccessDeniedException();
}
return $this->render('SonataMediaBundle:Media:view.html.twig', array(
'media' => $media,
'formats' => $this->get('sonata.media.pool')->getFormatNamesByContext($media->getContext()),
'format' => $format,
));
}
/**
* This action applies a given filter to a given image,
* optionally saves the image and
* outputs it to the browser at the same time.
*
* @param Request $request
* @param string $path
* @param string $filter
*
* @return Response
*/
public function liipImagineFilterAction(Request $request, $path, $filter)
{
if (!preg_match('@([^/]*)/(.*)/([0-9]*)_([a-z_A-Z]*).jpg@', $path, $matches)) {
throw new NotFoundHttpException();
}
$targetPath = $this->get('liip_imagine.cache.manager')->resolve($request, $path, $filter);
if ($targetPath instanceof Response) {
return $targetPath;
}
// get the file
$media = $this->getMedia($matches[3]);
if (!$media) {
throw new NotFoundHttpException();
}
$provider = $this->getProvider($media);
$file = $provider->getReferenceFile($media);
// load the file content from the abstracted file system
$tmpFile = sprintf('%s.%s', tempnam(sys_get_temp_dir(), 'sonata_media_liip_imagine'), $media->getExtension());
file_put_contents($tmpFile, $file->getContent());
$image = $this->get('liip_imagine')->open($tmpFile);
$response = $this->get('liip_imagine.filter.manager')->get($request, $filter, $image, $path);
if ($targetPath) {
$response = $this->get('liip_imagine.cache.manager')->store($response, $targetPath, $filter);
}
return $response;
}
}