diff options
author | Julius Härtl <jus@bitgrid.net> | 2022-08-12 20:17:44 +0200 |
---|---|---|
committer | Julius Härtl <jus@bitgrid.net> | 2022-08-31 16:20:06 +0200 |
commit | 0ce0d37ac18456092702a6ed4410ec7e61bdfc07 (patch) | |
tree | d0f327f6d9473ef628b155980bcaa0f374827078 | |
parent | f58218deeab3a56d5a6a1dfb2047901155daa74d (diff) | |
download | nextcloud-server-0ce0d37ac18456092702a6ed4410ec7e61bdfc07.tar.gz nextcloud-server-0ce0d37ac18456092702a6ed4410ec7e61bdfc07.zip |
Implement image caching
Signed-off-by: Julius Härtl <jus@bitgrid.net>
-rw-r--r-- | core/Controller/ReferenceApiController.php | 82 | ||||
-rw-r--r-- | core/Controller/ReferenceController.php | 63 | ||||
-rw-r--r-- | core/routes.php | 5 | ||||
-rw-r--r-- | lib/private/Collaboration/Reference/LinkReferenceProvider.php | 38 | ||||
-rw-r--r-- | lib/private/Collaboration/Reference/Reference.php | 11 | ||||
-rw-r--r-- | lib/private/Collaboration/Reference/ReferenceManager.php | 9 |
6 files changed, 163 insertions, 45 deletions
diff --git a/core/Controller/ReferenceApiController.php b/core/Controller/ReferenceApiController.php new file mode 100644 index 00000000000..d9af5520cfc --- /dev/null +++ b/core/Controller/ReferenceApiController.php @@ -0,0 +1,82 @@ +<?php + +declare(strict_types=1); +/** + * @copyright Copyright (c) 2022 Julius Härtl <jus@bitgrid.net> + * + * @author Julius Härtl <jus@bitgrid.net> + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +namespace OC\Core\Controller; + +use OCP\AppFramework\Http\DataResponse; +use OC\Collaboration\Reference\ReferenceManager; +use OCP\IRequest; + +class ReferenceApiController extends \OCP\AppFramework\OCSController { + private ReferenceManager $referenceManager; + + public function __construct($appName, IRequest $request, ReferenceManager $referenceManager) { + parent::__construct($appName, $request); + $this->referenceManager = $referenceManager; + } + + /** + * @NoAdminRequired + * + * @param string $text + * @param bool $resolve + * @return DataResponse + */ + public function extract(string $text, bool $resolve = false, int $limit = 1): DataResponse { + $references = $this->referenceManager->extractReferences($text); + + $result = []; + $index = 0; + foreach ($references as $reference) { + if ($index++ < $limit) { + $result[$reference] = $resolve ? $this->referenceManager->resolveReference($reference) : null; + } + } + + return new DataResponse([ + 'references' => $result + ]); + } + + + /** + * @NoAdminRequired + * + * @param array $references + * @return DataResponse + */ + public function resolve(array $references, int $limit = 1): DataResponse { + $result = []; + $index = 0; + foreach ($references as $reference) { + if ($index++ < $limit) { + $result[$reference] = $this->referenceManager->resolveReference($reference); + } + } + + return new DataResponse([ + 'references' => array_filter($result) + ]); + } +} diff --git a/core/Controller/ReferenceController.php b/core/Controller/ReferenceController.php index 1d61bb0f772..dda213db54d 100644 --- a/core/Controller/ReferenceController.php +++ b/core/Controller/ReferenceController.php @@ -24,59 +24,42 @@ declare(strict_types=1); namespace OC\Core\Controller; -use OCP\AppFramework\Http\DataResponse; use OC\Collaboration\Reference\ReferenceManager; +use OCP\AppFramework\Http; +use OCP\AppFramework\Http\DataDownloadResponse; +use OCP\AppFramework\Http\DataResponse; +use OCP\Files\AppData\IAppDataFactory; +use OCP\Files\NotFoundException; use OCP\IRequest; -class ReferenceController extends \OCP\AppFramework\OCSController { +class ReferenceController extends \OCP\AppFramework\Controller { private ReferenceManager $referenceManager; - public function __construct($appName, IRequest $request, ReferenceManager $referenceManager) { + public function __construct($appName, IRequest $request, ReferenceManager $referenceManager, IAppDataFactory $appDataFactory) { parent::__construct($appName, $request); $this->referenceManager = $referenceManager; + $this->appDataFactory = $appDataFactory; } /** - * @NoAdminRequired - * - * @param string $text - * @param bool $resolve - * @return DataResponse + * @PublicPage + * @NoCSRFRequired + * @param $referenceId + * @throws \OCP\Files\NotFoundException */ - public function extract(string $text, bool $resolve = false, int $limit = 1): DataResponse { - $references = $this->referenceManager->extractReferences($text); - - $result = []; - $index = 0; - foreach ($references as $reference) { - if ($index++ < $limit) { - $result[$reference] = $resolve ? $this->referenceManager->resolveReference($reference) : null; - } + public function preview($referenceId) { + $reference = $this->referenceManager->getReferenceByCacheKey($referenceId); + if ($reference === null) { + return new DataResponse('', Http::STATUS_NOT_FOUND); } - return new DataResponse([ - 'references' => $result - ]); - } - - - /** - * @NoAdminRequired - * - * @param array $references - * @return DataResponse - */ - public function resolve(array $references, int $limit = 1): DataResponse { - $result = []; - $index = 0; - foreach ($references as $reference) { - if ($index++ < $limit) { - $result[$reference] = $this->referenceManager->resolveReference($reference); - } + try { + $appData = $this->appDataFactory->get('core'); + $folder = $appData->getFolder('opengraph'); + $file = $folder->getFile($referenceId); + } catch (NotFoundException $e) { + return new DataResponse('', Http::STATUS_NOT_FOUND); } - - return new DataResponse([ - 'references' => array_filter($result) - ]); + return new DataDownloadResponse($file->getContent(), $referenceId, $reference->getImageContentType()); } } diff --git a/core/routes.php b/core/routes.php index aef121591e3..b75cb0f6b3b 100644 --- a/core/routes.php +++ b/core/routes.php @@ -79,6 +79,7 @@ $application->registerRoutes($this, [ ['name' => 'Preview#getPreviewByFileId', 'url' => '/core/preview', 'verb' => 'GET'], ['name' => 'Preview#getPreview', 'url' => '/core/preview.png', 'verb' => 'GET'], ['name' => 'RecommendedApps#index', 'url' => '/core/apps/recommended', 'verb' => 'GET'], + ['name' => 'Reference#preview', 'url' => '/core/references/preview/{referenceId}', 'verb' => 'GET'], ['name' => 'Css#getCss', 'url' => '/css/{appName}/{fileName}', 'verb' => 'GET'], ['name' => 'Js#getJs', 'url' => '/js/{appName}/{fileName}', 'verb' => 'GET'], ['name' => 'contactsMenu#index', 'url' => '/contactsmenu/contacts', 'verb' => 'POST'], @@ -120,8 +121,8 @@ $application->registerRoutes($this, [ ['root' => '/collaboration', 'name' => 'CollaborationResources#getCollectionsByResource', 'url' => '/resources/{resourceType}/{resourceId}', 'verb' => 'GET'], ['root' => '/collaboration', 'name' => 'CollaborationResources#createCollectionOnResource', 'url' => '/resources/{baseResourceType}/{baseResourceId}', 'verb' => 'POST'], - ['root' => '/references', 'name' => 'Reference#extract', 'url' => '/extract', 'verb' => 'POST'], - ['root' => '/references', 'name' => 'Reference#resolve', 'url' => '/resolve', 'verb' => 'POST'], + ['root' => '/references', 'name' => 'ReferenceApi#extract', 'url' => '/extract', 'verb' => 'POST'], + ['root' => '/references', 'name' => 'ReferenceApi#resolve', 'url' => '/resolve', 'verb' => 'POST'], ['root' => '/profile', 'name' => 'ProfileApi#setVisibility', 'url' => '/{targetUserId}', 'verb' => 'PUT'], 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); |