Skip to content

Commit

Permalink
Merge pull request #2163 from nextcloud/archiveUndefinedFix
Browse files Browse the repository at this point in the history
Rework archive functions to avoid undefinded keys
  • Loading branch information
marcelklehr authored May 29, 2024
2 parents 8647e09 + fb6a337 commit 41ca553
Showing 1 changed file with 51 additions and 42 deletions.
93 changes: 51 additions & 42 deletions lib/Service/CrawlService.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,59 +89,68 @@ public function crawl(Bookmark $bookmark): void {
$this->bookmarkMapper->update($bookmark);
}

private function archiveContent(Bookmark $bookmark, Response $resp) : void {
$header = $resp->getHeader('Content-type');
if(empty($header)) {
private function archiveContent(Bookmark $bookmark, Response $resp): void {
$header = $resp->getHeader('Content-Type');

if (empty($header)) {
return;
}

$contentType = $header[0];
if ((bool)preg_match('#text/html#i', $contentType) === true && ($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());
} catch (\Throwable $e) {
$this->logger->debug(get_class($e)." ".$e->getMessage()."\r\n".$e->getTraceAsString());
return;
$contentType = $header[0] ?? null;

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());
}
}
$bookmark->setHtmlContent($readability->getContent());
$bookmark->setTextContent(strip_tags($readability->getContent()));
}
}

private function archiveFile(Bookmark $bookmark, Response $resp) :void {
$header = $resp->getHeader('Content-type');
if(empty($header)) {
private function archiveFile(Bookmark $bookmark, Response $resp): void {
$header = $resp->getHeader('Content-Type');

if (empty($header)) {
return;
}

$contentType = $header[0];
if ((bool)preg_match('#text/html#i', $contentType) === false && $bookmark->getArchivedFile() === null && (int)$resp->getHeader('Content-length')[0] < 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);
if (!$extension || trim($extension) === '') {
$extension = 'txt';
}
$path = $folderPath . '/' . $name . '.' . $extension;
$i = 0;
while ($userFolder->nodeExists($path)) {
$path = $folderPath . '/' .$name . '_' . $i . '.' . $extension;
$i++;
$contentType = $header[0] ?? null;

if ($contentType !== null && !str_contains($contentType, 'text/html') && $bookmark->getArchivedFile() === null) {
$contentLengthHeader = $resp->getHeader('Content-Length');
$contentLength = isset($contentLengthHeader[0]) ? (int)$contentLengthHeader[0] : 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());
}
$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());
}
}
}
Expand Down

0 comments on commit 41ca553

Please sign in to comment.