bookmarks/lib/Service/Previewers/DefaultBookmarkPreviewer.php

95 lines
2.4 KiB
PHP
Raw Normal View History

2018-02-09 22:16:51 +00:00
<?php
2020-09-21 12:25:50 +00:00
/*
* Copyright (c) 2020-2024. The Nextcloud Bookmarks contributors.
2018-02-09 22:16:51 +00:00
*
2020-09-21 12:25:50 +00:00
* This file is licensed under the Affero General Public License version 3 or later. See the COPYING file.
2018-02-09 22:16:51 +00:00
*/
2019-10-26 12:10:49 +00:00
namespace OCA\Bookmarks\Service\Previewers;
2018-02-09 22:16:51 +00:00
2020-09-21 12:17:46 +00:00
use Exception;
2019-10-26 12:10:49 +00:00
use OCA\Bookmarks\Contract\IBookmarkPreviewer;
use OCA\Bookmarks\Contract\IImage;
use OCA\Bookmarks\Db\Bookmark;
use OCA\Bookmarks\Image;
2019-10-26 12:10:49 +00:00
use OCA\Bookmarks\Service\LinkExplorer;
use OCP\Http\Client\IClient;
use OCP\Http\Client\IClientService;
use Psr\Log\LoggerInterface;
2019-10-26 12:10:49 +00:00
class DefaultBookmarkPreviewer implements IBookmarkPreviewer {
public const CACHE_PREFIX = 'bookmarks.DefaultPreviewService';
public const HTTP_TIMEOUT = 10 * 1000;
2018-09-02 16:19:56 +00:00
/** @var IClient */
protected $client;
/** @var LinkExplorer */
protected $linkExplorer;
2018-02-09 22:16:51 +00:00
/** @var LoggerInterface */
2018-11-03 14:54:48 +00:00
private $logger;
2018-02-09 22:16:51 +00:00
/**
* @param LinkExplorer $linkExplorer
2019-10-26 12:10:49 +00:00
* @param IClientService $clientService
* @param LoggerInterface $logger
2018-02-09 22:16:51 +00:00
*/
public function __construct(LinkExplorer $linkExplorer, IClientService $clientService, LoggerInterface $logger) {
$this->linkExplorer = $linkExplorer;
$this->client = $clientService->newClient();
2018-11-03 14:54:48 +00:00
$this->logger = $logger;
2018-02-09 22:16:51 +00:00
}
/**
* @param Bookmark $bookmark
*
* @return Image|null
2018-02-09 22:16:51 +00:00
*/
public function getImage($bookmark): ?IImage {
if (!isset($bookmark)) {
return null;
}
2019-10-26 12:10:49 +00:00
$site = $this->scrapeUrl($bookmark->getUrl());
$this->logger->debug('getImage for URL: ' . $bookmark->getUrl() . ' ' . var_export($site, true), ['app' => 'bookmarks']);
if (isset($site['image']['small'])) {
return $this->fetchImage($site['image']['small']);
}
if (isset($site['image']['large'])) {
return $this->fetchImage($site['image']['large']);
}
return null;
}
public function scrapeUrl($url): array {
return $this->linkExplorer->get($url);
2018-02-09 22:16:51 +00:00
}
2018-02-09 22:16:51 +00:00
/**
* @param $url
* @return Image|null
2018-02-09 22:16:51 +00:00
*/
2020-07-26 16:59:56 +00:00
protected function fetchImage($url): ?Image {
2018-02-09 22:16:51 +00:00
try {
$response = $this->client->get($url, ['timeout' => self::HTTP_TIMEOUT]);
2020-09-21 12:17:46 +00:00
} catch (Exception $e) {
2020-10-11 11:50:30 +00:00
$this->logger->debug($e->getMessage(), ['app' => 'bookmarks']);
return null;
2018-02-09 22:16:51 +00:00
}
$body = $response->getBody();
$contentType = $response->getHeader('Content-Type');
2018-02-09 22:16:51 +00:00
// Some HTPP Error occured :/
2018-11-03 14:43:49 +00:00
if (200 !== $response->getStatusCode()) {
2018-02-09 22:16:51 +00:00
return null;
}
// It's not actually an image, doh.
if (!isset($contentType) || stripos($contentType, 'image') !== 0) {
2018-02-09 22:16:51 +00:00
return null;
}
return new Image($contentType, $body);
2018-02-09 22:16:51 +00:00
}
}