summaryrefslogtreecommitdiffstats
path: root/lib/Fetcher/FeedFetcher.php
diff options
context:
space:
mode:
Diffstat (limited to 'lib/Fetcher/FeedFetcher.php')
-rwxr-xr-xlib/Fetcher/FeedFetcher.php76
1 files changed, 65 insertions, 11 deletions
diff --git a/lib/Fetcher/FeedFetcher.php b/lib/Fetcher/FeedFetcher.php
index ef232eece..9cab0287d 100755
--- a/lib/Fetcher/FeedFetcher.php
+++ b/lib/Fetcher/FeedFetcher.php
@@ -18,6 +18,8 @@ use Favicon\Favicon;
use FeedIo\Feed\ItemInterface;
use FeedIo\FeedInterface;
use FeedIo\FeedIo;
+use GuzzleHttp\Client;
+use GuzzleHttp\Exception\RequestException;
use Net_URL2;
use OCP\IL10N;
@@ -68,6 +70,11 @@ class FeedFetcher implements IFeedFetcher
*/
private $logger;
+ /**
+ * @var Client
+ */
+ private $client;
+
public function __construct(
FeedIo $fetcher,
Favicon $favicon,
@@ -75,7 +82,8 @@ class FeedFetcher implements IFeedFetcher
IL10N $l10n,
ITempManager $ITempManager,
Time $time,
- LoggerInterface $logger
+ LoggerInterface $logger,
+ Client $client
) {
$this->reader = $fetcher;
$this->faviconFactory = $favicon;
@@ -84,6 +92,7 @@ class FeedFetcher implements IFeedFetcher
$this->ITempManager = $ITempManager;
$this->time = $time;
$this->logger = $logger;
+ $this->client = $client;
}
@@ -318,14 +327,15 @@ class FeedFetcher implements IFeedFetcher
/**
* Return the favicon for a given feed and url
*
- * @param FeedInterface $feed Feed to check for a logo
- * @param string $url Original URL for the feed
+ * @param FeedInterface $feed Feed to check for a logo
+ * @param string $url Original URL for the feed
*
* @return string|mixed|bool
*/
protected function getFavicon(FeedInterface $feed, string $url)
{
- $favicon = $feed->getLogo();
+ // trim the string because authors do funny things
+ $favicon = trim($feed->getLogo());
ini_set('user_agent', 'NextCloud-News/1.0');
@@ -334,17 +344,61 @@ class FeedFetcher implements IFeedFetcher
$base_url = $base_url->getNormalizedURL();
// check if feed has a logo entry
- if (is_null($favicon) || trim($favicon) === '') {
+ if (is_null($favicon) || $favicon === '') {
return $this->faviconFactory->get($base_url);
}
- $favicon_path = join("/", [$this->ITempManager->getTempBaseDir(), basename($favicon)]);
+ // logo will be saved in the tmp folder provided by Nextcloud, file is named as md5 of the url
+ $favicon_path = join("/", [$this->ITempManager->getTempBaseDir(), md5($favicon)]);
+ $downloaded = false;
- $downloaded = copy(
- $favicon,
- $favicon_path,
- stream_context_create([ 'http' => [ 'ignore_errors' => true ] ])
- );
+ if (file_exists($favicon_path)) {
+ $last_modified = filemtime($favicon_path);
+ } else {
+ $last_modified = 0;
+ }
+
+ try {
+ $response = $this->client->request(
+ 'GET',
+ $favicon,
+ [
+ 'sink' => $favicon_path,
+ 'headers' => [
+ 'User-Agent' => 'NextCloud-News/1.0',
+ 'Accept' => 'image/*',
+ 'If-Modified-Since' => date(DateTime::RFC7231, $last_modified)
+ ]
+ ]
+ );
+ $downloaded = true;
+
+ $this->logger->debug(
+ "Feed:{url} Logo:{logo} Status:{status}",
+ [
+ 'status' => $response->getStatusCode(),
+ 'url' => $favicon_path,
+ 'logo' => $favicon
+ ]
+ );
+ } catch (RequestException $e) {
+ if ($e->hasResponse()) {
+ $this->logger->info(
+ 'An error occurred while trying to download the feed logo of {url}: {error}',
+ [
+ 'url' => $url,
+ 'error' => $e->getResponse()
+ ]
+ );
+ } else {
+ $this->logger->info(
+ 'An unknown error occurred while trying to download the feed logo of {url}.',
+ [
+ 'url' => $url,
+ ]
+ );
+ }
+ }
$is_image = $downloaded && substr(mime_content_type($favicon_path), 0, 5) === "image";