Skip to content

Commit

Permalink
use compression when downloading feed logo
Browse files Browse the repository at this point in the history
add changelog
code cleanup

Co-authored-by: Sean Molenaar <SMillerDev@users.noreply.github.com>
Signed-off-by: Benjamin Brahmer <info@b-brahmer.de>
  • Loading branch information
Grotax and SMillerDev committed Aug 23, 2023
1 parent 1b1af43 commit 38af7d1
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 18 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ The format is mostly based on [Keep a Changelog](https://keepachangelog.com/en/1
### Changed
- Drop support for Nextcloud 25, Supported: 26, 27 (#2316)
- Add a new command for occ `./occ news:updater:job` allows to check and reset the update job (#2166)
- Check for available http(s) compression options and use them (gzip, deflate, brotli) (#2328)
### Fixed

# Releases
Expand Down
21 changes: 9 additions & 12 deletions lib/Config/FetcherConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,28 +96,25 @@ public function __construct(IConfig $config)

/**
* Checks for available encoding options
*
*
* @return String list of supported encoding types
*/
private function checkEncoding()
public function checkEncoding()
{
$supportedEncoding = [];

$curl_version = curl_version();
// check curl features
$curl_features = curl_version()["features"];

$bitfields = Array(
'CURL_VERSION_LIBZ' => ['gzip', 'deflate'],
'CURL_VERSION_BROTLI' => ['br']
);
$bitfields = array('CURL_VERSION_LIBZ' => ['gzip', 'deflate'], 'CURL_VERSION_BROTLI' => ['br']);

foreach ($bitfields as $feature => $header)
{
foreach ($bitfields as $feature => $header) {
// checking available features via the 'features' bitmask and adding available types to the list
$curl_version['features'] & constant($feature) ? $supportedEncoding = array_merge($supportedEncoding, $header) : null;

if (defined($feature) && $curl_features & constant($feature)) {
$supportedEncoding = array_merge($supportedEncoding, $header);
}
}
return implode(", ", $supportedEncoding);

}

/**
Expand Down
14 changes: 12 additions & 2 deletions lib/Fetcher/FeedFetcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
use Net_URL2;
use OCP\IL10N;
use OCP\ITempManager;
use OCP\IConfig;

use OCA\News\Db\Item;
use OCA\News\Db\Feed;
Expand Down Expand Up @@ -73,14 +74,20 @@ class FeedFetcher implements IFeedFetcher
*/
private $logger;

/**
* @var IConfig
*/
private $iConfig;

public function __construct(
FeedIo $fetcher,
Favicon $favicon,
Scraper $scraper,
IL10N $l10n,
ITempManager $ITempManager,
Time $time,
LoggerInterface $logger
LoggerInterface $logger,
IConfig $iConfig
) {
$this->reader = $fetcher;
$this->faviconFactory = $favicon;
Expand All @@ -89,6 +96,7 @@ public function __construct(
$this->ITempManager = $ITempManager;
$this->time = $time;
$this->logger = $logger;
$this->iConfig = $iConfig;
}


Expand Down Expand Up @@ -401,6 +409,7 @@ protected function getFavicon(FeedInterface $feed, string $url): ?string
try {
// Base_uri can only be set on creation, will be used when link is relative.
$client = new Client(['base_uri' => $base_url]);
$fetcherConfig = new FetcherConfig($this->iConfig);
$response = $client->request(
'GET',
$favicon,
Expand All @@ -409,7 +418,8 @@ protected function getFavicon(FeedInterface $feed, string $url): ?string
'headers' => [
'User-Agent' => FetcherConfig::DEFAULT_USER_AGENT,
'Accept' => 'image/*',
'If-Modified-Since' => date(DateTime::RFC7231, $last_modified)
'If-Modified-Since' => date(DateTime::RFC7231, $last_modified),
'Accept-Encoding' => $fetcherConfig->checkEncoding()
]
]
);
Expand Down
9 changes: 5 additions & 4 deletions tests/Unit/Fetcher/FeedFetcherTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
use OCA\News\Utility\Time;
use OCP\IL10N;
use OCP\ITempManager;
use OCP\IConfig;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Psr\Log\LoggerInterface;
Expand Down Expand Up @@ -108,9 +109,9 @@ class FeedFetcherTest extends TestCase
private $scraper;

/**
* @var MockObject|Client
* @var MockObject|iConfig
*/
private $client;
private $iConfig;

//metadata
/**
Expand Down Expand Up @@ -194,7 +195,7 @@ protected function setUp(): void
$this->scraper = $this->getMockBuilder(Scraper::class)
->disableOriginalConstructor()
->getMock();
$this->client = $this->getMockBuilder(Client::class)
$this->iConfig = $this->getMockBuilder(IConfig::class)
->disableOriginalConstructor()
->getMock();
$this->fetcher = new FeedFetcher(
Expand All @@ -205,7 +206,7 @@ protected function setUp(): void
$this->ITempManager,
$timeFactory,
$this->logger,
$this->client
$this->iConfig
);
$this->url = 'http://tests/';

Expand Down

0 comments on commit 38af7d1

Please sign in to comment.