repod/lib/Controller/PodcastController.php
Michel Roux 0024507ed5
All checks were successful
repod / xml (push) Successful in 16s
repod / php (push) Successful in 1m7s
repod / nodejs (push) Successful in 1m29s
repod / release (push) Has been skipped
refactor: 🚚 move routes to PHP anotations
2024-10-18 15:19:45 +02:00

61 lines
1.5 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 OCP\AppFramework\Controller;
use OCP\AppFramework\Http\Attribute\FrontpageRoute;
use OCP\AppFramework\Http\Attribute\NoAdminRequired;
use OCP\AppFramework\Http\Attribute\NoCSRFRequired;
use OCP\AppFramework\Http\JSONResponse;
use OCP\Http\Client\IClientService;
use OCP\ICacheFactory;
use OCP\IRequest;
class PodcastController extends Controller
{
public function __construct(
IRequest $request,
private ICacheFactory $cacheFactory,
private IClientService $clientService,
private PodcastDataReader $podcastDataReader
) {
parent::__construct(Application::APP_ID, $request);
}
#[NoAdminRequired]
#[NoCSRFRequired]
#[FrontpageRoute(verb: 'GET', url: '/podcast')]
public function index(string $url): JSONResponse {
$podcast = null;
if ($this->cacheFactory->isLocalCacheAvailable()) {
try {
$podcast = $this->podcastDataReader->tryGetCachedPodcastData($url);
} catch (\Exception $e) {
}
}
if ($podcast) {
return new JSONResponse($podcast);
}
$client = $this->clientService->newClient();
$feed = $client->get($url);
$podcast = PodcastData::parseRssXml((string) $feed->getBody());
if ($this->cacheFactory->isLocalCacheAvailable()) {
try {
$this->podcastDataReader->trySetCachedPodcastData($url, $podcast);
} catch (\Exception $e) {
}
}
return new JSONResponse($podcast, $feed->getStatusCode());
}
}