From 0ce0d37ac18456092702a6ed4410ec7e61bdfc07 Mon Sep 17 00:00:00 2001 From: Julius Härtl Date: Fri, 12 Aug 2022 20:17:44 +0200 Subject: Implement image caching MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Julius Härtl --- .../Reference/LinkReferenceProvider.php | 38 ++++++++++++++++++++-- lib/private/Collaboration/Reference/Reference.php | 11 +++++++ .../Collaboration/Reference/ReferenceManager.php | 9 +++++ 3 files changed, 55 insertions(+), 3 deletions(-) (limited to 'lib/private/Collaboration') diff --git a/lib/private/Collaboration/Reference/LinkReferenceProvider.php b/lib/private/Collaboration/Reference/LinkReferenceProvider.php index 0f3ff5f6732..51c26008b83 100644 --- a/lib/private/Collaboration/Reference/LinkReferenceProvider.php +++ b/lib/private/Collaboration/Reference/LinkReferenceProvider.php @@ -28,20 +28,36 @@ use Fusonic\OpenGraph\Consumer; use OC\SystemConfig; use OCP\Collaboration\Reference\IReference; use OCP\Collaboration\Reference\IReferenceProvider; +use OCP\Files\AppData\IAppDataFactory; +use OCP\Files\NotFoundException; use OCP\Http\Client\IClientService; +use OCP\IURLGenerator; use Psr\Log\LoggerInterface; class LinkReferenceProvider implements IReferenceProvider { public const URL_PATTERN = '/(\s|^)(https?:\/\/)?((?:[-A-Z0-9+_]+\.)+[-A-Z]+(?:\/[-A-Z0-9+&@#%?=~_|!:,.;()]*)*)(\s|$)/i'; + public const ALLOWED_CONTENT_TYPES = [ + 'image/png', + 'image/jpg', + 'image/jpeg', + 'image/gif', + 'image/svg+xml', + 'image/webp' + ]; + private IClientService $clientService; private LoggerInterface $logger; private SystemConfig $systemConfig; + private IAppDataFactory $appDataFactory; + private IURLGenerator $urlGenerator; - public function __construct(IClientService $clientService, LoggerInterface $logger, SystemConfig $systemConfig) { + public function __construct(IClientService $clientService, LoggerInterface $logger, SystemConfig $systemConfig, IAppDataFactory $appDataFactory, IURLGenerator $urlGenerator) { $this->clientService = $clientService; $this->logger = $logger; $this->systemConfig = $systemConfig; + $this->appDataFactory = $appDataFactory; + $this->urlGenerator = $urlGenerator; } public function matchReference(string $referenceText): bool { @@ -65,7 +81,7 @@ class LinkReferenceProvider implements IReferenceProvider { private function fetchReference(Reference $reference) { $client = $this->clientService->newClient(); try { - $response = $client->get($reference->getId()); + $response = $client->get($reference->getId(), [ 'timeout' => 10 ]); } catch (\Exception $e) { $this->logger->debug('Failed to fetch link for obtaining open graph data', ['exception' => $e]); return; @@ -88,7 +104,23 @@ class LinkReferenceProvider implements IReferenceProvider { } if ($object->images) { - $reference->setImageUrl($object->images[0]->url); + try { + $appData = $this->appDataFactory->get('core'); + try { + $folder = $appData->getFolder('opengraph'); + } catch (NotFoundException $e) { + $folder = $appData->newFolder('opengraph'); + } + $response = $client->get($object->images[0]->url, [ 'timeout' => 10 ]); + $contentType = $response->getHeader('Content-Type'); + if (in_array($contentType, self::ALLOWED_CONTENT_TYPES, true)) { + $reference->setImageContentType($contentType); + $folder->newFile(md5($reference->getId()), $response->getBody()); + $reference->setImageUrl($this->urlGenerator->linkToRouteAbsolute('core.Reference.preview', ['referenceId' => md5($reference->getId())])); + } + } catch (\Throwable $e) { + $this->logger->error('Failed to fetch and store the open graph image for ' . $reference->getId(), ['exception' => $e]); + } } } diff --git a/lib/private/Collaboration/Reference/Reference.php b/lib/private/Collaboration/Reference/Reference.php index b7cfc00ecef..08b3847e46e 100644 --- a/lib/private/Collaboration/Reference/Reference.php +++ b/lib/private/Collaboration/Reference/Reference.php @@ -33,6 +33,7 @@ class Reference implements \OCP\Collaboration\Reference\IReference, \JsonSeriali private ?string $title = null; private ?string $description = null; private ?string $imageUrl = null; + private ?string $contentType = null; private ?string $url = null; private ?string $richObjectType = null; @@ -74,6 +75,14 @@ class Reference implements \OCP\Collaboration\Reference\IReference, \JsonSeriali return $this->imageUrl; } + public function setImageContentType(?string $contentType): void { + $this->contentType = $contentType; + } + + public function getImageContentType(): ?string { + return $this->contentType; + } + public function setUrl(?string $url): void { $this->url = $url; } @@ -116,6 +125,7 @@ class Reference implements \OCP\Collaboration\Reference\IReference, \JsonSeriali 'id' => $reference->getId(), 'title' => $reference->getTitle(), 'imageUrl' => $reference->getImageUrl(), + 'imageContentType' => $reference->getImageContentType(), 'description' => $reference->getDescription(), 'link' => $reference->getUrl(), 'accessible' => $reference->accessible, @@ -129,6 +139,7 @@ class Reference implements \OCP\Collaboration\Reference\IReference, \JsonSeriali $reference->setTitle($cache['title']); $reference->setDescription($cache['description']); $reference->setImageUrl($cache['imageUrl']); + $reference->setImageContentType($cache['imageContentType']); $reference->setUrl($cache['link']); $reference->setRichObject($cache['richObjectType'], $cache['richObject']); $reference->setAccessible($cache['accessible']); diff --git a/lib/private/Collaboration/Reference/ReferenceManager.php b/lib/private/Collaboration/Reference/ReferenceManager.php index 29084902400..0d156cd8c71 100644 --- a/lib/private/Collaboration/Reference/ReferenceManager.php +++ b/lib/private/Collaboration/Reference/ReferenceManager.php @@ -54,6 +54,15 @@ class ReferenceManager implements IReferenceManager { }, $references); } + public function getReferenceByCacheKey(string $cacheKey): ?IReference { + $cached = $this->cache->get($cacheKey); + if ($cached) { + return Reference::fromCache($cached); + } + + return null; + } + public function resolveReference(string $referenceId): ?IReference { $matchedProvider = $this->getMatchedProvider($referenceId); -- cgit v1.2.3