|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Prokl\SymfonyMiddlewareBundle\ControllersMiddleware; |
| 4 | + |
| 5 | +use Prokl\SymfonyMiddlewareBundle\ControllersMiddleware\Utils\ContentTypeMapping; |
| 6 | +use Prokl\SymfonyMiddlewareBundle\MiddlewareInterface; |
| 7 | +use LogicException; |
| 8 | +use Symfony\Component\HttpFoundation\Request; |
| 9 | +use Symfony\Component\HttpFoundation\Response; |
| 10 | +use Symfony\Component\HttpFoundation\StreamedResponse; |
| 11 | + |
| 12 | +/** |
| 13 | + * Class StaticFileMiddleware |
| 14 | + * @package Local\Services\ControllerMiddleware |
| 15 | + * |
| 16 | + * @since 23.02.2021 |
| 17 | + */ |
| 18 | +class StaticFileMiddleware implements MiddlewareInterface |
| 19 | +{ |
| 20 | + /** |
| 21 | + * @var string $publicDirectory Директория, где лежат файлы. |
| 22 | + */ |
| 23 | + private $publicDirectory; |
| 24 | + |
| 25 | + /** |
| 26 | + * @var string $hashAlgorithm Алгоритм хэширования. |
| 27 | + */ |
| 28 | + private $hashAlgorithm; |
| 29 | + |
| 30 | + /** |
| 31 | + * StaticFileMiddleware constructor. |
| 32 | + * |
| 33 | + * @param string $publicDirectory Директория, где лежат файлы. |
| 34 | + * @param string $hashAlgorithm Алгоритм хэширования. |
| 35 | + */ |
| 36 | + public function __construct( |
| 37 | + string $publicDirectory = '', |
| 38 | + string $hashAlgorithm = 'md5' |
| 39 | + ) { |
| 40 | + $this->publicDirectory = $publicDirectory; |
| 41 | + $this->hashAlgorithm = $hashAlgorithm; |
| 42 | + } |
| 43 | + |
| 44 | + /** |
| 45 | + * @inheritDoc |
| 46 | + */ |
| 47 | + public function handle(Request $request): ?Response |
| 48 | + { |
| 49 | + if (!in_array($this->hashAlgorithm, hash_algos(), true)) { |
| 50 | + throw new LogicException(sprintf('Invalid or not supported hash algorithm: "%s"', $this->hashAlgorithm)); |
| 51 | + } |
| 52 | + |
| 53 | + if ($this->publicDirectory === '') { |
| 54 | + throw new LogicException( |
| 55 | + sprintf('Public upload directory not configured. You forget initialize StaticFileMiddleware as service?') |
| 56 | + ); |
| 57 | + } |
| 58 | + |
| 59 | + $target = $this->getRequestTarget($request); |
| 60 | + if ($target === '') { |
| 61 | + return null; |
| 62 | + } |
| 63 | + |
| 64 | + $filename = $this->publicDirectory . $target; |
| 65 | + |
| 66 | + $hash = hash_file($this->hashAlgorithm, $filename); |
| 67 | + |
| 68 | + if (!is_readable($filename)) { |
| 69 | + return null; |
| 70 | + } |
| 71 | + |
| 72 | + $response = new StreamedResponse(); |
| 73 | + |
| 74 | + if ($request->headers->get('If-None-Match') === $hash) { |
| 75 | + $response->setStatusCode(304); |
| 76 | + } |
| 77 | + |
| 78 | + $response->headers->set('Content-Length', (string) filesize($filename)); |
| 79 | + $response->headers->set( |
| 80 | + 'Content-Disposition', |
| 81 | + 'attachment; filename="'.$target.'"' |
| 82 | + ); |
| 83 | + $response->headers->set('ETag', $hash); |
| 84 | + |
| 85 | + $this->addContentType($response, $target); |
| 86 | + |
| 87 | + $response->setCallback(function () use ($filename) { |
| 88 | + echo (string)file_get_contents($filename); |
| 89 | + }); |
| 90 | + |
| 91 | + return $response; |
| 92 | + } |
| 93 | + |
| 94 | + /** |
| 95 | + * @param Request $request Request. |
| 96 | + * |
| 97 | + * @return string |
| 98 | + */ |
| 99 | + private function getRequestTarget(Request $request) : string |
| 100 | + { |
| 101 | + $target = ''; |
| 102 | + |
| 103 | + if ($request->getQueryString()) { |
| 104 | + $target = $request->getQueryString(); |
| 105 | + parse_str($target, $result); |
| 106 | + $target = (string)$result['file']; |
| 107 | + } |
| 108 | + |
| 109 | + return $target; |
| 110 | + } |
| 111 | + |
| 112 | + /** |
| 113 | + * Добавить content type. |
| 114 | + * |
| 115 | + * @param Response $response Response. |
| 116 | + * @param string $filename Имя файла. |
| 117 | + * |
| 118 | + * @return Response |
| 119 | + */ |
| 120 | + private function addContentType(Response $response, string $filename): Response |
| 121 | + { |
| 122 | + $extension = pathinfo($filename, PATHINFO_EXTENSION); |
| 123 | + if (array_key_exists($extension, ContentTypeMapping::CONTENT_TYPE_MAPPING)) { |
| 124 | + $response->headers->set('Content-Type', ContentTypeMapping::CONTENT_TYPE_MAPPING[$extension]); |
| 125 | + } |
| 126 | + |
| 127 | + return $response; |
| 128 | + } |
| 129 | +} |
0 commit comments