Passed
Pull Request — master (#5614)
by Angel Fernando Quiroz
08:27
created

ThemeController::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 0
nc 1
nop 2
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
/* For licensing terms, see /license.txt */
4
5
declare(strict_types=1);
6
7
namespace Chamilo\CoreBundle\Controller;
8
9
use League\Flysystem\FilesystemException;
10
use League\Flysystem\FilesystemOperator;
11
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
12
use Symfony\Component\DependencyInjection\Attribute\Autowire;
13
use Symfony\Component\HttpFoundation\Response;
14
use Symfony\Component\HttpFoundation\ResponseHeaderBag;
15
use Symfony\Component\HttpFoundation\StreamedResponse;
16
use Symfony\Component\Routing\Attribute\Route;
17
18
#[Route('/themes')]
19
class ThemeController extends AbstractController
20
{
21
    /**
22
     * @throws FilesystemException
23
     */
24
    #[Route('/{name}/{path}', name: 'theme_asset', requirements: ['path' => '.+'])]
25
    public function index(
26
        string $name,
27
        string $path,
28
        #[Autowire(service: 'oneup_flysystem.themes_filesystem')] FilesystemOperator $filesystem
29
    ): Response {
30
        $themeDir = basename($name);
31
32
        if (!$filesystem->directoryExists($themeDir)) {
33
            throw $this->createNotFoundException("The folder name does not exist.");
34
        }
35
36
        $filePath = $themeDir.DIRECTORY_SEPARATOR.$path;
37
38
        if (!$filesystem->fileExists($filePath)) {
39
            throw $this->createNotFoundException("The requested file does not exist.");
40
        }
41
42
        $response = new StreamedResponse(function () use ($filesystem, $filePath) {
43
            $outputStream = fopen('php://output', 'wb');
44
45
            $fileStream = $filesystem->readStream($filePath);
46
47
            stream_copy_to_stream($fileStream, $outputStream);
48
        });
49
50
        $mimeType = $filesystem->mimeType($filePath);
51
52
        $disposition = $response->headers->makeDisposition(ResponseHeaderBag::DISPOSITION_INLINE, basename($path));
53
54
        $response->headers->set('Content-Disposition', $disposition);
55
        $response->headers->set('Content-Type', $mimeType ?: 'application/octet-stream');
56
57
        return $response;
58
    }
59
}
60