Passed
Push — master ( 4e7d2f...7a7af2 )
by Julito
08:07
created

AssetController::showFile()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 21
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 13
c 1
b 0
f 0
nc 2
nop 3
dl 0
loc 21
rs 9.8333
1
<?php
2
3
/* For licensing terms, see /license.txt */
4
5
namespace Chamilo\CoreBundle\Controller;
6
7
use Chamilo\CoreBundle\Repository\AssetRepository;
8
use Chamilo\CoreBundle\Traits\ControllerTrait;
9
use Chamilo\CoreBundle\Traits\CourseControllerTrait;
10
use Chamilo\CoreBundle\Traits\ResourceControllerTrait;
11
use Symfony\Component\HttpFoundation\ResponseHeaderBag;
12
use Symfony\Component\HttpFoundation\StreamedResponse;
13
use Symfony\Component\Routing\Annotation\Route;
14
15
/**
16
 * @Route("/assets")
17
 */
18
class AssetController
19
{
20
    use CourseControllerTrait;
21
    use ResourceControllerTrait;
0 ignored issues
show
Bug introduced by
The trait Chamilo\CoreBundle\Traits\ResourceControllerTrait requires the property $container which is not provided by Chamilo\CoreBundle\Controller\AssetController.
Loading history...
22
    use ControllerTrait;
0 ignored issues
show
Bug introduced by
The trait Chamilo\CoreBundle\Traits\ControllerTrait requires the property $container which is not provided by Chamilo\CoreBundle\Controller\AssetController.
Loading history...
23
24
    /**
25
     *  @Route("/{category}/{path}", methods={"GET"}, requirements={"path"=".+"})
26
     */
27
    public function showFile($category, $path, AssetRepository $assetRepository)
28
    {
29
        $filePath = $category.'/'.$path;
30
        $has = $assetRepository->getFileSystem()->has($filePath);
31
        if ($has) {
32
            $fileName = basename($filePath);
33
            $stream = $assetRepository->getFileSystem()->readStream($filePath);
34
35
            $response = new StreamedResponse(
36
                function () use ($stream): void {
37
                    stream_copy_to_stream($stream, fopen('php://output', 'wb'));
38
                }
39
            );
40
            $disposition = $response->headers->makeDisposition(
41
                ResponseHeaderBag::DISPOSITION_INLINE,
42
                $fileName
43
            );
44
            $response->headers->set('Content-Disposition', $disposition);
45
            //$response->headers->set('Content-Type', $mimeType ?: 'application/octet-stream');
46
47
            return $response;
48
        }
49
    }
50
}
51