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

UploadController   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 23
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 9
c 1
b 0
f 0
dl 0
loc 23
rs 10
ccs 8
cts 8
cp 1
wmc 2

1 Method

Rating   Name   Duplication   Size   Complexity  
A serveUpload() 0 21 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