summaryrefslogtreecommitdiffstats
path: root/lib/public
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/public
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/public')
-rw-r--r--lib/public/Files/Template/CreatedFromTemplateEvent.php64
-rw-r--r--lib/public/Files/Template/ICustomTemplateProvider.php51
-rw-r--r--lib/public/Files/Template/ITemplateManager.php94
-rw-r--r--lib/public/Files/Template/Template.php68
-rw-r--r--lib/public/Files/Template/TemplateFileCreator.php73
5 files changed, 350 insertions, 0 deletions
diff --git a/lib/public/Files/Template/CreatedFromTemplateEvent.php b/lib/public/Files/Template/CreatedFromTemplateEvent.php
new file mode 100644
index 00000000000..8d802814406
--- /dev/null
+++ b/lib/public/Files/Template/CreatedFromTemplateEvent.php
@@ -0,0 +1,64 @@
+<?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 OCP\Files\Template;
+
+use OCP\EventDispatcher\Event;
+use OCP\Files\File;
+
+/**
+ * @since 21.0.0
+ */
+class CreatedFromTemplateEvent extends Event {
+ private $template;
+ private $target;
+
+ /**
+ * @param File|null $template
+ * @param File $target
+ * @since 21.0.0
+ */
+ public function __construct(?File $template, File $target) {
+ $this->template = $template;
+ $this->target = $target;
+ }
+
+ /**
+ * @return File|null
+ * @since 21.0.0
+ */
+ public function getTemplate(): ?File {
+ return $this->template;
+ }
+
+ /**
+ * @return File
+ * @since 21.0.0
+ */
+ public function getTarget(): File {
+ return $this->target;
+ }
+}
diff --git a/lib/public/Files/Template/ICustomTemplateProvider.php b/lib/public/Files/Template/ICustomTemplateProvider.php
new file mode 100644
index 00000000000..a14ea86a100
--- /dev/null
+++ b/lib/public/Files/Template/ICustomTemplateProvider.php
@@ -0,0 +1,51 @@
+<?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 OCP\Files\Template;
+
+use OCP\Files\File;
+
+/**
+ * @since 21.0.0
+ */
+interface ICustomTemplateProvider {
+ /**
+ * Return a list of additional templates that the template provider is offering
+ *
+ * @return File[]
+ * @since 21.0.0
+ */
+ public function getCustomTemplates(string $mimetype): array;
+
+ /**
+ * Return the file for a given template id
+ *
+ * @param string $template identifier of the template
+ * @return File
+ * @since 21.0.0
+ */
+ public function getCustomTemplate(string $template): File;
+}
diff --git a/lib/public/Files/Template/ITemplateManager.php b/lib/public/Files/Template/ITemplateManager.php
new file mode 100644
index 00000000000..61dbb68cd77
--- /dev/null
+++ b/lib/public/Files/Template/ITemplateManager.php
@@ -0,0 +1,94 @@
+<?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 OCP\Files\Template;
+
+use OCP\Files\GenericFileException;
+
+/**
+ * @since 21.0.0
+ */
+interface ITemplateManager {
+
+ /**
+ * Register a template type support
+ *
+ * @param TemplateFileCreator $templateType
+ * @since 21.0.0
+ */
+ public function registerTemplateFileCreator(TemplateFileCreator $templateType): void;
+
+ /**
+ * Register a custom template provider class that is able to inject custom templates
+ * in addition to the user defined ones
+ *
+ * @param string $providerClass
+ * @since 21.0.0
+ */
+ public function registerTemplateProvider(string $providerClass): void;
+
+ /**
+ * Get a list of available file creators and their offered templates
+ *
+ * @return array
+ * @since 21.0.0
+ */
+ public function listCreators(): array;
+
+ /**
+ * @return bool
+ * @since 21.0.0
+ */
+ public function hasTemplateDirectory(): bool;
+
+ /**
+ * @param string $path
+ * @return void
+ * @since 21.0.0
+ */
+ public function setTemplatePath(string $path): void;
+
+ /**
+ * @return string
+ * @since 21.0.0
+ */
+ public function getTemplatePath(): string;
+
+ /**
+ * @param string $path
+ * @since 21.0.0
+ */
+ public function initializeTemplateDirectory(string $path): void;
+
+ /**
+ * @param string $filePath
+ * @param string $templateId
+ * @return array
+ * @throws GenericFileException
+ * @since 21.0.0
+ */
+ public function createFromTemplate(string $filePath, string $templateId = '', string $templateType = 'user'): array;
+}
diff --git a/lib/public/Files/Template/Template.php b/lib/public/Files/Template/Template.php
new file mode 100644
index 00000000000..b5b90e01f89
--- /dev/null
+++ b/lib/public/Files/Template/Template.php
@@ -0,0 +1,68 @@
+<?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 OCP\Files\Template;
+
+use OCP\Files\File;
+
+class Template implements \JsonSerializable {
+ protected $templateType;
+ protected $templateId;
+ protected $file;
+ protected $hasPreview = false;
+ protected $previewUrl;
+
+ final public function __construct(string $templateType, string $templateId, File $file) {
+ $this->templateType = $templateType;
+ $this->templateId = $templateId;
+ $this->file = $file;
+ }
+
+ final public function setCustomPreviewUrl(string $previewUrl): void {
+ $this->previewUrl = $previewUrl;
+ }
+
+ final public function setHasPreview(bool $hasPreview): void {
+ $this->hasPreview = $hasPreview;
+ }
+
+ final public function jsonSerialize() {
+ return [
+ 'templateType' => $this->templateType,
+ 'templateId' => $this->templateId,
+ 'basename' => $this->file->getName(),
+ 'etag' => $this->file->getEtag(),
+ 'fileid' => $this->file->getId(),
+ 'filename' => $this->templateId,
+ 'lastmod' => $this->file->getMTime(),
+ 'mime' => $this->file->getMimetype(),
+ 'size' => $this->file->getSize(),
+ 'type' => $this->file->getType(),
+ 'hasPreview' => $this->hasPreview,
+ 'previewUrl' => $this->previewUrl
+ ];
+ }
+}
diff --git a/lib/public/Files/Template/TemplateFileCreator.php b/lib/public/Files/Template/TemplateFileCreator.php
new file mode 100644
index 00000000000..eeca2f0c01f
--- /dev/null
+++ b/lib/public/Files/Template/TemplateFileCreator.php
@@ -0,0 +1,73 @@
+<?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 OCP\Files\Template;
+
+/**
+ * @since 21.0.0
+ */
+final class TemplateFileCreator implements \JsonSerializable {
+ protected $appId;
+ protected $mimetypes = [];
+ protected $actionName;
+ protected $fileExtension;
+ protected $iconClass;
+
+ public function __construct(
+ string $appId, string $actionName, string $fileExtension
+ ) {
+ $this->appId = $appId;
+ $this->actionName = $actionName;
+ $this->fileExtension = $fileExtension;
+ }
+
+ public function getAppId(): string {
+ return $this->appId;
+ }
+
+ public function setIconClass(string $iconClass): TemplateFileCreator {
+ $this->iconClass = $iconClass;
+ return $this;
+ }
+
+ public function addMimetype(string $mimetype): TemplateFileCreator {
+ $this->mimetypes[] = $mimetype;
+ return $this;
+ }
+
+ public function getMimetypes(): array {
+ return $this->mimetypes;
+ }
+
+ public function jsonSerialize() {
+ return [
+ 'app' => $this->appId,
+ 'label' => $this->actionName,
+ 'extension' => $this->fileExtension,
+ 'iconClass' => $this->iconClass,
+ 'mimetypes' => $this->mimetypes
+ ];
+ }
+}