Skip to content

Commit

Permalink
Merge pull request #38645 from nextcloud/enh/noid/link-ref-provider-l…
Browse files Browse the repository at this point in the history
…ocal-image-uri
  • Loading branch information
juliusknorr authored Jun 9, 2023
2 parents a719b43 + 3621a6b commit 76d4487
Showing 1 changed file with 21 additions and 16 deletions.
37 changes: 21 additions & 16 deletions lib/private/Collaboration/Reference/LinkReferenceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -151,22 +151,27 @@ private function fetchReference(Reference $reference): void {

if ($object->images) {
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');
$contentLength = $response->getHeader('Content-Length');

if (in_array($contentType, self::ALLOWED_CONTENT_TYPES, true) && $contentLength < self::MAX_PREVIEW_SIZE) {
$stream = Utils::streamFor($response->getBody());
$bodyStream = new LimitStream($stream, self::MAX_PREVIEW_SIZE, 0);
$reference->setImageContentType($contentType);
$folder->newFile(md5($reference->getId()), $bodyStream->getContents());
$reference->setImageUrl($this->urlGenerator->linkToRouteAbsolute('core.Reference.preview', ['referenceId' => md5($reference->getId())]));
$host = parse_url($object->images[0]->url, PHP_URL_HOST);
if ($host === false || $host === null) {
$this->logger->warning('Could not detect host of open graph image URI for ' . $reference->getId());
} else {
$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');
$contentLength = $response->getHeader('Content-Length');

if (in_array($contentType, self::ALLOWED_CONTENT_TYPES, true) && $contentLength < self::MAX_PREVIEW_SIZE) {
$stream = Utils::streamFor($response->getBody());
$bodyStream = new LimitStream($stream, self::MAX_PREVIEW_SIZE, 0);
$reference->setImageContentType($contentType);
$folder->newFile(md5($reference->getId()), $bodyStream->getContents());
$reference->setImageUrl($this->urlGenerator->linkToRouteAbsolute('core.Reference.preview', ['referenceId' => md5($reference->getId())]));
}
}
} catch (GuzzleException $e) {
$this->logger->info('Failed to fetch and store the open graph image for ' . $reference->getId(), ['exception' => $e]);
Expand Down

0 comments on commit 76d4487

Please sign in to comment.