aboutsummaryrefslogtreecommitdiffstats
path: root/lib/private/Collaboration
diff options
context:
space:
mode:
authorJulius Härtl <jus@bitgrid.net>2022-08-12 20:17:44 +0200
committerJulius Härtl <jus@bitgrid.net>2022-08-31 16:20:06 +0200
commit0ce0d37ac18456092702a6ed4410ec7e61bdfc07 (patch)
treed0f327f6d9473ef628b155980bcaa0f374827078 /lib/private/Collaboration
parentf58218deeab3a56d5a6a1dfb2047901155daa74d (diff)
downloadnextcloud-server-0ce0d37ac18456092702a6ed4410ec7e61bdfc07.tar.gz
nextcloud-server-0ce0d37ac18456092702a6ed4410ec7e61bdfc07.zip
Implement image caching
Signed-off-by: Julius Härtl <jus@bitgrid.net>
Diffstat (limited to 'lib/private/Collaboration')
-rw-r--r--lib/private/Collaboration/Reference/LinkReferenceProvider.php38
-rw-r--r--lib/private/Collaboration/Reference/Reference.php11
-rw-r--r--lib/private/Collaboration/Reference/ReferenceManager.php9
3 files changed, 55 insertions, 3 deletions
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);