diff options
Diffstat (limited to 'lib/public/Collaboration/Reference/LinkReferenceProvider.php')
-rw-r--r-- | lib/public/Collaboration/Reference/LinkReferenceProvider.php | 70 |
1 files changed, 38 insertions, 32 deletions
diff --git a/lib/public/Collaboration/Reference/LinkReferenceProvider.php b/lib/public/Collaboration/Reference/LinkReferenceProvider.php index d41c1160c7c..65bdcecb577 100644 --- a/lib/public/Collaboration/Reference/LinkReferenceProvider.php +++ b/lib/public/Collaboration/Reference/LinkReferenceProvider.php @@ -2,31 +2,13 @@ declare(strict_types=1); /** - * @copyright Copyright (c) 2022 Julius Härtl <jus@bitgrid.net> - * - * @author Julius Härtl <jus@bitgrid.net> - * @author Anupam Kumar <kyteinsky@gmail.com> - * - * @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/>. + * SPDX-FileCopyrightText: 2022 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-or-later */ namespace OCP\Collaboration\Reference; use Fusonic\OpenGraph\Consumer; -use GuzzleHttp\Exception\GuzzleException; use GuzzleHttp\Psr7\LimitStream; use GuzzleHttp\Psr7\Utils; use OC\Security\RateLimiting\Exception\RateLimitExceededException; @@ -43,7 +25,7 @@ use Psr\Log\LoggerInterface; /** * @since 29.0.0 */ -class LinkReferenceProvider implements IReferenceProvider { +class LinkReferenceProvider implements IReferenceProvider, IPublicReferenceProvider { /** * for image size and webpage header @@ -105,6 +87,14 @@ class LinkReferenceProvider implements IReferenceProvider { } /** + * @inheritDoc + * @since 30.0.0 + */ + public function resolveReferencePublic(string $referenceText, string $sharingToken): ?IReference { + return $this->resolveReference($referenceText); + } + + /** * Populates the reference with OpenGraph data * * @param Reference $reference @@ -124,15 +114,15 @@ class LinkReferenceProvider implements IReferenceProvider { $client = $this->clientService->newClient(); try { - $headResponse = $client->head($reference->getId(), [ 'timeout' => 10 ]); + $headResponse = $client->head($reference->getId(), [ 'timeout' => 3 ]); } catch (\Exception $e) { $this->logger->debug('Failed to perform HEAD request to get target metadata', ['exception' => $e]); return; } $linkContentLength = $headResponse->getHeader('Content-Length'); - if (is_numeric($linkContentLength) && (int) $linkContentLength > self::MAX_CONTENT_LENGTH) { - $this->logger->debug('Skip resolving links pointing to content length > 5 MiB'); + if (is_numeric($linkContentLength) && (int)$linkContentLength > self::MAX_CONTENT_LENGTH) { + $this->logger->debug('[Head] Skip resolving links pointing to content length > 5 MiB'); return; } @@ -146,18 +136,28 @@ class LinkReferenceProvider implements IReferenceProvider { } try { - $response = $client->get($reference->getId(), [ 'timeout' => 10 ]); + $response = $client->get($reference->getId(), [ 'timeout' => 3, 'stream' => true ]); } catch (\Exception $e) { $this->logger->debug('Failed to fetch link for obtaining open graph data', ['exception' => $e]); return; } - $responseBody = (string)$response->getBody(); + $body = $response->getBody(); + if (is_resource($body)) { + $responseContent = fread($body, self::MAX_CONTENT_LENGTH); + if (!feof($body)) { + $this->logger->debug('[Get] Skip resolving links pointing to content length > 5 MiB'); + return; + } + } else { + $this->logger->error('[Get] Impossible to check content length'); + return; + } // OpenGraph handling $consumer = new Consumer(); $consumer->useFallbackMode = true; - $object = $consumer->loadHtml($responseBody); + $object = $consumer->loadHtml($responseContent); $reference->setUrl($reference->getId()); @@ -184,7 +184,7 @@ class LinkReferenceProvider implements IReferenceProvider { $folder = $appData->newFolder('opengraph'); } - $response = $client->get($object->images[0]->url, ['timeout' => 10]); + $response = $client->get($object->images[0]->url, ['timeout' => 3]); $contentType = $response->getHeader('Content-Type'); $contentLength = $response->getHeader('Content-Length'); @@ -195,10 +195,8 @@ class LinkReferenceProvider implements IReferenceProvider { $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]); - } catch (\Throwable $e) { - $this->logger->error('Failed to fetch and store the open graph image for ' . $reference->getId(), ['exception' => $e]); + } catch (\Exception $e) { + $this->logger->debug('Failed to fetch and store the open graph image for ' . $reference->getId(), ['exception' => $e]); } } } @@ -218,4 +216,12 @@ class LinkReferenceProvider implements IReferenceProvider { public function getCacheKey(string $referenceId): ?string { return null; } + + /** + * @inheritDoc + * @since 30.0.0 + */ + public function getCacheKeyPublic(string $referenceId, string $sharingToken): ?string { + return null; + } } |