diff options
author | Julius Härtl <jus@bitgrid.net> | 2022-08-31 20:12:34 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-08-31 20:12:34 +0200 |
commit | b3eb0bfe05f6626cb02fe09d6b4f144f4b7dbc8e (patch) | |
tree | 97edf7d4275cfb3bc030df366a4adc2955bab1e7 /core | |
parent | 4209dc7d66d192f49ab8dee305015491d7a32089 (diff) | |
parent | 1ab66988bc6e5dca0b0b18ad9366880124fb28e1 (diff) | |
download | nextcloud-server-b3eb0bfe05f6626cb02fe09d6b4f144f4b7dbc8e.tar.gz nextcloud-server-b3eb0bfe05f6626cb02fe09d6b4f144f4b7dbc8e.zip |
Merge pull request #33494 from nextcloud/enh/references
Backend for reference metadata fetching
Diffstat (limited to 'core')
-rw-r--r-- | core/Controller/ReferenceApiController.php | 81 | ||||
-rw-r--r-- | core/Controller/ReferenceController.php | 67 | ||||
-rw-r--r-- | core/routes.php | 4 | ||||
-rw-r--r-- | core/src/OCP/comments.js | 2 |
4 files changed, 154 insertions, 0 deletions
diff --git a/core/Controller/ReferenceApiController.php b/core/Controller/ReferenceApiController.php new file mode 100644 index 00000000000..3ecd9917b23 --- /dev/null +++ b/core/Controller/ReferenceApiController.php @@ -0,0 +1,81 @@ +<?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 OCP\Collaboration\Reference\IReferenceManager; +use OCP\IRequest; + +class ReferenceApiController extends \OCP\AppFramework\OCSController { + private IReferenceManager $referenceManager; + + public function __construct(string $appName, IRequest $request, IReferenceManager $referenceManager) { + parent::__construct($appName, $request); + $this->referenceManager = $referenceManager; + } + + /** + * @NoAdminRequired + */ + 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) { + break; + } + + $result[$reference] = $resolve ? $this->referenceManager->resolveReference($reference) : null; + } + + return new DataResponse([ + 'references' => $result + ]); + } + + + /** + * @NoAdminRequired + * + * @param string[] $references + */ + public function resolve(array $references, int $limit = 1): DataResponse { + $result = []; + $index = 0; + foreach ($references as $reference) { + if ($index++ >= $limit) { + break; + } + + $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 new file mode 100644 index 00000000000..2f6ef152d01 --- /dev/null +++ b/core/Controller/ReferenceController.php @@ -0,0 +1,67 @@ +<?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\Response; +use OCP\Collaboration\Reference\IReferenceManager; +use OCP\AppFramework\Controller; +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\Files\NotPermittedException; +use OCP\IRequest; + +class ReferenceController extends Controller { + private IReferenceManager $referenceManager; + private IAppDataFactory $appDataFactory; + + public function __construct(string $appName, IRequest $request, IReferenceManager $referenceManager, IAppDataFactory $appDataFactory) { + parent::__construct($appName, $request); + $this->referenceManager = $referenceManager; + $this->appDataFactory = $appDataFactory; + } + + /** + * @PublicPage + * @NoCSRFRequired + */ + public function preview(string $referenceId): Response { + $reference = $this->referenceManager->getReferenceByCacheKey($referenceId); + if ($reference === null) { + return new DataResponse('', Http::STATUS_NOT_FOUND); + } + + try { + $appData = $this->appDataFactory->get('core'); + $folder = $appData->getFolder('opengraph'); + $file = $folder->getFile($referenceId); + return new DataDownloadResponse($file->getContent(), $referenceId, $reference->getImageContentType()); + } catch (NotFoundException|NotPermittedException $e) { + return new DataResponse('', Http::STATUS_NOT_FOUND); + } + } +} diff --git a/core/routes.php b/core/routes.php index bfc614935e1..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,6 +121,9 @@ $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' => 'ReferenceApi#extract', 'url' => '/extract', 'verb' => 'POST'], + ['root' => '/references', 'name' => 'ReferenceApi#resolve', 'url' => '/resolve', 'verb' => 'POST'], + ['root' => '/profile', 'name' => 'ProfileApi#setVisibility', 'url' => '/{targetUserId}', 'verb' => 'PUT'], // Unified search diff --git a/core/src/OCP/comments.js b/core/src/OCP/comments.js index ed7ac67a073..0b44dd7be81 100644 --- a/core/src/OCP/comments.js +++ b/core/src/OCP/comments.js @@ -31,6 +31,8 @@ import $ from 'jquery' * * The downside: anything not ascii is excluded. Not sure how common it is in areas using different * alphabets… the upside: fake domains with similar looking characters won't be formatted as links + * + * This is a copy of the backend regex in IURLGenerator, make sure to adjust both when changing */ const urlRegex = /(\s|^)(https?:\/\/)?((?:[-A-Z0-9+_]+\.)+[-A-Z]+(?:\/[-A-Z0-9+&@#%?=~_|!:,.;()]*)*)(\s|$)/ig |