Skip to content

Commit

Permalink
perf(bootstrap): Lazy register template creator through event
Browse files Browse the repository at this point in the history
Signed-off-by: Julius Härtl <jus@bitgrid.net>
  • Loading branch information
juliushaertl committed Apr 22, 2024
1 parent 28690d7 commit a44bd6f
Show file tree
Hide file tree
Showing 2 changed files with 121 additions and 58 deletions.
61 changes: 3 additions & 58 deletions lib/AppInfo/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
use OCA\Richdocuments\Listener\FileCreatedFromTemplateListener;
use OCA\Richdocuments\Listener\LoadViewerListener;
use OCA\Richdocuments\Listener\ReferenceListener;
use OCA\Richdocuments\Listener\RegisterTemplateFileCreatorListener;
use OCA\Richdocuments\Listener\ShareLinkListener;
use OCA\Richdocuments\Middleware\WOPIMiddleware;
use OCA\Richdocuments\PermissionManager;
Expand All @@ -58,6 +59,7 @@
use OCP\Collaboration\Reference\RenderReferenceEvent;
use OCP\Files\Template\FileCreatedFromTemplateEvent;
use OCP\Files\Template\ITemplateManager;
use OCP\Files\Template\RegisterTemplateCreatorEvent;
use OCP\Files\Template\TemplateFileCreator;
use OCP\IConfig;
use OCP\IL10N;
Expand All @@ -78,6 +80,7 @@ public function register(IRegistrationContext $context): void {
$context->registerTemplateProvider(CollaboraTemplateProvider::class);
$context->registerCapability(Capabilities::class);
$context->registerMiddleWare(WOPIMiddleware::class);
$context->registerEventListener(RegisterTemplateCreatorEvent::class, RegisterTemplateFileCreatorListener::class);

Check failure on line 83 in lib/AppInfo/Application.php

View workflow job for this annotation

GitHub Actions / static-psalm-analysis

UndefinedClass

lib/AppInfo/Application.php:83:35: UndefinedClass: Class, interface or enum named OCP\Files\Template\RegisterTemplateCreatorEvent does not exist (see https://psalm.dev/019)

Check failure on line 83 in lib/AppInfo/Application.php

View workflow job for this annotation

GitHub Actions / static-psalm-analysis

InvalidArgument

lib/AppInfo/Application.php:83:72: InvalidArgument: Argument 2 of OCP\AppFramework\Bootstrap\IRegistrationContext::registerEventListener expects class-string<OCP\EventDispatcher\IEventListener<OCP\EventDispatcher\Event>>, but OCA\Richdocuments\Listener\RegisterTemplateFileCreatorListener::class provided (see https://psalm.dev/004)
$context->registerEventListener(FileCreatedFromTemplateEvent::class, FileCreatedFromTemplateListener::class);
$context->registerEventListener(AddContentSecurityPolicyEvent::class, AddContentSecurityPolicyListener::class);
$context->registerEventListener(AddFeaturePolicyEvent::class, AddFeaturePolicyListener::class);
Expand All @@ -101,64 +104,6 @@ public function register(IRegistrationContext $context): void {
}

public function boot(IBootContext $context): void {
$context->injectFn(function (ITemplateManager $templateManager, IL10N $l10n, IConfig $config, CapabilitiesService $capabilitiesService, PermissionManager $permissionManager, IAppManager $appManager) {
if (!$permissionManager->isEnabledForUser() || empty($capabilitiesService->getCapabilities())) {
return;
}
$ooxml = $config->getAppValue(self::APPNAME, 'doc_format', '') === 'ooxml';
$templateManager->registerTemplateFileCreator(function () use ($l10n, $ooxml, $appManager) {
$odtType = new TemplateFileCreator('richdocuments', $l10n->t('New document'), ($ooxml ? '.docx' : '.odt'));
if ($ooxml) {
$odtType->addMimetype('application/msword');
$odtType->addMimetype('application/vnd.openxmlformats-officedocument.wordprocessingml.document');
} else {
$odtType->addMimetype('application/vnd.oasis.opendocument.text');
$odtType->addMimetype('application/vnd.oasis.opendocument.text-template');
}
$odtType->setIconSvgInline(file_get_contents($appManager->getAppPath('richdocuments') . '/img/x-office-document.svg'));
$odtType->setRatio(21 / 29.7);
return $odtType;
});
$templateManager->registerTemplateFileCreator(function () use ($l10n, $ooxml, $appManager) {
$odsType = new TemplateFileCreator('richdocuments', $l10n->t('New spreadsheet'), ($ooxml ? '.xlsx' : '.ods'));
if ($ooxml) {
$odsType->addMimetype('application/vnd.ms-excel');
$odsType->addMimetype('application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
} else {
$odsType->addMimetype('application/vnd.oasis.opendocument.spreadsheet');
$odsType->addMimetype('application/vnd.oasis.opendocument.spreadsheet-template');
}
$odsType->setIconSvgInline(file_get_contents($appManager->getAppPath('richdocuments') . '/img/x-office-spreadsheet.svg'));
$odsType->setRatio(16 / 9);
return $odsType;
});
$templateManager->registerTemplateFileCreator(function () use ($l10n, $ooxml, $appManager) {
$odpType = new TemplateFileCreator('richdocuments', $l10n->t('New presentation'), ($ooxml ? '.pptx' : '.odp'));
if ($ooxml) {
$odpType->addMimetype('application/vnd.ms-powerpoint');
$odpType->addMimetype('application/vnd.openxmlformats-officedocument.presentationml.presentation');
} else {
$odpType->addMimetype('application/vnd.oasis.opendocument.presentation');
$odpType->addMimetype('application/vnd.oasis.opendocument.presentation-template');
}
$odpType->setIconSvgInline(file_get_contents($appManager->getAppPath('richdocuments') . '/img/x-office-presentation.svg'));
$odpType->setRatio(16 / 9);
return $odpType;
});

if (!$capabilitiesService->hasDrawSupport()) {
return;
}
$templateManager->registerTemplateFileCreator(function () use ($l10n, $ooxml, $appManager) {
$odpType = new TemplateFileCreator('richdocuments', $l10n->t('New diagram'), '.odg');
$odpType->addMimetype('application/vnd.oasis.opendocument.graphics');
$odpType->addMimetype('application/vnd.oasis.opendocument.graphics-template');
$odpType->setIconSvgInline(file_get_contents($appManager->getAppPath('richdocuments') . '/img/x-office-drawing.svg'));
$odpType->setRatio(1);
return $odpType;
});
});

$this->checkAndEnableCODEServer();
}

Expand Down
118 changes: 118 additions & 0 deletions lib/Listener/RegisterTemplateFileCreatorListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
<?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 OCA\Richdocuments\Listener;

use OCA\Richdocuments\AppConfig;
use OCA\Richdocuments\AppInfo\Application;
use OCA\Richdocuments\PermissionManager;
use OCA\Richdocuments\Service\CapabilitiesService;
use OCP\App\IAppManager;
use OCP\AppFramework\Http\FeaturePolicy;
use OCP\EventDispatcher\Event;
use OCP\EventDispatcher\IEventListener;
use OCP\Files\Template\RegisterTemplateCreatorEvent;
use OCP\Files\Template\TemplateFileCreator;
use OCP\IConfig;
use OCP\IL10N;
use OCP\IRequest;
use OCP\Security\FeaturePolicy\AddFeaturePolicyEvent;

/** @template-implements IEventListener<Event|RegisterTemplateCreatorEvent> */
class RegisterTemplateFileCreatorListener implements IEventListener {

Check failure on line 43 in lib/Listener/RegisterTemplateFileCreatorListener.php

View workflow job for this annotation

GitHub Actions / static-psalm-analysis

UndefinedDocblockClass

lib/Listener/RegisterTemplateFileCreatorListener.php:43:7: UndefinedDocblockClass: Docblock-defined class, interface or enum named OCP\Files\Template\RegisterTemplateCreatorEvent does not exist (see https://psalm.dev/200)

Check failure on line 43 in lib/Listener/RegisterTemplateFileCreatorListener.php

View workflow job for this annotation

GitHub Actions / static-psalm-analysis

InvalidTemplateParam

lib/Listener/RegisterTemplateFileCreatorListener.php:43:54: InvalidTemplateParam: Extended template param T expects type OCP\EventDispatcher\Event, type OCP\EventDispatcher\Event|OCP\Files\Template\RegisterTemplateCreatorEvent given (see https://psalm.dev/183)
public function __construct(
private IL10N $l10n,
private IConfig $config,
private IAppManager $appManager,
private CapabilitiesService $capabilitiesService,
private PermissionManager $permissionManager,
) {
}

public function handle(Event $event): void {

Check failure on line 53 in lib/Listener/RegisterTemplateFileCreatorListener.php

View workflow job for this annotation

GitHub Actions / static-psalm-analysis

MoreSpecificImplementedParamType

lib/Listener/RegisterTemplateFileCreatorListener.php:53:31: MoreSpecificImplementedParamType: Argument 1 of OCA\Richdocuments\Listener\RegisterTemplateFileCreatorListener::handle has the more specific type 'OCP\EventDispatcher\Event', expecting 'OCP\EventDispatcher\Event|OCP\Files\Template\RegisterTemplateCreatorEvent' as defined by OCP\EventDispatcher\IEventListener::handle (see https://psalm.dev/140)
if (!$event instanceof RegisterTemplateCreatorEvent) {

Check failure on line 54 in lib/Listener/RegisterTemplateFileCreatorListener.php

View workflow job for this annotation

GitHub Actions / static-psalm-analysis

UndefinedClass

lib/Listener/RegisterTemplateFileCreatorListener.php:54:26: UndefinedClass: Class, interface or enum named OCP\Files\Template\RegisterTemplateCreatorEvent does not exist (see https://psalm.dev/019)
return;
}

if (!$this->permissionManager->isEnabledForUser() || empty($this->capabilitiesService->getCapabilities())) {
return;
}

$templateManager = $event->getTemplateManager();
$ooxml = $this->config->getAppValue(Application::APPNAME, 'doc_format', '') === 'ooxml';
$appPath = $this->appManager->getAppPath('richdocuments');

$templateManager->registerTemplateFileCreator(function () use ($ooxml, $appPath) {
$odtType = new TemplateFileCreator('richdocuments', $this->l10n->t('New document'), ($ooxml ? '.docx' : '.odt'));
if ($ooxml) {
$odtType->addMimetype('application/msword');
$odtType->addMimetype('application/vnd.openxmlformats-officedocument.wordprocessingml.document');
} else {
$odtType->addMimetype('application/vnd.oasis.opendocument.text');
$odtType->addMimetype('application/vnd.oasis.opendocument.text-template');
}
$odtType->setIconSvgInline(file_get_contents($appPath . '/img/x-office-document.svg'));
$odtType->setRatio(21 / 29.7);
return $odtType;
});
$templateManager->registerTemplateFileCreator(function () use ($ooxml, $appPath) {
$odsType = new TemplateFileCreator('richdocuments', $this->l10n->t('New spreadsheet'), ($ooxml ? '.xlsx' : '.ods'));
if ($ooxml) {
$odsType->addMimetype('application/vnd.ms-excel');
$odsType->addMimetype('application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
} else {
$odsType->addMimetype('application/vnd.oasis.opendocument.spreadsheet');
$odsType->addMimetype('application/vnd.oasis.opendocument.spreadsheet-template');
}
$odsType->setIconSvgInline(file_get_contents($appPath . '/img/x-office-spreadsheet.svg'));
$odsType->setRatio(16 / 9);
return $odsType;
});
$templateManager->registerTemplateFileCreator(function () use ($ooxml, $appPath) {
$odpType = new TemplateFileCreator('richdocuments', $this->l10n->t('New presentation'), ($ooxml ? '.pptx' : '.odp'));
if ($ooxml) {
$odpType->addMimetype('application/vnd.ms-powerpoint');
$odpType->addMimetype('application/vnd.openxmlformats-officedocument.presentationml.presentation');
} else {
$odpType->addMimetype('application/vnd.oasis.opendocument.presentation');
$odpType->addMimetype('application/vnd.oasis.opendocument.presentation-template');
}
$odpType->setIconSvgInline(file_get_contents($appPath . '/img/x-office-presentation.svg'));
$odpType->setRatio(16 / 9);
return $odpType;
});

if (!$this->capabilitiesService->hasDrawSupport()) {
return;
}
$templateManager->registerTemplateFileCreator(function () use ($ooxml, $appPath) {
$odpType = new TemplateFileCreator('richdocuments', $this->l10n->t('New diagram'), '.odg');
$odpType->addMimetype('application/vnd.oasis.opendocument.graphics');
$odpType->addMimetype('application/vnd.oasis.opendocument.graphics-template');
$odpType->setIconSvgInline(file_get_contents($appPath . '/img/x-office-drawing.svg'));
$odpType->setRatio(1);
return $odpType;
});
}
}

0 comments on commit a44bd6f

Please sign in to comment.