Skip to content

Commit

Permalink
Prevent some of the favicon fetching errors
Browse files Browse the repository at this point in the history
change user agent for fetching the feeds log to 'NextCloud-News/1.0'
check if logo in feed is just an empty string
remove path of url before searching for a favicon
check if file was actually downloaded
Ignore http errors when fetching favicons
Add feed to integration tests that doesn't have a logo

Co-authored-by: Alec Kojaev <alec.kojaev@gmail.com>
Signed-off-by: Benjamin Brahmer <info@b-brahmer.de>
  • Loading branch information
Grotax and oldnomad committed May 3, 2021
1 parent ccc7153 commit 6dfc4ad
Show file tree
Hide file tree
Showing 5 changed files with 130 additions and 66 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ The format is almost based on [Keep a Changelog](https://keepachangelog.com/en/1
### Changed

### Fixed
- content of atom feeds is missing
- content of atom feeds is missing (#1325)
- Fix some of the favicon fetching errors (#1319)

# Releases
## [15.4.0] - 2021-04-26
Expand Down
149 changes: 94 additions & 55 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 16 additions & 8 deletions lib/Fetcher/FeedFetcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -327,28 +327,36 @@ protected function getFavicon(FeedInterface $feed, string $url)
{
$favicon = $feed->getLogo();

// check if feed has a logo
if (is_null($favicon)) {
return $this->faviconFactory->get($url);
ini_set('user_agent', 'NextCloud-News/1.0');

$base_url = new Net_URL2($url);
$base_url->setPath("");
$base_url = $base_url->getNormalizedURL();

// check if feed has a logo entry
if (is_null($favicon) || trim($favicon) === '') {
return $this->faviconFactory->get($base_url);
}

$favicon_path = join("/", [$this->ITempManager->getTempBaseDir(), basename($favicon)]);
copy(

$downloaded = copy(
$favicon,
$favicon_path
$favicon_path,
stream_context_create([ 'http' => [ 'ignore_errors' => true ] ])
);

$is_image = substr(mime_content_type($favicon_path), 0, 5) === "image";
$is_image = $downloaded && substr(mime_content_type($favicon_path), 0, 5) === "image";

// check if file is actually an image
if (!$is_image) {
return $this->faviconFactory->get($url);
return $this->faviconFactory->get($base_url);
}

list($width, $height, $type, $attr) = getimagesize($favicon_path);
// check if image is square else fall back to favicon
if ($width !== $height) {
return $this->faviconFactory->get($url);
return $this->faviconFactory->get($base_url);
}

return $favicon;
Expand Down
Loading

0 comments on commit 6dfc4ad

Please sign in to comment.