| Conditions | 2 |
| Paths | 2 |
| Total Lines | 21 |
| Code Lines | 8 |
| Lines | 0 |
| Ratio | 0 % |
| Tests | 8 |
| CRAP Score | 2 |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
| 1 | <?php |
||
| 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 | } |
||
| 34 |