Completed
Push — master ( 9278e5...afd0c3 )
by IT
04:14
created

VideoController::downloadAction()   C

Complexity

Conditions 7
Paths 8

Size

Total Lines 24
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 17
nc 8
nop 2
dl 0
loc 24
rs 6.7272
c 0
b 0
f 0
1
<?php
2
3
namespace AppBundle\Controller;
4
5
use AppBundle\Entity\Video;
6
use AppBundle\Services\XsendFileResponseManager;
0 ignored issues
show
Bug introduced by
The type AppBundle\Services\XsendFileResponseManager was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
7
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
8
use Symfony\Component\HttpFoundation\Request;
9
use Symfony\Component\DependencyInjection\Exception\ParameterNotFoundException;
10
use Symfony\Component\HttpFoundation\BinaryFileResponse;
11
use Symfony\Component\HttpFoundation\ResponseHeaderBag;
12
13
14
/**
15
 * Video controller.
16
 */
17
class VideoController extends Controller
18
{
19
    use ControllerUtilsTrait;
20
21
    /**
22
     * Lists all video entities.
23
     */
24
    public function indexAction()
25
    {
26
        $em = $this->getDoctrine()->getManager();
27
28
        $videos = $em->getRepository('AppBundle:Video')->findLatest();
29
30
        return $this->render('video/index.html.twig', array(
31
            'videos' => $videos,
32
        ));
33
    }
34
35
    /**
36
     * Creates a new video entity.
37
     */
38
    public function newAction(Request $request)
39
    {
40
        $video = new Video();
41
        $video->setCreator($this->getUser());
42
        $form = $this->createForm('AppBundle\Form\VideoType', $video, ['action_type' => 'create']);
43
        $form->handleRequest($request);
44
45
        if ($form->isSubmitted() && $form->isValid()) {
46
            $em = $this->getDoctrine()->getManager();
47
            $em->persist($video);
48
            $em->flush();
49
            $this->flashMessage(ControllerUtilsTrait::$flashSuccess);
50
51
            return $this->redirectToRoute('video_upload', array('id' => $video->getId()));
52
        }
53
54
        return $this->render('video/edit.html.twig', array(
55
            'title' => $this->get('translator')->trans('form_create.create', [], 'video'),
56
            'video' => $video,
57
            'form' => $form->createView(),
58
        ));
59
    }
60
61
    public function uploadVideoAction(Request $request, Video $video)
62
    {
63
        $form = $this->createForm('AppBundle\Form\VideoFileType', $video);
64
        $form->handleRequest($request);
65
66
        if ($form->isSubmitted() && $form->isValid()) {
67
            $em = $this->getDoctrine()->getManager();
68
            $video->setUpdatedAt(new \DateTime());
69
            $em->persist($video);
70
            $em->flush();
71
            $this->flashMessage(ControllerUtilsTrait::$flashSuccess);
72
73
            return $this->redirectToRoute('video_show', array('id' => $video->getId()));
74
        }
75
        return $this->render('video/upload.html.twig', array(
76
            'title' => $this->get('translator')->trans('form_create.create', [], 'video'),
77
            'video' => $video,
78
            'form' => $form->createView(),
79
        ));
80
    }
81
82
    /**
83
     * Finds and displays a video entity.
84
     */
85
    public function showAction(Video $video)
86
    {
87
        $deleteForm = $this->createDeleteForm($video);
88
89
        return $this->render('video/show.html.twig', array(
90
            'video' => $video,
91
            'delete_form' => $deleteForm->createView(),
92
        ));
93
    }
94
95
    /**
96
     * Displays a form to edit an existing video entity.
97
     */
98
    public function editAction(Request $request, Video $video)
99
    {
100
        $editForm = $this->createForm('AppBundle\Form\VideoType', $video, ['action_type' => 'edit']);
101
        $editForm->handleRequest($request);
102
103
        if ($editForm->isSubmitted() && $editForm->isValid()) {
104
            $video->setUpdatedAt(new \DateTime());
105
            $em = $this->getDoctrine()->getManager();
106
            $em->persist($video);
107
            $em->flush();
108
            $this->flashMessage(ControllerUtilsTrait::$flashSuccess);
109
            return $this->redirectToRoute('video_show', ['id' => $video->getId()]);
110
        }
111
112
        return $this->render('video/edit.html.twig', array(
113
            'title' => $this->get('translator')->trans('form_create.edit', [], 'video'),
114
            'video' => $video,
115
            'form' => $editForm->createView(),
116
        ));
117
    }
118
119
    public function searchAction(Request $request)
120
    {
121
        $form = $this->createForm('AppBundle\Form\VideoSearchType');
122
        $form->handleRequest($request);
123
        $videos = [];
124
        if ($form->isSubmitted() && $form->isValid()) {
125
            $criteria = $form->getData();
126
            $videos = $this->getDoctrine()->getRepository(Video::class)->findVideo($criteria);
127
        }
128
129
        return $this->render('video/search.twig', array(
130
            'form' => $form->createView(),
131
            'submitted' => $form->isSubmitted(),
132
            'videos' => $videos,
133
        ));
134
    }
135
136
    /**
137
     * Deletes a video entity.
138
     */
139
    public function deleteAction(Request $request, Video $video)
140
    {
141
        $form = $this->createDeleteForm($video);
142
        $form->handleRequest($request);
143
144
        if ($form->isSubmitted() && $form->isValid()) {
145
            $em = $this->getDoctrine()->getManager();
146
            $em->remove($video);
147
            $em->flush();
148
            $this->flashMessage(ControllerUtilsTrait::$flashSuccess);
149
150
            return $this->redirectToRoute('video_index');
151
        }
152
153
        return $this->render('video/delete.twig', array(
154
            'video' => $video,
155
            'form' => $form->createView(),
156
        ));
157
    }
158
159
    /**
160
     * Creates a form to delete a video entity.
161
     *
162
     * @param Video $video The video entity
163
     *
164
     * @return \Symfony\Component\Form\Form The form
165
     */
166
    private function createDeleteForm(Video $video)
167
    {
168
        return $this->createFormBuilder()
169
            ->setAction($this->generateUrl('video_delete', array('id' => $video->getId())))
170
            ->getForm();
171
    }
172
173
    /**
174
     * @see https://symfony.com/doc/current/components/http_foundation.html#serving-files
175
     * @see apache mod_xsendfile: https://tn123.org/mod_xsendfile/
176
     * @see nginx X-accel: https://www.nginx.com/resources/wiki/start/topics/examples/x-accel/
177
     * @param Request $request
178
     * @param Video $video
179
     * @return BinaryFileResponse
180
     * @throws \Exception
181
     */
182
    public function downloadAction(Request $request, Video $video)
183
    {
184
        $videoPath = $this->get('vich_uploader.storage')->resolvePath($video, 'videoFile');
185
        $response = new BinaryFileResponse($videoPath);
186
        if (filter_var(getenv('USE_X_SENDFILE_MODE'), FILTER_VALIDATE_BOOLEAN)) {
187
            BinaryFileResponse::trustXSendfileTypeHeader();
188
            $serverSoftware = $request->server->get('SERVER_SOFTWARE');
189
            //determine header according to server software to serve file faster directly by server instead of using php
190
            if (preg_match('/nginx/', $serverSoftware)) {
191
                if (!$nginxLocationXSendFile = getenv('NGINX_LOCATION_X_SEND_FILE')) {
192
                    throw new ParameterNotFoundException('nginx_location_x_send_file');
193
                }
194
                // slash management in lginx stream location
195
                $nginxLocationXSendFile = substr($nginxLocationXSendFile, -1) === '/' ? $nginxLocationXSendFile : $nginxLocationXSendFile . '/';
196
                $nginxLocationXSendFile = substr($nginxLocationXSendFile, 0, 1) === '/' ? $nginxLocationXSendFile : '/' . $nginxLocationXSendFile;
197
                $response->headers->set('X-Accel-Redirect', $nginxLocationXSendFile . '/' . basename(pathinfo($videoPath)['dirname']) . '/' . pathinfo($videoPath)['basename']);
198
            } elseif (preg_match('/apache/', $serverSoftware)) {
199
                $response->headers->set('X-Sendfile', $videoPath);
200
            } else {
201
                throw  new \Exception(sprintf('server "%s" not supported', $serverSoftware));
202
            }
203
            $response->setContentDisposition(ResponseHeaderBag::DISPOSITION_INLINE, basename($videoPath));
204
        }
205
        return $response;
206
    }
207
}
208