repod/lib/Controller/PodcastController.php
Michel Roux 93e4998394
Some checks failed
repod / nextcloud (push) Successful in 56s
repod / xml (push) Successful in 13s
repod / nodejs (push) Failing after 1m50s
cs fix
2023-12-23 16:25:20 +00:00

48 lines
1.3 KiB
PHP

<?php
declare(strict_types=1);
namespace OCA\RePod\Controller;
use OCA\GPodderSync\Core\PodcastData\PodcastData;
use OCA\GPodderSync\Core\PodcastData\PodcastDataReader;
use OCA\RePod\AppInfo\Application;
use OCA\RePod\Service\UserService;
use OCP\AppFramework\Controller;
use OCP\AppFramework\Http\JSONResponse;
use OCP\Http\Client\IClientService;
use OCP\IRequest;
class PodcastController extends Controller
{
public function __construct(
IRequest $request,
private IClientService $clientService,
private UserService $userService,
private PodcastDataReader $podcastDataReader
) {
parent::__construct(Application::APP_ID, $request);
}
public function index(string $url): JSONResponse {
$podcast = $this->podcastDataReader->tryGetCachedPodcastData($url);
if ($podcast) {
return new JSONResponse($podcast);
}
$client = $this->clientService->newClient();
$feed = $client->get($url);
$statusCode = $feed->getStatusCode();
if ($statusCode < 200 || $statusCode >= 300) {
throw new \ErrorException("Web request returned non-2xx status code: {$statusCode}");
}
$podcast = PodcastData::parseRssXml((string) $feed->getBody());
$this->podcastDataReader->trySetCachedPodcastData($url, $podcast);
return new JSONResponse($podcast, $statusCode);
}
}