2018-02-09 22:16:51 +00:00
|
|
|
<?php
|
2020-09-21 12:25:50 +00:00
|
|
|
/*
|
2024-02-02 18:27: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-12-28 16:41:31 +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;
|
2020-04-05 17:05:48 +00:00
|
|
|
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;
|
2019-12-28 16:41:31 +00:00
|
|
|
use OCP\Http\Client\IClient;
|
|
|
|
use OCP\Http\Client\IClientService;
|
2020-10-09 11:25:17 +00:00
|
|
|
use Psr\Log\LoggerInterface;
|
|
|
|
|
2019-10-26 12:10:49 +00:00
|
|
|
class DefaultBookmarkPreviewer implements IBookmarkPreviewer {
|
2020-08-02 14:05:52 +00:00
|
|
|
public const CACHE_PREFIX = 'bookmarks.DefaultPreviewService';
|
|
|
|
public const HTTP_TIMEOUT = 10 * 1000;
|
2018-07-28 16:08:48 +00:00
|
|
|
|
2018-09-02 16:19:56 +00:00
|
|
|
/** @var IClient */
|
|
|
|
protected $client;
|
|
|
|
|
2018-07-20 21:37:58 +00:00
|
|
|
/** @var LinkExplorer */
|
|
|
|
protected $linkExplorer;
|
2018-02-09 22:16:51 +00:00
|
|
|
|
2020-10-09 11:25:17 +00:00
|
|
|
/** @var LoggerInterface */
|
2018-11-03 14:54:48 +00:00
|
|
|
private $logger;
|
|
|
|
|
2018-02-09 22:16:51 +00:00
|
|
|
/**
|
2018-07-20 21:37:58 +00:00
|
|
|
* @param LinkExplorer $linkExplorer
|
2019-10-26 12:10:49 +00:00
|
|
|
* @param IClientService $clientService
|
2020-10-09 11:25:17 +00:00
|
|
|
* @param LoggerInterface $logger
|
2018-02-09 22:16:51 +00:00
|
|
|
*/
|
2020-10-09 11:25:17 +00:00
|
|
|
public function __construct(LinkExplorer $linkExplorer, IClientService $clientService, LoggerInterface $logger) {
|
2018-07-20 21:37:58 +00:00
|
|
|
$this->linkExplorer = $linkExplorer;
|
2018-09-01 21:12:31 +00:00
|
|
|
$this->client = $clientService->newClient();
|
2018-11-03 14:54:48 +00:00
|
|
|
$this->logger = $logger;
|
2018-02-09 22:16:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-04-05 17:05:48 +00:00
|
|
|
* @param Bookmark $bookmark
|
2021-01-20 14:42:41 +00:00
|
|
|
*
|
|
|
|
* @return Image|null
|
2018-02-09 22:16:51 +00:00
|
|
|
*/
|
2020-04-05 17:05:48 +00:00
|
|
|
public function getImage($bookmark): ?IImage {
|
2018-07-20 21:37:58 +00:00
|
|
|
if (!isset($bookmark)) {
|
|
|
|
return null;
|
|
|
|
}
|
2019-10-26 12:10:49 +00:00
|
|
|
$site = $this->scrapeUrl($bookmark->getUrl());
|
2019-12-28 16:41:31 +00:00
|
|
|
$this->logger->debug('getImage for URL: ' . $bookmark->getUrl() . ' ' . var_export($site, true), ['app' => 'bookmarks']);
|
2018-09-01 21:12:31 +00:00
|
|
|
if (isset($site['image']['small'])) {
|
2020-07-25 12:08:44 +00:00
|
|
|
return $this->fetchImage($site['image']['small']);
|
2018-07-20 21:37:58 +00:00
|
|
|
}
|
2018-09-01 21:12:31 +00:00
|
|
|
if (isset($site['image']['large'])) {
|
2020-07-25 12:08:44 +00:00
|
|
|
return $this->fetchImage($site['image']['large']);
|
2018-09-01 21:12:31 +00:00
|
|
|
}
|
2019-09-12 23:55:49 +00:00
|
|
|
return null;
|
2018-07-20 21:37:58 +00:00
|
|
|
}
|
|
|
|
|
2020-10-23 10:20:50 +00:00
|
|
|
public function scrapeUrl($url): array {
|
2020-07-25 12:08:44 +00:00
|
|
|
return $this->linkExplorer->get($url);
|
2018-02-09 22:16:51 +00:00
|
|
|
}
|
2018-07-20 21:37:58 +00:00
|
|
|
|
2018-02-09 22:16:51 +00:00
|
|
|
/**
|
2020-04-05 17:05:48 +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 {
|
2018-09-01 21:12:31 +00:00
|
|
|
$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']);
|
2018-09-01 21:12:31 +00:00
|
|
|
return null;
|
2018-02-09 22:16:51 +00:00
|
|
|
}
|
2018-09-01 21:12:31 +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.
|
2018-09-01 21:12:31 +00:00
|
|
|
if (!isset($contentType) || stripos($contentType, 'image') !== 0) {
|
2018-02-09 22:16:51 +00:00
|
|
|
return null;
|
|
|
|
}
|
2018-07-20 21:37:58 +00:00
|
|
|
|
2020-04-05 17:05:48 +00:00
|
|
|
return new Image($contentType, $body);
|
2018-02-09 22:16:51 +00:00
|
|
|
}
|
|
|
|
}
|