2023-08-22 17:41:17 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace OCA\RePod\Controller;
|
|
|
|
|
2024-01-16 13:21:35 +00:00
|
|
|
use OCA\GPodderSync\Core\PodcastData\PodcastData;
|
|
|
|
use OCA\GPodderSync\Core\PodcastData\PodcastDataReader;
|
2023-08-22 17:41:17 +00:00
|
|
|
use OCA\RePod\AppInfo\Application;
|
|
|
|
use OCP\AppFramework\Controller;
|
2024-10-18 13:19:45 +00:00
|
|
|
use OCP\AppFramework\Http\Attribute\FrontpageRoute;
|
|
|
|
use OCP\AppFramework\Http\Attribute\NoAdminRequired;
|
|
|
|
use OCP\AppFramework\Http\Attribute\NoCSRFRequired;
|
2023-08-22 17:41:17 +00:00
|
|
|
use OCP\AppFramework\Http\JSONResponse;
|
|
|
|
use OCP\Http\Client\IClientService;
|
2024-06-14 17:32:26 +00:00
|
|
|
use OCP\ICacheFactory;
|
2023-08-22 17:41:17 +00:00
|
|
|
use OCP\IRequest;
|
|
|
|
|
2023-08-24 10:48:10 +00:00
|
|
|
class PodcastController extends Controller
|
2023-08-22 17:41:17 +00:00
|
|
|
{
|
|
|
|
public function __construct(
|
|
|
|
IRequest $request,
|
2024-11-12 09:11:21 +00:00
|
|
|
private readonly ICacheFactory $cacheFactory,
|
|
|
|
private readonly IClientService $clientService,
|
|
|
|
private readonly PodcastDataReader $podcastDataReader
|
2023-08-22 17:41:17 +00:00
|
|
|
) {
|
|
|
|
parent::__construct(Application::APP_ID, $request);
|
|
|
|
}
|
|
|
|
|
2024-10-18 13:19:45 +00:00
|
|
|
#[NoAdminRequired]
|
|
|
|
#[NoCSRFRequired]
|
|
|
|
#[FrontpageRoute(verb: 'GET', url: '/podcast')]
|
2023-12-23 16:25:20 +00:00
|
|
|
public function index(string $url): JSONResponse {
|
2024-06-14 17:40:32 +00:00
|
|
|
$podcast = null;
|
|
|
|
|
2024-06-14 17:32:26 +00:00
|
|
|
if ($this->cacheFactory->isLocalCacheAvailable()) {
|
|
|
|
try {
|
|
|
|
$podcast = $this->podcastDataReader->tryGetCachedPodcastData($url);
|
2024-11-10 12:18:53 +00:00
|
|
|
} catch (\Exception) {
|
2024-06-14 17:32:26 +00:00
|
|
|
}
|
2024-02-04 08:40:21 +00:00
|
|
|
}
|
2024-01-16 13:21:35 +00:00
|
|
|
|
|
|
|
if ($podcast) {
|
|
|
|
return new JSONResponse($podcast);
|
|
|
|
}
|
|
|
|
|
2023-08-22 17:41:17 +00:00
|
|
|
$client = $this->clientService->newClient();
|
2023-08-22 18:14:15 +00:00
|
|
|
$feed = $client->get($url);
|
2023-08-24 18:53:54 +00:00
|
|
|
$podcast = PodcastData::parseRssXml((string) $feed->getBody());
|
2024-02-04 08:40:21 +00:00
|
|
|
|
2024-06-14 17:32:26 +00:00
|
|
|
if ($this->cacheFactory->isLocalCacheAvailable()) {
|
|
|
|
try {
|
|
|
|
$this->podcastDataReader->trySetCachedPodcastData($url, $podcast);
|
2024-11-10 12:18:53 +00:00
|
|
|
} catch (\Exception) {
|
2024-06-14 17:32:26 +00:00
|
|
|
}
|
2024-02-04 08:40:21 +00:00
|
|
|
}
|
2023-08-22 17:41:17 +00:00
|
|
|
|
2024-01-16 13:21:35 +00:00
|
|
|
return new JSONResponse($podcast, $feed->getStatusCode());
|
2023-08-22 17:41:17 +00:00
|
|
|
}
|
|
|
|
}
|