Files
web/app/Controllers/ApiController.php
T

84 lines
2.7 KiB
PHP

<?php
/*
* Copyright (c) 2025, Антон Аксенов
* This file is part of iptv.axenov.dev web interface
* MIT License: https://git.axenov.dev/IPTV/web/src/branch/master/LICENSE
*/
declare(strict_types=1);
namespace App\Controllers;
use App\Errors\PlaylistNotFoundException;
use chillerlan\QRCode\QRCode;
use chillerlan\QRCode\QROptions;
use Exception;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
/**
* Контроллер методов API
*/
class ApiController extends BasicController
{
/**
* Возвращает информацию о плейлисте
*
* @param ServerRequestInterface $request
* @param ResponseInterface $response
* @return ResponseInterface
* @throws Exception
*/
public function getOne(ServerRequestInterface $request, ResponseInterface $response): ResponseInterface
{
try {
$code = $request->getAttributes()['code'] ?? null;
empty($code) && throw new PlaylistNotFoundException('');
$playlist = ini()->getPlaylist($code);
if ($playlist['isOnline'] === true) {
unset($playlist['content']);
}
return $this->responseJson($response, 200, $playlist);
} catch (PlaylistNotFoundException $e) {
return $this->responseJsonError($response, 404, $e);
}
}
/**
* Возвращает информацию о каналов плейлиста
*
* @param ServerRequestInterface $request
* @param ResponseInterface $response
* @return ResponseInterface
* @throws Exception
*/
public function makeQrCode(ServerRequestInterface $request, ResponseInterface $response): ResponseInterface
{
$code = $request->getAttribute('code');
$codes = array_keys(ini()->getPlaylists());
if (!in_array($code, $codes, true)) {
return $response->withStatus(404);
}
$filePath = cache_path("qr-codes/$code.jpg");
if (file_exists($filePath)) {
$raw = file_get_contents($filePath);
} else {
$options = new QROptions([
'version' => 5,
'outputType' => QRCode::OUTPUT_IMAGE_JPG,
'eccLevel' => QRCode::ECC_L,
]);
$data = config('app.mirror_url') ? mirror_url("$code.m3u") : base_url("$code.m3u");
$raw = new QRCode($options)->render($data, $filePath);
$raw = base64_decode(str_replace('data:image/jpg;base64,', '', $raw));
}
$mime = mime_content_type($filePath);
$response->getBody()->write($raw);
return $response->withStatus(200)
->withHeader('Content-Type', $mime);
}
}