Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 24 |
|
0.00% |
0 / 1 |
CRAP | |
0.00% |
0 / 1 |
ImageToolsController | |
0.00% |
0 / 24 |
|
0.00% |
0 / 1 |
56 | |
0.00% |
0 / 1 |
thumb | |
0.00% |
0 / 24 |
|
0.00% |
0 / 1 |
56 |
1 | <?php |
2 | |
3 | namespace Olz\Controller; |
4 | |
5 | use Olz\Utils\EnvUtils; |
6 | use Olz\Utils\ImageUtils; |
7 | use Psr\Log\LoggerInterface; |
8 | use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; |
9 | use Symfony\Component\HttpFoundation\Request; |
10 | use Symfony\Component\HttpFoundation\Response; |
11 | use Symfony\Component\HttpKernel\Exception\BadRequestHttpException; |
12 | use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; |
13 | use Symfony\Component\Routing\Annotation\Route; |
14 | |
15 | class ImageToolsController extends AbstractController { |
16 | // This is a backup mechanism for the case where the thumbnail does not exist yet |
17 | #[Route('/img/{db_table}/{id}/thumb/{index}${dimension}.jpg', requirements: [ |
18 | 'db_table' => '[a-z_]+', |
19 | 'id' => '\d+', |
20 | 'dimension' => '\d+', |
21 | ])] |
22 | public function thumb( |
23 | Request $request, |
24 | LoggerInterface $log, |
25 | ImageUtils $imageUtils, |
26 | EnvUtils $envUtils, |
27 | string $db_table, |
28 | int $id, |
29 | string $index, |
30 | int $dimension, |
31 | ): Response { |
32 | $data_path = $envUtils->getDataPath(); |
33 | |
34 | session_write_close(); |
35 | if (!isset(ImageUtils::TABLES_IMG_DIRS[$db_table])) { |
36 | throw new NotFoundHttpException("No such DB table: {$db_table}"); |
37 | } |
38 | $db_imgpath = ImageUtils::TABLES_IMG_DIRS[$db_table]; |
39 | $entity_img_path = "{$data_path}{$db_imgpath}{$id}/"; |
40 | $imgfile = "{$entity_img_path}img/{$index}"; |
41 | if (!is_file($imgfile)) { |
42 | throw new NotFoundHttpException("No such image: {$imgfile}"); |
43 | } |
44 | $dim = $imageUtils->getThumbSize($dimension); |
45 | if ($dim < 32) { |
46 | $dim = 32; |
47 | } |
48 | $thumbfile = ''; |
49 | try { |
50 | $thumbfile = $imageUtils->getThumbFile($index, $entity_img_path, $dim); |
51 | } catch (\Throwable $th) { |
52 | throw new BadRequestHttpException($th->getMessage()); |
53 | } |
54 | $filemtime = @filemtime($thumbfile); |
55 | $one_second_ago = time() - 1; |
56 | if ($filemtime > $one_second_ago) { |
57 | $log->notice("Remaining thumb: {$thumbfile}"); |
58 | } |
59 | $response = new Response(file_get_contents($thumbfile) ?: null); |
60 | $response->headers->set('Cache-Control', 'max-age=2592000'); |
61 | $response->headers->set('Content-Type', 'image/jpeg'); |
62 | return $response; |
63 | } |
64 | } |