Skip to content

Commit 1b6cf88

Browse files
committed
fix(CrawlService): Use IClient instead of raw Guzzle
Signed-off-by: Marcel Klehr <mklehr@gmx.net>
1 parent 35ab6bc commit 1b6cf88

1 file changed

Lines changed: 14 additions & 16 deletions

File tree

lib/Service/CrawlService.php

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@
1111
use Exception;
1212
use fivefilters\Readability\Configuration;
1313
use fivefilters\Readability\Readability;
14-
use GuzzleHttp\Client;
15-
use GuzzleHttp\Psr7\Response;
1614
use Mimey\MimeTypes;
1715
use OC\User\NoUserException;
1816
use OCA\Bookmarks\Db\Bookmark;
@@ -24,6 +22,9 @@
2422
use OCP\Files\IRootFolder;
2523
use OCP\Files\NotFoundException;
2624
use OCP\Files\NotPermittedException;
25+
use OCP\Http\Client\IClient;
26+
use OCP\Http\Client\IClientService;
27+
use OCP\Http\Client\IResponse;
2728
use OCP\IConfig;
2829
use OCP\IL10N;
2930
use OCP\Lock\LockedException;
@@ -36,18 +37,21 @@ class CrawlService {
3637
public const READ_TIMEOUT = 30;
3738
public const UA_FIREFOX = 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:87.0) Gecko/20100101 Firefox/87.0';
3839

40+
private IClient $client;
3941
private MimeTypes $mimey;
4042

4143
public function __construct(
4244
private BookmarkMapper $bookmarkMapper,
4345
private BookmarkPreviewer $bookmarkPreviewer,
4446
private FaviconPreviewer $faviconPreviewer,
47+
IClientService $clientService,
4548
private IConfig $config,
4649
private IRootFolder $rootFolder,
4750
private IL10N $l,
4851
private LoggerInterface $logger,
4952
private UserSettingsService $userSettingsService,
5053
) {
54+
$this->client = $clientService->newClient();
5155
$this->mimey = new MimeTypes;
5256
}
5357

@@ -60,9 +64,7 @@ public function crawl(Bookmark $bookmark): void {
6064
return;
6165
}
6266
try {
63-
$client = new Client();
64-
/** @var Response $resp */
65-
$resp = $client->get($bookmark->getUrl(), [
67+
$resp = $this->client->get($bookmark->getUrl(), [
6668
'headers' => [
6769
'User-Agent' => self::UA_FIREFOX,
6870
],
@@ -91,15 +93,13 @@ public function crawl(Bookmark $bookmark): void {
9193
$this->bookmarkMapper->update($bookmark);
9294
}
9395

94-
private function archiveContent(Bookmark $bookmark, Response $resp): void {
95-
$header = $resp->getHeader('Content-Type');
96+
private function archiveContent(Bookmark $bookmark, IResponse $resp): void {
97+
$contentType = $resp->getHeader('Content-Type');
9698

97-
if (empty($header)) {
99+
if ($contentType === '') {
98100
return;
99101
}
100102

101-
$contentType = $header[0] ?? null;
102-
103103
if ($contentType !== null && str_contains($contentType, 'text/html')) {
104104
if ($bookmark->getHtmlContent() === null || $bookmark->getHtmlContent() === '') {
105105
$config = new Configuration();
@@ -120,18 +120,16 @@ private function archiveContent(Bookmark $bookmark, Response $resp): void {
120120
}
121121
}
122122

123-
private function archiveFile(Bookmark $bookmark, Response $resp): void {
124-
$header = $resp->getHeader('Content-Type');
123+
private function archiveFile(Bookmark $bookmark, IResponse $resp): void {
124+
$contentType = $resp->getHeader('Content-Type');
125125

126-
if (empty($header)) {
126+
if ($contentType === '') {
127127
return;
128128
}
129129

130-
$contentType = $header[0] ?? null;
131-
132130
if ($contentType !== null && !str_contains($contentType, 'text/html') && $bookmark->getArchivedFile() === null) {
133131
$contentLengthHeader = $resp->getHeader('Content-Length');
134-
$contentLength = isset($contentLengthHeader[0]) ? (int)$contentLengthHeader[0] : 0;
132+
$contentLength = $contentLengthHeader !== '' ? (int)$contentLengthHeader : 0;
135133

136134
if ($contentLength < self::MAX_BODY_LENGTH) {
137135
try {

0 commit comments

Comments
 (0)