diff options
Diffstat (limited to 'lib/private')
-rw-r--r-- | lib/private/AppFramework/Bootstrap/PreviewProviderRegistration.php | 47 | ||||
-rw-r--r-- | lib/private/AppFramework/Bootstrap/RegistrationContext.php | 22 | ||||
-rw-r--r-- | lib/private/PreviewManager.php | 53 | ||||
-rw-r--r-- | lib/private/Server.php | 5 |
4 files changed, 125 insertions, 2 deletions
diff --git a/lib/private/AppFramework/Bootstrap/PreviewProviderRegistration.php b/lib/private/AppFramework/Bootstrap/PreviewProviderRegistration.php new file mode 100644 index 00000000000..47c25d39300 --- /dev/null +++ b/lib/private/AppFramework/Bootstrap/PreviewProviderRegistration.php @@ -0,0 +1,47 @@ +<?php + +declare(strict_types=1); + +/* + * @copyright 2021 Christoph Wurst <christoph@winzerhof-wurst.at> + * + * @author 2021 Christoph Wurst <christoph@winzerhof-wurst.at> + * + * @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\AppFramework\Bootstrap; + +/** + * @psalm-immutable + * @template-extends ServiceRegistration<\OCP\Preview\IProviderV2> + */ +class PreviewProviderRegistration extends ServiceRegistration { + + /** @var string */ + private $mimeTypeRegex; + + public function __construct(string $appId, + string $service, + string $mimeTypeRegex) { + parent::__construct($appId, $service); + $this->mimeTypeRegex = $mimeTypeRegex; + } + + public function getMimeTypeRegex(): string { + return $this->mimeTypeRegex; + } +} diff --git a/lib/private/AppFramework/Bootstrap/RegistrationContext.php b/lib/private/AppFramework/Bootstrap/RegistrationContext.php index c638af94c84..256d3e2346b 100644 --- a/lib/private/AppFramework/Bootstrap/RegistrationContext.php +++ b/lib/private/AppFramework/Bootstrap/RegistrationContext.php @@ -107,6 +107,9 @@ class RegistrationContext { /** @var LoggerInterface */ private $logger; + /** @var PreviewProviderRegistration[] */ + private $previewProviders = []; + public function __construct(LoggerInterface $logger) { $this->logger = $logger; } @@ -235,6 +238,14 @@ class RegistrationContext { ); } + public function registerPreviewProvider(string $previewProviderClass, string $mimeTypeRegex): void { + $this->context->registerPreviewProvider( + $this->appId, + $previewProviderClass, + $mimeTypeRegex + ); + } + public function registerCalendarProvider(string $class): void { $this->context->registerCalendarProvider( $this->appId, @@ -323,6 +334,10 @@ class RegistrationContext { $this->twoFactorProviders[] = new ServiceRegistration($appId, $class); } + public function registerPreviewProvider(string $appId, string $class, string $mimeTypeRegex): void { + $this->previewProviders[] = new PreviewProviderRegistration($appId, $class, $mimeTypeRegex); + } + public function registerCalendarProvider(string $appId, string $class): void { $this->calendarProviders[] = new ServiceRegistration($appId, $class); } @@ -566,6 +581,13 @@ class RegistrationContext { } /** + * @return PreviewProviderRegistration[] + */ + public function getPreviewProviders(): array { + return $this->previewProviders; + } + + /** * @return ServiceRegistration<ICalendarProvider>[] */ public function getCalendarProviders(): array { diff --git a/lib/private/PreviewManager.php b/lib/private/PreviewManager.php index a381ec8c23d..02d017a5017 100644 --- a/lib/private/PreviewManager.php +++ b/lib/private/PreviewManager.php @@ -30,8 +30,10 @@ */ namespace OC; +use OC\AppFramework\Bootstrap\Coordinator; use OC\Preview\Generator; use OC\Preview\GeneratorHelper; +use OCP\AppFramework\QueryException; use OCP\Files\File; use OCP\Files\IAppData; use OCP\Files\IRootFolder; @@ -39,8 +41,10 @@ use OCP\Files\NotFoundException; use OCP\Files\SimpleFS\ISimpleFile; use OCP\IConfig; use OCP\IPreview; +use OCP\IServerContainer; use OCP\Preview\IProviderV2; use Symfony\Component\EventDispatcher\EventDispatcherInterface; +use function array_key_exists; class PreviewManager implements IPreview { /** @var IConfig */ @@ -79,6 +83,20 @@ class PreviewManager implements IPreview { /** @var string */ protected $userId; + /** @var Coordinator */ + private $bootstrapCoordinator; + + /** + * Hash map (without value) of loaded bootstrap providers + * + * @var null[] + * @psalm-var array<string, null> + */ + private $loadedBootstrapProviders = []; + + /** @var IServerContainer */ + private $container; + /** * PreviewManager constructor. * @@ -93,13 +111,17 @@ class PreviewManager implements IPreview { IAppData $appData, EventDispatcherInterface $eventDispatcher, GeneratorHelper $helper, - $userId) { + $userId, + Coordinator $bootstrapCoordinator, + IServerContainer $container) { $this->config = $config; $this->rootFolder = $rootFolder; $this->appData = $appData; $this->eventDispatcher = $eventDispatcher; $this->helper = $helper; $this->userId = $userId; + $this->bootstrapCoordinator = $bootstrapCoordinator; + $this->container = $container; } /** @@ -134,6 +156,7 @@ class PreviewManager implements IPreview { } $this->registerCoreProviders(); + $this->registerBootstrapProviders(); if ($this->providerListDirty) { $keys = array_map('strlen', array_keys($this->providers)); array_multisort($keys, SORT_DESC, $this->providers); @@ -220,6 +243,7 @@ class PreviewManager implements IPreview { } $this->registerCoreProviders(); + $this->registerBootstrapProviders(); $providerMimeTypes = array_keys($this->providers); foreach ($providerMimeTypes as $supportedMimeType) { if (preg_match($supportedMimeType, $mimeType)) { @@ -431,4 +455,31 @@ class PreviewManager implements IPreview { } } } + + private function registerBootstrapProviders(): void { + $context = $this->bootstrapCoordinator->getRegistrationContext(); + + if ($context === null) { + // Just ignore for now + return; + } + + $providers = $context->getPreviewProviders(); + foreach ($providers as $provider) { + $key = $provider->getMimeTypeRegex() . '-' . $provider->getService(); + if (array_key_exists($key, $this->loadedBootstrapProviders)) { + // Do not load the provider more than once + continue; + } + $this->loadedBootstrapProviders[$key] = null; + + $this->registerProvider($provider->getMimeTypeRegex(), function () use ($provider) { + try { + return $this->container->query($provider->getService()); + } catch (QueryException $e) { + return null; + } + }); + } + } } diff --git a/lib/private/Server.php b/lib/private/Server.php index a9ea07c27d3..529e2eacfc3 100644 --- a/lib/private/Server.php +++ b/lib/private/Server.php @@ -58,6 +58,7 @@ use OC\App\AppManager; use OC\App\AppStore\Bundles\BundleFetcher; use OC\App\AppStore\Fetcher\AppFetcher; use OC\App\AppStore\Fetcher\CategoryFetcher; +use OC\AppFramework\Bootstrap\Coordinator; use OC\AppFramework\Http\Request; use OC\AppFramework\Utility\TimeFactory; use OC\Authentication\Events\LoginFailed; @@ -318,7 +319,9 @@ class Server extends ServerContainer implements IServerContainer { ), $c->get(SymfonyAdapter::class), $c->get(GeneratorHelper::class), - $c->get(ISession::class)->get('user_id') + $c->get(ISession::class)->get('user_id'), + $c->get(Coordinator::class), + $c->get(IServerContainer::class) ); }); /** @deprecated 19.0.0 */ |