-
-
Notifications
You must be signed in to change notification settings - Fork 188
Expand file tree
/
Copy pathCrawlService.php
More file actions
178 lines (158 loc) Β· 5.48 KB
/
Copy pathCrawlService.php
File metadata and controls
178 lines (158 loc) Β· 5.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
<?php
/*
* Copyright (c) 2020-2024. The Nextcloud Bookmarks contributors.
*
* This file is licensed under the Affero General Public License version 3 or later. See the COPYING file.
*/
namespace OCA\Bookmarks\Service;
use Exception;
use fivefilters\Readability\Configuration;
use fivefilters\Readability\Readability;
use Mimey\MimeTypes;
use OC\User\NoUserException;
use OCA\Bookmarks\Db\Bookmark;
use OCA\Bookmarks\Db\BookmarkMapper;
use OCA\Bookmarks\Exception\UrlParseError;
use OCP\Files\Folder;
use OCP\Files\GenericFileException;
use OCP\Files\InvalidPathException;
use OCP\Files\IRootFolder;
use OCP\Files\NotFoundException;
use OCP\Files\NotPermittedException;
use OCP\Http\Client\IClient;
use OCP\Http\Client\IClientService;
use OCP\Http\Client\IResponse;
use OCP\IConfig;
use OCP\IL10N;
use OCP\Lock\LockedException;
use Psr\Log\LoggerInterface;
class CrawlService {
public const MAX_BODY_LENGTH = 92160000; // 90 MB
public const TIMEOUT = 60;
public const CONNECT_TIMEOUT = 30;
public const READ_TIMEOUT = 30;
public const UA_FIREFOX = 'Mozilla/5.0 (X11; Linux x86_64; rv:150.0) Gecko/20100101 Firefox/150.0';
private IClient $client;
private MimeTypes $mimey;
public function __construct(
private BookmarkMapper $bookmarkMapper,
private BookmarkPreviewer $bookmarkPreviewer,
private FaviconPreviewer $faviconPreviewer,
IClientService $clientService,
private IConfig $config,
private IRootFolder $rootFolder,
private IL10N $l,
private LoggerInterface $logger,
private UserSettingsService $userSettingsService,
) {
$this->client = $clientService->newClient();
$this->mimey = new MimeTypes;
}
/**
* @param Bookmark $bookmark
* @throws UrlParseError
*/
public function crawl(Bookmark $bookmark): void {
if (!$bookmark->isWebLink()) {
return;
}
try {
$resp = $this->client->get($bookmark->getUrl(), [
'headers' => [
'User-Agent' => self::UA_FIREFOX,
],
'connect_timeout' => self::CONNECT_TIMEOUT,
'timeout' => self::TIMEOUT,
'read_timeout' => self::READ_TIMEOUT,
'http_errors' => false
]);
$available = $resp ? $resp->getStatusCode() !== 404 : false;
} catch (Exception $e) {
$this->logger->warning($e->getMessage());
$available = false;
}
if ($available) {
$this->userSettingsService->setUserId($bookmark->getUserId());
if ($this->userSettingsService->get('archive.enabled') === 'true') {
$this->archiveFile($bookmark, $resp);
$this->archiveContent($bookmark, $resp);
}
$this->bookmarkPreviewer->getImage($bookmark);
$this->faviconPreviewer->getImage($bookmark);
}
$bookmark->markPreviewCreated();
$bookmark->setAvailable($available);
$this->bookmarkMapper->update($bookmark);
}
private function archiveContent(Bookmark $bookmark, IResponse $resp): void {
$contentType = $resp->getHeader('Content-Type');
if ($contentType === '') {
return;
}
if ($contentType !== null && str_contains($contentType, 'text/html')) {
if ($bookmark->getHtmlContent() === null || $bookmark->getHtmlContent() === '') {
$config = new Configuration();
$config
->setFixRelativeURLs(true)
->setOriginalURL($bookmark->getUrl())
->setSubstituteEntities(true);
$readability = new Readability($config);
try {
$readability->parse($resp->getBody());
$bookmark->setHtmlContent($readability->getContent());
$bookmark->setTextContent(strip_tags($readability->getContent()));
} catch (\Throwable $e) {
$this->logger->debug(get_class($e) . ' ' . $e->getMessage() . "\r\n" . $e->getTraceAsString());
}
}
}
}
private function archiveFile(Bookmark $bookmark, IResponse $resp): void {
$contentType = $resp->getHeader('Content-Type');
if ($contentType === '') {
return;
}
if ($contentType !== null && !str_contains($contentType, 'text/html') && $bookmark->getArchivedFile() === null) {
$contentLengthHeader = $resp->getHeader('Content-Length');
$contentLength = $contentLengthHeader !== '' ? (int)$contentLengthHeader : 0;
if ($contentLength < self::MAX_BODY_LENGTH) {
try {
$userFolder = $this->rootFolder->getUserFolder($bookmark->getUserId());
$folderPath = $this->getArchivePath($bookmark, $userFolder);
$name = $bookmark->slugify('title');
$extension = $this->mimey->getExtension($contentType) ?? 'txt';
$i = 0;
do {
$path = $folderPath . '/' . $name . ($i > 0 ? '_' . $i : '') . '.' . $extension;
$i++;
} while ($userFolder->nodeExists($path));
$file = $userFolder->newFile($path);
$file->putContent($resp->getBody());
$bookmark->setArchivedFile($file->getId());
$this->bookmarkMapper->update($bookmark);
} catch (NotPermittedException|NoUserException|GenericFileException|LockedException|UrlParseError|InvalidPathException|NotFoundException $e) {
$this->logger->debug(get_class($e) . ' ' . $e->getMessage() . "\r\n" . $e->getTraceAsString());
}
}
}
}
private function getArchivePath(Bookmark $bookmark, Folder $userFolder): string {
$folderPath = $this->config->getUserValue($bookmark->getUserId(), 'bookmarks', 'archive.filePath', $this->l->t('Bookmarks'));
$this->getOrCreateFolder($userFolder, $folderPath);
return $folderPath;
}
public function getOrCreateFolder(Folder $userFolder, string $path) : ?Folder {
if ($path === '/') {
return $userFolder;
}
if ($userFolder->nodeExists($path)) {
$folder = $userFolder->get($path);
} else {
$folder = $userFolder->newFolder($path);
}
if (!($folder instanceof Folder)) {
return null;
}
return $folder;
}
}