Исправлена логика редиректов

This commit is contained in:
2025-03-03 14:01:10 +08:00
parent 50a5161374
commit 1bce9ecfe5
2 changed files with 16 additions and 29 deletions

View File

@@ -55,30 +55,4 @@ class BasicController
$view = Twig::fromRequest($request); $view = Twig::fromRequest($request);
return $view->render($response, $template, $data); return $view->render($response, $template, $data);
} }
/**
* Возвращает плейлист по его ID для обработки
*
* @param string $id
* @param bool $asJson
* @return Playlist
* @throws Exception
*/
protected function getPlaylist(string $id, bool $asJson = false): Playlist
{
ini()->load();
if (ini()->getRedirection($id)) {
$redirectTo = base_url(ini()->getRedirection($id) . ($asJson ? '/json' : '/details'));
Flight::redirect($redirectTo);
die;
}
try {
return ini()->getPlaylist($id);
} catch (PlaylistNotFoundException) {
$this->notFound($id, $asJson);
die;
}
}
} }

View File

@@ -5,6 +5,7 @@ declare(strict_types=1);
namespace App\Controllers; namespace App\Controllers;
use App\Core\ChannelLogo; use App\Core\ChannelLogo;
use App\Errors\PlaylistNotFoundException;
use Exception; use Exception;
use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface; use Psr\Http\Message\ServerRequestInterface;
@@ -67,9 +68,14 @@ class WebController extends BasicController
*/ */
public function redirect(ServerRequestInterface $request, ResponseInterface $response): ResponseInterface public function redirect(ServerRequestInterface $request, ResponseInterface $response): ResponseInterface
{ {
ini()->load();
$code = $request->getAttributes()['code']; $code = $request->getAttributes()['code'];
$playlist = $this->getPlaylist($code); try {
$playlist = ini()->getPlaylist($code);
return $response->withHeader('Location', $playlist->pls); return $response->withHeader('Location', $playlist->pls);
} catch (PlaylistNotFoundException) {
return $this->notFound($request, $response);
}
} }
/** /**
@@ -83,8 +89,15 @@ class WebController extends BasicController
*/ */
public function details(ServerRequestInterface $request, ResponseInterface $response): ResponseInterface public function details(ServerRequestInterface $request, ResponseInterface $response): ResponseInterface
{ {
ini()->load();
$code = $request->getAttributes()['code']; $code = $request->getAttributes()['code'];
$playlist = $this->getPlaylist($code); try {
$playlist = ini()->getPlaylist($code);
$response->withHeader('Location', $playlist->pls);
} catch (PlaylistNotFoundException) {
return $this->notFound($request, $response);
}
$playlist->fetchContent(); $playlist->fetchContent();
$playlist->parse(); $playlist->parse();