summaryrefslogtreecommitdiffstats
path: root/lib/private
diff options
context:
space:
mode:
authorJulius Härtl <jus@bitgrid.net>2021-01-12 11:28:04 +0100
committerJulius Härtl <jus@bitgrid.net>2021-01-28 11:59:46 +0100
commit497440477492b6e7df8ca1eb6c79eb7100a2fe24 (patch)
tree66050dc6e96bb1f7c78fb0729761c017410d5636 /lib/private
parent7e7284d790f55a14ec100771c08809e32514c533 (diff)
downloadnextcloud-server-497440477492b6e7df8ca1eb6c79eb7100a2fe24.tar.gz
nextcloud-server-497440477492b6e7df8ca1eb6c79eb7100a2fe24.zip
files: Create files from template API
Signed-off-by: Julius Härtl <jus@bitgrid.net>
Diffstat (limited to 'lib/private')
-rw-r--r--lib/private/Files/Template/TemplateManager.php237
-rw-r--r--lib/private/Server.php3
-rw-r--r--lib/private/legacy/OC_Util.php3
3 files changed, 243 insertions, 0 deletions
diff --git a/lib/private/Files/Template/TemplateManager.php b/lib/private/Files/Template/TemplateManager.php
new file mode 100644
index 00000000000..8b0c78d0b9a
--- /dev/null
+++ b/lib/private/Files/Template/TemplateManager.php
@@ -0,0 +1,237 @@
+<?php
+/*
+ * @copyright Copyright (c) 2021 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/>.
+ *
+ */
+
+declare(strict_types=1);
+
+
+namespace OC\Files\Template;
+
+use OCP\EventDispatcher\IEventDispatcher;
+use OCP\Files\Folder;
+use OCP\Files\File;
+use OCP\Files\GenericFileException;
+use OCP\Files\IRootFolder;
+use OCP\Files\Node;
+use OCP\Files\NotFoundException;
+use OCP\Files\Template\CreatedFromTemplateEvent;
+use OCP\Files\Template\ICustomTemplateProvider;
+use OCP\Files\Template\ITemplateManager;
+use OCP\Files\Template\Template;
+use OCP\Files\Template\TemplateFileCreator;
+use OCP\IConfig;
+use OCP\IPreview;
+use OCP\IServerContainer;
+use OCP\IUserSession;
+use OCP\L10N\IFactory;
+use Psr\Log\LoggerInterface;
+
+class TemplateManager implements ITemplateManager {
+ private $types = [];
+
+ private $registeredProviders = [];
+ private $providers;
+
+ private $serverContainer;
+ private $eventDispatcher;
+ private $rootFolder;
+ private $previewManager;
+ private $config;
+ private $l10n;
+ private $logger;
+ private $userId;
+
+ public function __construct(
+ IServerContainer $serverContainer,
+ IEventDispatcher $eventDispatcher,
+ IRootFolder $rootFolder,
+ IUserSession $userSession,
+ IPreview $previewManager,
+ IConfig $config,
+ IFactory $l10n,
+ LoggerInterface $logger
+ ) {
+ $this->serverContainer = $serverContainer;
+ $this->eventDispatcher = $eventDispatcher;
+ $this->rootFolder = $rootFolder;
+ $this->previewManager = $previewManager;
+ $this->config = $config;
+ $this->l10n = $l10n->get('lib');
+ $this->logger = $logger;
+ $user = $userSession->getUser();
+ $this->userId = $user ? $user->getUID() : null;
+ }
+
+ public function registerTemplateFileCreator(TemplateFileCreator $templateType): void {
+ $this->types[] = $templateType;
+ }
+
+ public function registerTemplateProvider(string $providerClass): void {
+ $this->registeredProviders[] = $providerClass;
+ }
+
+ public function getRegisteredProviders(): array {
+ if ($this->providers !== null) {
+ return $this->providers;
+ }
+ $this->providers = [];
+ foreach ($this->registeredProviders as $providerClass) {
+ $this->providers[$providerClass] = $this->serverContainer->get($providerClass);
+ }
+ return $this->providers;
+ }
+
+ public function listCreators(): array {
+ return array_map(function (TemplateFileCreator $entry) {
+ return array_merge($entry->jsonSerialize(), [
+ 'templates' => $this->getTemplateFiles($entry)
+ ]);
+ }, $this->types);
+ }
+
+ /**
+ * @param string $filePath
+ * @param string $templateId
+ * @return array
+ * @throws GenericFileException
+ */
+ public function createFromTemplate(string $filePath, string $templateId = '', string $templateType = 'user'): array {
+ $userFolder = $this->rootFolder->getUserFolder($this->userId);
+ try {
+ $userFolder->get($filePath);
+ throw new GenericFileException($this->l10n->t('File already exists'));
+ } catch (NotFoundException $e) {
+ }
+ try {
+ $targetFile = $userFolder->newFile($filePath);
+ 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);
+ });
+ $provider = array_shift($matchingProvider);
+ if ($provider) {
+ $template = $provider->getCustomTemplate($templateId);
+ $template->copy($targetFile->getPath());
+ }
+ }
+ $this->eventDispatcher->dispatchTyped(new CreatedFromTemplateEvent($template, $targetFile));
+ return $this->formatFile($userFolder->get($filePath));
+ } catch (\Exception $e) {
+ $this->logger->error($e->getMessage(), ['exception' => $e]);
+ throw new GenericFileException($this->l10n->t('Failed to create file from template'));
+ }
+ }
+
+ /**
+ * @return Folder
+ * @throws \OCP\Files\NotFoundException
+ * @throws \OCP\Files\NotPermittedException
+ * @throws \OC\User\NoUserException
+ */
+ private function getTemplateFolder(): Node {
+ return $this->rootFolder->getUserFolder($this->userId)->get($this->getTemplatePath());
+ }
+
+ private function getTemplateFiles(TemplateFileCreator $type): array {
+ $templates = [];
+ foreach ($this->getRegisteredProviders() as $provider) {
+ foreach ($type->getMimetypes() as $mimetype) {
+ foreach ($provider->getCustomTemplates($mimetype) as $template) {
+ $templates[] = $template;
+ }
+ }
+ }
+ try {
+ $userTemplateFolder = $this->getTemplateFolder();
+ } catch (\Exception $e) {
+ return $templates;
+ }
+ foreach ($type->getMimetypes() as $mimetype) {
+ foreach ($userTemplateFolder->searchByMime($mimetype) as $templateFile) {
+ $template = new Template(
+ 'user',
+ $this->rootFolder->getUserFolder($this->userId)->getRelativePath($templateFile->getPath()),
+ $templateFile
+ );
+ $template->setHasPreview($this->previewManager->isAvailable($templateFile));
+ $templates[] = $template;
+ }
+ }
+
+ return $templates;
+ }
+
+ /**
+ * @param Node|File $file
+ * @return array
+ * @throws NotFoundException
+ * @throws \OCP\Files\InvalidPathException
+ */
+ private function formatFile(Node $file): array {
+ return [
+ 'basename' => $file->getName(),
+ 'etag' => $file->getEtag(),
+ 'fileid' => $file->getId(),
+ 'filename' => $this->rootFolder->getUserFolder($this->userId)->getRelativePath($file->getPath()),
+ 'lastmod' => $file->getMTime(),
+ 'mime' => $file->getMimetype(),
+ 'size' => $file->getSize(),
+ 'type' => $file->getType(),
+ 'hasPreview' => $this->previewManager->isAvailable($file)
+ ];
+ }
+
+ public function hasTemplateDirectory(): bool {
+ try {
+ $this->getTemplateFolder();
+ return true;
+ } catch (\Exception $e) {
+ }
+ return false;
+ }
+
+ public function setTemplatePath(string $path): void {
+ $this->config->setUserValue($this->userId, 'core', 'templateDirectory', $path);
+ }
+
+ public function getTemplatePath(): string {
+ return $this->config->getUserValue($this->userId, 'core', 'templateDirectory', $this->l10n->t('Templates') . '/');
+ }
+
+ public function initializeTemplateDirectory(string $path = null, string $userId = null): void {
+ if ($userId !== null) {
+ $this->userId = $userId;
+ }
+ $userFolder = $this->rootFolder->getUserFolder($this->userId);
+ $templateDirectoryPath = $path ?? $this->l10n->t('Templates') . '/';
+ try {
+ $userFolder->get($templateDirectoryPath);
+ } catch (NotFoundException $e) {
+ $folder = $userFolder->newFolder($templateDirectoryPath);
+ $folder->newFile('Testtemplate.txt');
+ }
+ $this->setTemplatePath($templateDirectoryPath);
+ }
+}
diff --git a/lib/private/Server.php b/lib/private/Server.php
index ba954165799..c0d6afbaaf6 100644
--- a/lib/private/Server.php
+++ b/lib/private/Server.php
@@ -95,6 +95,7 @@ use OC\Files\Node\HookConnector;
use OC\Files\Node\LazyRoot;
use OC\Files\Node\Root;
use OC\Files\Storage\StorageFactory;
+use OC\Files\Template\TemplateManager;
use OC\Files\Type\Loader;
use OC\Files\View;
use OC\FullTextSearch\FullTextSearchManager;
@@ -166,6 +167,7 @@ use OCP\Files\IRootFolder;
use OCP\Files\Mount\IMountManager;
use OCP\Files\NotFoundException;
use OCP\Files\Storage\IStorageFactory;
+use OCP\Files\Template\ITemplateManager;
use OCP\FullTextSearch\IFullTextSearchManager;
use OCP\GlobalScale\IConfig;
use OCP\Group\Events\BeforeGroupCreatedEvent;
@@ -289,6 +291,7 @@ class Server extends ServerContainer implements IServerContainer {
$this->registerDeprecatedAlias('ContactsManager', \OCP\Contacts\IManager::class);
$this->registerAlias(\OCP\DirectEditing\IManager::class, \OC\DirectEditing\Manager::class);
+ $this->registerAlias(ITemplateManager::class, TemplateManager::class);
$this->registerAlias(IActionFactory::class, ActionFactory::class);
diff --git a/lib/private/legacy/OC_Util.php b/lib/private/legacy/OC_Util.php
index d5805719ea2..16e68b07cf1 100644
--- a/lib/private/legacy/OC_Util.php
+++ b/lib/private/legacy/OC_Util.php
@@ -66,6 +66,7 @@
use bantu\IniGetWrapper\IniGetWrapper;
use OC\AppFramework\Http\Request;
use OC\Files\Storage\LocalRootStorage;
+use OCP\Files\Template\ITemplateManager;
use OCP\IConfig;
use OCP\IGroupManager;
use OCP\ILogger;
@@ -447,6 +448,8 @@ class OC_Util {
self::copyr($skeletonDirectory, $userDirectory);
// update the file cache
$userDirectory->getStorage()->getScanner()->scan('', \OC\Files\Cache\Scanner::SCAN_RECURSIVE);
+ $templateManaer = \OC::$server->get(ITemplateManager::class);
+ $templateManaer->initializeTemplateDirectory(null, $userId);
}
}