2022-08-28 18:44:30 +00:00
|
|
|
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace OCA\GPodderSync\Core\PodcastData;
|
|
|
|
|
2022-09-17 19:03:14 +00:00
|
|
|
use Exception;
|
2022-09-17 16:12:13 +00:00
|
|
|
use OCA\GPodderSync\Db\SubscriptionChange\SubscriptionChangeRepository;
|
2022-08-28 18:44:30 +00:00
|
|
|
use OCP\Http\Client\IClient;
|
|
|
|
use OCP\Http\Client\IClientService;
|
2022-09-17 19:03:14 +00:00
|
|
|
use OCP\Http\Client\IResponse;
|
2022-08-28 18:44:30 +00:00
|
|
|
use OCP\ICache;
|
|
|
|
use OCP\ICacheFactory;
|
|
|
|
|
2022-09-17 16:12:13 +00:00
|
|
|
class PodcastDataReader {
|
2022-08-28 18:44:30 +00:00
|
|
|
private ?ICache $cache = null;
|
|
|
|
private IClient $httpClient;
|
2022-09-17 16:12:13 +00:00
|
|
|
private SubscriptionChangeRepository $subscriptionChangeRepository;
|
2022-08-28 18:44:30 +00:00
|
|
|
|
|
|
|
public function __construct(
|
|
|
|
ICacheFactory $cacheFactory,
|
|
|
|
IClientService $httpClientService,
|
2022-10-26 20:32:59 +00:00
|
|
|
SubscriptionChangeRepository $subscriptionChangeRepository
|
2022-08-28 18:44:30 +00:00
|
|
|
) {
|
|
|
|
if ($cacheFactory->isLocalCacheAvailable()) {
|
|
|
|
$this->cache = $cacheFactory->createLocal('GPodderSync-Podcasts');
|
|
|
|
}
|
|
|
|
$this->httpClient = $httpClientService->newClient();
|
2022-09-17 16:12:13 +00:00
|
|
|
$this->subscriptionChangeRepository = $subscriptionChangeRepository;
|
2022-08-28 18:44:30 +00:00
|
|
|
}
|
|
|
|
|
2022-09-17 16:12:13 +00:00
|
|
|
public function getCachedOrFetchPodcastData(string $url, string $userId): ?PodcastData {
|
2022-08-28 18:44:30 +00:00
|
|
|
if ($this->cache == null) {
|
2022-09-17 16:12:13 +00:00
|
|
|
return $this->fetchPodcastData($url, $userId);
|
2022-08-28 18:44:30 +00:00
|
|
|
}
|
|
|
|
$oldData = $this->tryGetCachedPodcastData($url);
|
|
|
|
if ($oldData) {
|
|
|
|
return $oldData;
|
|
|
|
}
|
2022-09-17 16:12:13 +00:00
|
|
|
$newData = $this->fetchPodcastData($url, $userId);
|
2022-08-28 18:44:30 +00:00
|
|
|
$this->trySetCachedPodcastData($url, $newData);
|
|
|
|
return $newData;
|
|
|
|
}
|
|
|
|
|
2022-09-17 16:12:13 +00:00
|
|
|
private function userHasPodcast(string $url, string $userId): bool {
|
|
|
|
$subscriptionChanges = $this->subscriptionChangeRepository->findByUrl($url, $userId);
|
|
|
|
return $subscriptionChanges !== null;
|
|
|
|
}
|
|
|
|
|
2023-02-19 16:04:10 +00:00
|
|
|
public function fetchPodcastData(string $url, string $userId): ?PodcastData {
|
2022-09-17 16:12:13 +00:00
|
|
|
if (!$this->userHasPodcast($url, $userId)) {
|
|
|
|
return null;
|
|
|
|
}
|
2022-09-17 19:03:14 +00:00
|
|
|
$resp = $this->fetchUrl($url);
|
|
|
|
$data = PodcastData::parseRssXml($resp->getBody());
|
|
|
|
$blob = $this->tryFetchImageBlob($data);
|
|
|
|
if ($blob) {
|
|
|
|
$data->setImageBlob($blob);
|
|
|
|
}
|
|
|
|
return $data;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function tryFetchImageBlob(PodcastData $data): ?string {
|
|
|
|
if (!$data->getImageUrl()) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
$resp = $this->fetchUrl($data->getImageUrl());
|
|
|
|
$contentType = $resp->getHeader('Content-Type');
|
|
|
|
$body = $resp->getBody();
|
|
|
|
$bodyBase64 = base64_encode($body);
|
|
|
|
return "data:$contentType;base64,$bodyBase64";
|
2022-10-27 07:10:16 +00:00
|
|
|
} catch (Exception $e) {
|
2022-09-17 19:03:14 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private function fetchUrl(string $url): IResponse {
|
2022-08-28 18:44:30 +00:00
|
|
|
$resp = $this->httpClient->get($url);
|
|
|
|
$statusCode = $resp->getStatusCode();
|
|
|
|
if ($statusCode < 200 || $statusCode >= 300) {
|
2022-09-17 19:03:14 +00:00
|
|
|
throw new \ErrorException("Web request returned non-2xx status code: $statusCode");
|
2022-08-28 18:44:30 +00:00
|
|
|
}
|
2022-09-17 19:03:14 +00:00
|
|
|
return $resp;
|
2022-08-28 18:44:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function tryGetCachedPodcastData(string $url): ?PodcastData {
|
|
|
|
$oldData = $this->cache->get($url);
|
|
|
|
if (!$oldData) {
|
|
|
|
return null;
|
|
|
|
}
|
2022-09-17 15:03:02 +00:00
|
|
|
return PodcastData::fromArray($oldData);
|
2022-08-28 18:44:30 +00:00
|
|
|
}
|
|
|
|
|
2022-09-17 16:12:13 +00:00
|
|
|
public function trySetCachedPodcastData(string $url, PodcastData $data): bool {
|
|
|
|
return $this->cache->set($url, $data->toArray());
|
2022-08-28 18:44:30 +00:00
|
|
|
}
|
|
|
|
}
|