aboutsummaryrefslogtreecommitdiffstats
path: root/lib/private/Files/Template/TemplateManager.php
diff options
context:
space:
mode:
Diffstat (limited to 'lib/private/Files/Template/TemplateManager.php')
-rw-r--r--lib/private/Files/Template/TemplateManager.php134
1 files changed, 97 insertions, 37 deletions
diff --git a/lib/private/Files/Template/TemplateManager.php b/lib/private/Files/Template/TemplateManager.php
index dde224e5ead..80ef5a14786 100644
--- a/lib/private/Files/Template/TemplateManager.php
+++ b/lib/private/Files/Template/TemplateManager.php
@@ -3,27 +3,8 @@
declare(strict_types=1);
/**
- * @copyright Copyright (c) 2021 Julius Härtl <jus@bitgrid.net>
- *
- * @author Christoph Wurst <christoph@winzerhof-wurst.at>
- * @author John Molakvoæ <skjnldsv@protonmail.com>
- * @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/>.
- *
+ * SPDX-FileCopyrightText: 2021 Nextcloud GmbH and Nextcloud contributors
+ * SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace OC\Files\Template;
@@ -31,15 +12,18 @@ use OC\AppFramework\Bootstrap\Coordinator;
use OC\Files\Cache\Scanner;
use OC\Files\Filesystem;
use OCP\EventDispatcher\IEventDispatcher;
-use OCP\Files\Folder;
use OCP\Files\File;
+use OCP\Files\Folder;
use OCP\Files\GenericFileException;
use OCP\Files\IRootFolder;
use OCP\Files\Node;
use OCP\Files\NotFoundException;
+use OCP\Files\Template\BeforeGetTemplatesEvent;
+use OCP\Files\Template\Field;
use OCP\Files\Template\FileCreatedFromTemplateEvent;
use OCP\Files\Template\ICustomTemplateProvider;
use OCP\Files\Template\ITemplateManager;
+use OCP\Files\Template\RegisterTemplateCreatorEvent;
use OCP\Files\Template\Template;
use OCP\Files\Template\TemplateFileCreator;
use OCP\IConfig;
@@ -80,7 +64,7 @@ class TemplateManager implements ITemplateManager {
IPreview $previewManager,
IConfig $config,
IFactory $l10nFactory,
- LoggerInterface $logger
+ LoggerInterface $logger,
) {
$this->serverContainer = $serverContainer;
$this->eventDispatcher = $eventDispatcher;
@@ -119,6 +103,7 @@ class TemplateManager implements ITemplateManager {
if (!empty($this->types)) {
return $this->types;
}
+ $this->eventDispatcher->dispatchTyped(new RegisterTemplateCreatorEvent($this));
foreach ($this->registeredTypes as $registeredType) {
$this->types[] = $registeredType();
}
@@ -134,20 +119,34 @@ class TemplateManager implements ITemplateManager {
}
public function listTemplates(): array {
- return array_map(function (TemplateFileCreator $entry) {
+ return array_values(array_map(function (TemplateFileCreator $entry) {
return array_merge($entry->jsonSerialize(), [
'templates' => $this->getTemplateFiles($entry)
]);
- }, $this->listCreators());
+ }, $this->listCreators()));
+ }
+
+ public function listTemplateFields(int $fileId): array {
+ foreach ($this->listCreators() as $creator) {
+ $fields = $this->getTemplateFields($creator, $fileId);
+ if (empty($fields)) {
+ continue;
+ }
+
+ return $fields;
+ }
+
+ return [];
}
/**
* @param string $filePath
* @param string $templateId
+ * @param array $templateFields
* @return array
* @throws GenericFileException
*/
- public function createFromTemplate(string $filePath, string $templateId = '', string $templateType = 'user'): array {
+ public function createFromTemplate(string $filePath, string $templateId = '', string $templateType = 'user', array $templateFields = []): array {
$userFolder = $this->rootFolder->getUserFolder($this->userId);
try {
$userFolder->get($filePath);
@@ -159,11 +158,9 @@ class TemplateManager implements ITemplateManager {
throw new GenericFileException($this->l10n->t('Invalid path'));
}
$folder = $userFolder->get(dirname($filePath));
- $targetFile = $folder->newFile(basename($filePath));
$template = null;
if ($templateType === 'user' && $templateId !== '') {
$template = $userFolder->get($templateId);
- $template->copy($targetFile->getPath());
} else {
$matchingProvider = array_filter($this->getRegisteredProviders(), function (ICustomTemplateProvider $provider) use ($templateType) {
return $templateType === get_class($provider);
@@ -171,10 +168,12 @@ class TemplateManager implements ITemplateManager {
$provider = array_shift($matchingProvider);
if ($provider) {
$template = $provider->getCustomTemplate($templateId);
- $template->copy($targetFile->getPath());
}
}
- $this->eventDispatcher->dispatchTyped(new FileCreatedFromTemplateEvent($template, $targetFile));
+
+ $targetFile = $folder->newFile(basename($filePath), ($template instanceof File ? $template->fopen('rb') : null));
+
+ $this->eventDispatcher->dispatchTyped(new FileCreatedFromTemplateEvent($template, $targetFile, $templateFields));
return $this->formatFile($userFolder->get($filePath));
} catch (\Exception $e) {
$this->logger->error($e->getMessage(), ['exception' => $e]);
@@ -188,14 +187,34 @@ class TemplateManager implements ITemplateManager {
* @throws \OCP\Files\NotPermittedException
* @throws \OC\User\NoUserException
*/
- private function getTemplateFolder(): Node {
+ private function getTemplateFolder(): Folder {
if ($this->getTemplatePath() !== '') {
- return $this->rootFolder->getUserFolder($this->userId)->get($this->getTemplatePath());
+ $path = $this->rootFolder->getUserFolder($this->userId)->get($this->getTemplatePath());
+ if ($path instanceof Folder) {
+ return $path;
+ }
}
throw new NotFoundException();
}
+ /**
+ * @return list<Template>
+ */
private function getTemplateFiles(TemplateFileCreator $type): array {
+ $templates = array_merge(
+ $this->getProviderTemplates($type),
+ $this->getUserTemplates($type)
+ );
+
+ $this->eventDispatcher->dispatchTyped(new BeforeGetTemplatesEvent($templates, false));
+
+ return $templates;
+ }
+
+ /**
+ * @return list<Template>
+ */
+ private function getProviderTemplates(TemplateFileCreator $type): array {
$templates = [];
foreach ($this->getRegisteredProviders() as $provider) {
foreach ($type->getMimetypes() as $mimetype) {
@@ -204,11 +223,22 @@ class TemplateManager implements ITemplateManager {
}
}
}
+
+ return $templates;
+ }
+
+ /**
+ * @return list<Template>
+ */
+ private function getUserTemplates(TemplateFileCreator $type): array {
+ $templates = [];
+
try {
$userTemplateFolder = $this->getTemplateFolder();
} catch (\Exception $e) {
return $templates;
}
+
foreach ($type->getMimetypes() as $mimetype) {
foreach ($userTemplateFolder->searchByMime($mimetype) as $templateFile) {
$template = new Template(
@@ -224,6 +254,30 @@ class TemplateManager implements ITemplateManager {
return $templates;
}
+ /*
+ * @return list<Field>
+ */
+ private function getTemplateFields(TemplateFileCreator $type, int $fileId): array {
+ $providerTemplates = $this->getProviderTemplates($type);
+ $userTemplates = $this->getUserTemplates($type);
+
+ $matchedTemplates = array_filter(
+ array_merge($providerTemplates, $userTemplates),
+ function (Template $template) use ($fileId) {
+ return $template->jsonSerialize()['fileid'] === $fileId;
+ });
+
+ if (empty($matchedTemplates)) {
+ return [];
+ }
+
+ $this->eventDispatcher->dispatchTyped(new BeforeGetTemplatesEvent($matchedTemplates, true));
+
+ return array_values(array_map(function (Template $template) {
+ return $template->jsonSerialize()['fields'] ?? [];
+ }, $matchedTemplates));
+ }
+
/**
* @param Node|File $file
* @return array
@@ -240,7 +294,8 @@ class TemplateManager implements ITemplateManager {
'mime' => $file->getMimetype(),
'size' => $file->getSize(),
'type' => $file->getType(),
- 'hasPreview' => $this->previewManager->isAvailable($file)
+ 'hasPreview' => $this->previewManager->isAvailable($file),
+ 'permissions' => $file->getPermissions(),
];
}
@@ -261,23 +316,28 @@ class TemplateManager implements ITemplateManager {
return $this->config->getUserValue($this->userId, 'core', 'templateDirectory', '');
}
- public function initializeTemplateDirectory(string $path = null, string $userId = null, $copyTemplates = true): string {
+ public function initializeTemplateDirectory(?string $path = null, ?string $userId = null, $copyTemplates = true): string {
if ($userId !== null) {
$this->userId = $userId;
}
$defaultSkeletonDirectory = \OC::$SERVERROOT . '/core/skeleton';
$defaultTemplateDirectory = \OC::$SERVERROOT . '/core/skeleton/Templates';
- $skeletonPath = $this->config->getSystemValue('skeletondirectory', $defaultSkeletonDirectory);
- $skeletonTemplatePath = $this->config->getSystemValue('templatedirectory', $defaultTemplateDirectory);
+ $skeletonPath = $this->config->getSystemValueString('skeletondirectory', $defaultSkeletonDirectory);
+ $skeletonTemplatePath = $this->config->getSystemValueString('templatedirectory', $defaultTemplateDirectory);
$isDefaultSkeleton = $skeletonPath === $defaultSkeletonDirectory;
$isDefaultTemplates = $skeletonTemplatePath === $defaultTemplateDirectory;
$userLang = $this->l10nFactory->getUserLanguage($this->userManager->get($this->userId));
+ if ($skeletonTemplatePath === '') {
+ $this->setTemplatePath('');
+ return '';
+ }
+
try {
$l10n = $this->l10nFactory->get('lib', $userLang);
$userFolder = $this->rootFolder->getUserFolder($this->userId);
- $userTemplatePath = $path ?? $l10n->t('Templates') . '/';
+ $userTemplatePath = $path ?? $this->config->getAppValue('core', 'defaultTemplateDirectory', $l10n->t('Templates')) . '/';
// Initial user setup without a provided path
if ($path === null) {