From 7d61a1cb09f792cbf0fbfc2eb070693c04533501 Mon Sep 17 00:00:00 2001 From: Benjamin Brahmer Date: Fri, 15 Oct 2021 10:52:16 +0200 Subject: Download feed logos via guzzle to have better error handling Signed-off-by: Benjamin Brahmer --- lib/Fetcher/FeedFetcher.php | 76 ++++++++++++++++++++++++++++++++++++++------- 1 file changed, 65 insertions(+), 11 deletions(-) (limited to 'lib/Fetcher/FeedFetcher.php') 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"; -- cgit v1.2.3 From 59a888481f1b2cd2dd928b1f23a949257be0d947 Mon Sep 17 00:00:00 2001 From: Benjamin Brahmer Date: Sun, 17 Oct 2021 11:33:17 +0200 Subject: make use of base_url to handle relative urls Signed-off-by: Benjamin Brahmer --- lib/Fetcher/FeedFetcher.php | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) (limited to 'lib/Fetcher/FeedFetcher.php') diff --git a/lib/Fetcher/FeedFetcher.php b/lib/Fetcher/FeedFetcher.php index 9cab0287d..0b6ce63a0 100755 --- a/lib/Fetcher/FeedFetcher.php +++ b/lib/Fetcher/FeedFetcher.php @@ -70,11 +70,6 @@ class FeedFetcher implements IFeedFetcher */ private $logger; - /** - * @var Client - */ - private $client; - public function __construct( FeedIo $fetcher, Favicon $favicon, @@ -82,8 +77,7 @@ class FeedFetcher implements IFeedFetcher IL10N $l10n, ITempManager $ITempManager, Time $time, - LoggerInterface $logger, - Client $client + LoggerInterface $logger ) { $this->reader = $fetcher; $this->faviconFactory = $favicon; @@ -92,7 +86,6 @@ class FeedFetcher implements IFeedFetcher $this->ITempManager = $ITempManager; $this->time = $time; $this->logger = $logger; - $this->client = $client; } @@ -359,7 +352,9 @@ class FeedFetcher implements IFeedFetcher } try { - $response = $this->client->request( + // Base_uri can only be set on creation, will be used when link is relative. + $client = new Client(['base_uri' => $base_url]); + $response = $client->request( 'GET', $favicon, [ -- cgit v1.2.3 From 74586396f89aaec1b79a6b601da324da25a0718b Mon Sep 17 00:00:00 2001 From: Benjamin Brahmer Date: Sun, 17 Oct 2021 15:15:55 +0200 Subject: make use of constant Signed-off-by: Benjamin Brahmer Co-authored-by: Sean Molenaar --- lib/Fetcher/FeedFetcher.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'lib/Fetcher/FeedFetcher.php') diff --git a/lib/Fetcher/FeedFetcher.php b/lib/Fetcher/FeedFetcher.php index 0b6ce63a0..7d8255e37 100755 --- a/lib/Fetcher/FeedFetcher.php +++ b/lib/Fetcher/FeedFetcher.php @@ -342,7 +342,7 @@ class FeedFetcher implements IFeedFetcher } // 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)]); + $favicon_path = join(DIRECTORY_SEPARATOR, [$this->ITempManager->getTempBaseDir(), md5($favicon)]); $downloaded = false; if (file_exists($favicon_path)) { -- cgit v1.2.3 From 137af71bb3578b46937d5cb0db363d7674861fc4 Mon Sep 17 00:00:00 2001 From: Benjamin Brahmer Date: Sun, 17 Oct 2021 15:16:25 +0200 Subject: Smarter error logging Signed-off-by: Benjamin Brahmer Co-authored-by: Sean Molenaar --- lib/Fetcher/FeedFetcher.php | 11 +---------- 1 file changed, 1 insertion(+), 10 deletions(-) (limited to 'lib/Fetcher/FeedFetcher.php') diff --git a/lib/Fetcher/FeedFetcher.php b/lib/Fetcher/FeedFetcher.php index 7d8255e37..7423c66df 100755 --- a/lib/Fetcher/FeedFetcher.php +++ b/lib/Fetcher/FeedFetcher.php @@ -377,22 +377,13 @@ class FeedFetcher implements IFeedFetcher ] ); } 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() + 'error' => $e->getResponse() ?? 'Unknown' ] ); - } 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"; -- cgit v1.2.3 From 9f3b63b5f986fe583a89226f8412e9e91f5f4ca2 Mon Sep 17 00:00:00 2001 From: Benjamin Brahmer Date: Sun, 14 Nov 2021 11:28:33 +0100 Subject: Catch network errors while fetching feed logos, fixes #1570 Signed-off-by: Benjamin Brahmer --- lib/Fetcher/FeedFetcher.php | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) (limited to 'lib/Fetcher/FeedFetcher.php') diff --git a/lib/Fetcher/FeedFetcher.php b/lib/Fetcher/FeedFetcher.php index 7423c66df..5b48070d5 100755 --- a/lib/Fetcher/FeedFetcher.php +++ b/lib/Fetcher/FeedFetcher.php @@ -20,6 +20,7 @@ use FeedIo\FeedInterface; use FeedIo\FeedIo; use GuzzleHttp\Client; use GuzzleHttp\Exception\RequestException; +use GuzzleHttp\Exception\ConnectException; use Net_URL2; use OCP\IL10N; @@ -376,14 +377,14 @@ class FeedFetcher implements IFeedFetcher 'logo' => $favicon ] ); - } catch (RequestException $e) { - $this->logger->info( - 'An error occurred while trying to download the feed logo of {url}: {error}', - [ - 'url' => $url, - 'error' => $e->getResponse() ?? 'Unknown' - ] - ); + } catch (RequestException | ConnectException $e) { + $this->logger->info( + 'An error occurred while trying to download the feed logo of {url}: {error}', + [ + 'url' => $url, + 'error' => $e->getResponse() ?? 'Unknown' + ] + ); } $is_image = $downloaded && substr(mime_content_type($favicon_path), 0, 5) === "image"; -- cgit v1.2.3