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 /core/Controller | |
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>
Diffstat (limited to 'core/Controller')
-rw-r--r-- | core/Controller/ReferenceApiController.php | 82 | ||||
-rw-r--r-- | core/Controller/ReferenceController.php | 63 |
2 files changed, 105 insertions, 40 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()); } } |