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