From d1be6909065cb3d4b39a220c94164e50a391476f Mon Sep 17 00:00:00 2001 From: AnthonyAxenov Date: Tue, 13 May 2025 14:02:53 +0800 Subject: [PATCH] =?UTF-8?q?=D0=A3=D0=B4=D0=B0=D0=BB=D0=B5=D0=BD=D0=B0=20?= =?UTF-8?q?=D0=BE=D0=B1=D1=80=D0=B0=D0=B1=D0=BE=D1=82=D0=BA=D0=B0=20=D0=BB?= =?UTF-8?q?=D0=BE=D0=B3=D0=BE=D1=82=D0=B8=D0=BF=D0=BE=D0=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Controllers/ApiController.php | 37 ------ app/Playlists/ChannelLogo.php | 200 ------------------------------ 2 files changed, 237 deletions(-) delete mode 100644 app/Playlists/ChannelLogo.php diff --git a/app/Controllers/ApiController.php b/app/Controllers/ApiController.php index c08e4aa..d523afb 100644 --- a/app/Controllers/ApiController.php +++ b/app/Controllers/ApiController.php @@ -76,41 +76,4 @@ class ApiController extends BasicController return $this->responseJsonError($response, 404, $e); } } - - /** - * Возвращает логотип канала - * - * @param ServerRequestInterface $request - * @param ResponseInterface $response - * @return ResponseInterface - * @throws LoaderError - * @throws PlaylistNotFoundException - * @todo логотипы каналов - */ - public function logo(ServerRequestInterface $request, ResponseInterface $response): ResponseInterface - { - $code = $request->getAttributes()['code']; - $playlist = ini()->getPlaylist($code); - $channelHash = $request->getAttributes()['hash']; - $channel = $playlist['channels'][$channelHash]; - $url = $channel['attributes']['tvg-logo'] ?? ''; - - $logo = new ChannelLogo($url); - if (!$logo->readFile()) { - $logo->fetch(); - if ($logo->size() === 0) { - $logo->setDefault(); - } else { - $logo->store(); - } - } - - $body = $logo->raw(); - $size = $logo->size(); - $mime = $logo->mimeType(); - - $response->getBody()->write($body); - return $response->withHeader('Content-Type', $mime) - ->withHeader('Content-Length', $size); - } } diff --git a/app/Playlists/ChannelLogo.php b/app/Playlists/ChannelLogo.php deleted file mode 100644 index caca32a..0000000 --- a/app/Playlists/ChannelLogo.php +++ /dev/null @@ -1,200 +0,0 @@ -prepareUrl($url); - if (is_string($url)) { - $this->url = $url; - $this->hash = md5($url); - $this->path = cache_path("tv-logos/$this->hash"); - } - } - - /** - * Валидирует и очищает ссылку на изображение - * - * @param string $url - * @return false|string - */ - protected function prepareUrl(string $url): false|string - { - $parts = parse_url(trim($url)); - if (!is_array($parts) || count($parts) < 2) { - return false; - } - - $result = $parts['scheme'] . '://' . $parts['host']; - $result .= (empty($parts['port']) ? '' : ':' . $parts['port']); - - return $result . $parts['path']; - } - - /** - * Загружает сырое изображение по ссылке и определяет его MIME-тип - * - * @return bool - */ - public function fetch(): bool - { - $this->rawData = @file_get_contents($this->url); - $isFetched = is_string($this->rawData); - if (!$isFetched) { - return false; - } - - $this->mimeType = $this->mimeType(); - return true; - } - - /** - * Сохраняет сырое изображение в кэш - * - * @return bool - */ - public function store(): bool - { - return is_string($this->rawData) - && $this->prepareCacheDir() - && @file_put_contents($this->path, $this->rawData); - } - - /** - * Считывает изображение из кэша - * - * @return bool - */ - public function readFile(): bool - { - if (!file_exists($this->path)) { - return false; - } - - $this->rawData = @file_get_contents($this->path); - return is_string($this->rawData); - } - - /** - * Считывает дефолтный эскиз вместо логотипа - * - * @return bool - */ - public function setDefault(): bool - { - $this->path = root_path('public/no-tvg-logo.png'); - return $this->readFile(); - } - - /** - * Возвращает base64-кодированное изображение - * - * @return string|null - */ - public function asBase64(): ?string - { - if (!is_string($this->rawData)) { - return null; - } - - return "data:$this->mimeType;base64," . base64_encode($this->rawData); - } - - /** - * Возвращает сырое изображение - * - * @return false|string|null - */ - public function raw(): false|string|null - { - return $this->rawData; - } - - /** - * Проверяет готовность директории кэша изображений, создавая её при необходимости - * - * @return bool - */ - public function prepareCacheDir(): bool - { - $cacheFileDir = cache_path('tv-logos'); - - return is_dir($cacheFileDir) - || @mkdir($cacheFileDir, 0775, true); - } - - /** - * Возвращает MIME-тип сырого изображения - * - * @return string|null - */ - public function mimeType(): ?string - { - if (!is_string($this->rawData)) { - return null; - } - - $finfo = new \finfo(FILEINFO_MIME_TYPE); - return $finfo->buffer($this->rawData) ?: null; - } - - /** - * Возвращает размер сырого изображения в байтах - * - * @return int - */ - public function size(): int - { - return strlen((string)$this->rawData); - } - - /** - * @inheritDoc - */ - public function __toString(): string - { - return $this->asBase64(); - } -}