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 <[email protected]>
  • Loading branch information
juliushaertl committed Apr 29, 2024
1 parent a2c7d26 commit 8da7923
Show file tree
Hide file tree
Showing 4 changed files with 123 additions and 58 deletions.
1 change: 1 addition & 0 deletions composer/composer/autoload_classmap.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
'OCA\\Richdocuments\\Listener\\FileCreatedFromTemplateListener' => $baseDir . '/../lib/Listener/FileCreatedFromTemplateListener.php',
'OCA\\Richdocuments\\Listener\\LoadViewerListener' => $baseDir . '/../lib/Listener/LoadViewerListener.php',
'OCA\\Richdocuments\\Listener\\ReferenceListener' => $baseDir . '/../lib/Listener/ReferenceListener.php',
'OCA\\Richdocuments\\Listener\\RegisterTemplateFileCreatorListener' => $baseDir . '/../lib/Listener/RegisterTemplateFileCreatorListener.php',
'OCA\\Richdocuments\\Listener\\ShareLinkListener' => $baseDir . '/../lib/Listener/ShareLinkListener.php',
'OCA\\Richdocuments\\Middleware\\WOPIMiddleware' => $baseDir . '/../lib/Middleware/WOPIMiddleware.php',
'OCA\\Richdocuments\\Migration\\InstallDefaultFonts' => $baseDir . '/../lib/Migration/InstallDefaultFonts.php',
Expand Down
1 change: 1 addition & 0 deletions composer/composer/autoload_static.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ class ComposerStaticInitRichdocuments
'OCA\\Richdocuments\\Listener\\FileCreatedFromTemplateListener' => __DIR__ . '/..' . '/../lib/Listener/FileCreatedFromTemplateListener.php',
'OCA\\Richdocuments\\Listener\\LoadViewerListener' => __DIR__ . '/..' . '/../lib/Listener/LoadViewerListener.php',
'OCA\\Richdocuments\\Listener\\ReferenceListener' => __DIR__ . '/..' . '/../lib/Listener/ReferenceListener.php',
'OCA\\Richdocuments\\Listener\\RegisterTemplateFileCreatorListener' => __DIR__ . '/..' . '/../lib/Listener/RegisterTemplateFileCreatorListener.php',
'OCA\\Richdocuments\\Listener\\ShareLinkListener' => __DIR__ . '/..' . '/../lib/Listener/ShareLinkListener.php',
'OCA\\Richdocuments\\Middleware\\WOPIMiddleware' => __DIR__ . '/..' . '/../lib/Middleware/WOPIMiddleware.php',
'OCA\\Richdocuments\\Migration\\InstallDefaultFonts' => __DIR__ . '/..' . '/../lib/Migration/InstallDefaultFonts.php',
Expand Down
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);
$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 <[email protected]>
*
* @author Julius Härtl <[email protected]>
*
* @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 {
public function __construct(
private IL10N $l10n,
private IConfig $config,
private IAppManager $appManager,
private CapabilitiesService $capabilitiesService,
private PermissionManager $permissionManager,
) {
}

public function handle(Event $event): void {
if (!$event instanceof RegisterTemplateCreatorEvent) {
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 8da7923

Please sign in to comment.