Passed
Push — master ( 2dc3a7...30a60e )
by Angel Fernando Quiroz
07:18
created

ThemeController::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 0
nc 1
nop 1
dl 0
loc 3
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 Chamilo\CoreBundle\ServiceHelper\ThemeHelper;
10
use League\Flysystem\FilesystemException;
11
use League\Flysystem\FilesystemOperator;
12
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
13
use Symfony\Component\DependencyInjection\Attribute\Autowire;
14
use Symfony\Component\HttpFoundation\Response;
15
use Symfony\Component\HttpFoundation\ResponseHeaderBag;
16
use Symfony\Component\HttpFoundation\StreamedResponse;
17
use Symfony\Component\Routing\Attribute\Route;
18
19
#[Route('/themes')]
20
class ThemeController extends AbstractController
21
{
22
    public function __construct(
23
        private readonly ThemeHelper $themeHelper
24
    ) {}
25
26
    /**
27
     * @throws FilesystemException
28
     */
29
    #[Route('/{name}/{path}', name: 'theme_asset', requirements: ['path' => '.+'])]
30
    public function index(
31
        string $name,
32
        string $path,
33
        #[Autowire(service: 'oneup_flysystem.themes_filesystem')]
34
        FilesystemOperator $filesystem
35
    ): Response {
36
        $themeDir = basename($name);
37
38
        if (!$filesystem->directoryExists($themeDir)) {
39
            throw $this->createNotFoundException('The folder name does not exist.');
40
        }
41
42
        $filePath = $this->themeHelper->getFileLocation($path);
43
44
        if (!$filePath) {
45
            throw $this->createNotFoundException('The requested file does not exist.');
46
        }
47
48
        $response = new StreamedResponse(function () use ($filesystem, $filePath): void {
49
            $outputStream = fopen('php://output', 'wb');
50
51
            $fileStream = $filesystem->readStream($filePath);
52
53
            stream_copy_to_stream($fileStream, $outputStream);
54
55
            fclose($outputStream);
56
            fclose($fileStream);
57
        });
58
59
        $mimeType = $filesystem->mimeType($filePath);
60
61
        $disposition = $response->headers->makeDisposition(ResponseHeaderBag::DISPOSITION_INLINE, basename($path));
62
63
        $response->headers->set('Content-Disposition', $disposition);
64
        $response->headers->set('Content-Type', $mimeType ?: 'application/octet-stream');
65
66
        return $response;
67
    }
68
}
69