From 0ce0d37ac18456092702a6ed4410ec7e61bdfc07 Mon Sep 17 00:00:00 2001 From: Julius Härtl Date: Fri, 12 Aug 2022 20:17:44 +0200 Subject: Implement image caching MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Julius Härtl --- core/Controller/ReferenceApiController.php | 82 ++++++++++++++++++++++++++++++ core/Controller/ReferenceController.php | 63 +++++++++-------------- 2 files changed, 105 insertions(+), 40 deletions(-) create mode 100644 core/Controller/ReferenceApiController.php (limited to 'core/Controller') 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 @@ + + * + * @author Julius Härtl + * + * @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 . + */ + +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()); } } -- cgit v1.2.3