Conditions | 4 |
Paths | 4 |
Total Lines | 24 |
Code Lines | 14 |
Lines | 0 |
Ratio | 0 % |
Tests | 0 |
CRAP Score | 20 |
Changes | 0 |
1 | <?php |
||
28 | public function handle(ServerRequestInterface $request): ResponseInterface |
||
29 | { |
||
30 | $id = $request->getAttribute('id'); |
||
31 | |||
32 | /** @var null|File $file */ |
||
33 | $file = $this->fileRepository->find($id); |
||
34 | if (!$file) { |
||
35 | return $this->createError("File $id not found in database"); |
||
36 | } |
||
37 | |||
38 | $path = $file->getPath(); |
||
39 | if (!is_readable($path)) { |
||
40 | return $this->createError("File for $id not found on disk, or not readable"); |
||
41 | } |
||
42 | |||
43 | $resource = fopen($path, 'rb'); |
||
44 | if ($resource === false) { |
||
45 | return $this->createError("Cannot open file for $id on disk"); |
||
46 | } |
||
47 | $size = filesize($path); |
||
48 | $type = mime_content_type($path); |
||
49 | $response = new Response($resource, 200, ['content-type' => $type, 'content-length' => $size]); |
||
50 | |||
51 | return $response; |
||
52 | } |
||
54 |