nextcloud-gpodder/lib/Core/PodcastData/PodcastDataReader.php

98 lines
2.8 KiB
PHP
Raw Normal View History

<?php
declare(strict_types=1);
namespace OCA\GPodderSync\Core\PodcastData;
2022-09-17 19:03:14 +00:00
use Exception;
use OCA\GPodderSync\Db\SubscriptionChange\SubscriptionChangeRepository;
use OCP\Http\Client\IClient;
use OCP\Http\Client\IClientService;
2022-09-17 19:03:14 +00:00
use OCP\Http\Client\IResponse;
use OCP\ICache;
use OCP\ICacheFactory;
class PodcastDataReader {
private ?ICache $cache = null;
private IClient $httpClient;
private SubscriptionChangeRepository $subscriptionChangeRepository;
public function __construct(
ICacheFactory $cacheFactory,
IClientService $httpClientService,
SubscriptionChangeRepository $subscriptionChangeRepository
) {
if ($cacheFactory->isLocalCacheAvailable()) {
$this->cache = $cacheFactory->createLocal('GPodderSync-Podcasts');
}
$this->httpClient = $httpClientService->newClient();
$this->subscriptionChangeRepository = $subscriptionChangeRepository;
}
public function getCachedOrFetchPodcastData(string $url, string $userId): ?PodcastData {
if ($this->cache == null) {
return $this->fetchPodcastData($url, $userId);
}
$oldData = $this->tryGetCachedPodcastData($url);
if ($oldData) {
return $oldData;
}
$newData = $this->fetchPodcastData($url, $userId);
$this->trySetCachedPodcastData($url, $newData);
return $newData;
}
private function userHasPodcast(string $url, string $userId): bool {
$subscriptionChanges = $this->subscriptionChangeRepository->findByUrl($url, $userId);
return $subscriptionChanges !== null;
}
public function fetchPodcastData(string $url, string $userId): ?PodcastData {
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";
} catch (Exception $e) {
2022-09-17 19:03:14 +00:00
return null;
}
}
private function fetchUrl(string $url): IResponse {
$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-09-17 19:03:14 +00:00
return $resp;
}
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);
}
public function trySetCachedPodcastData(string $url, PodcastData $data): bool {
return $this->cache->set($url, $data->toArray());
}
}