Completed
Push — master ( a29a95...dded20 )
by
unknown
03:05
created

MediaController::getCurrentRequest()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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