Passed
Push — main ( 06b8a6...5a482c )
by Emil
05:26
created

UploadController::serveUpload()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 21
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 8
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 21
rs 10
ccs 8
cts 8
cp 1
crap 2
1
<?php
2
3
namespace App\Controller;
4
5
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
6
use Symfony\Component\HttpFoundation\Response;
7
use Symfony\Component\Routing\Annotation\Route;
8
9
class UploadController extends AbstractController
10
{
11 2
    #[Route('/uploads/{filename}', name: 'file_uploads')]
12
    public function serveUpload(string $filename): Response
13
    {
14
        /** @var string $projectDir */
15 2
        $projectDir = $this->getParameter('kernel.project_dir');
16
17
        // Define the path to your uploads directory inside var
18 2
        $uploadsDir = $projectDir.'/var/uploads';
19
20
        // Sanitize filename to prevent directory traversal
21 2
        $safeFilename = basename($filename);
22
23
        // Complete path to the file
24 2
        $filePath = $uploadsDir.'/'.$safeFilename;
25
26 2
        if (!file_exists($filePath)) {
27 1
            throw $this->createNotFoundException('File not found');
28
        }
29
30
        // Serve the file
31 1
        return $this->file($filePath);
32
    }
33
}
34