Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/nextcloud/richdocuments.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorJohn Molakvoæ (skjnldsv) <skjnldsv@protonmail.com>2018-09-18 11:45:45 +0300
committerRoeland Jago Douma <roeland@famdouma.nl>2018-12-20 09:38:41 +0300
commite334a2420ede7fdbff6b2ed34a6ecc138a3eddb6 (patch)
treef888da6c0bc51f7956bd3ad466eaf9d90f3828f6 /lib
parent94701e018cb53f6a633a1bd4d1ec0b16a40765de (diff)
Template manager init
Signed-off-by: John Molakvoæ (skjnldsv) <skjnldsv@protonmail.com>
Diffstat (limited to 'lib')
-rw-r--r--lib/AppInfo/Application.php5
-rw-r--r--lib/Controller/OCSController.php19
-rw-r--r--lib/Controller/TemplatesController.php154
-rw-r--r--lib/Settings/Admin.php11
-rw-r--r--lib/TemplateManager.php192
5 files changed, 379 insertions, 2 deletions
diff --git a/lib/AppInfo/Application.php b/lib/AppInfo/Application.php
index 83ba668d..fea4d0fe 100644
--- a/lib/AppInfo/Application.php
+++ b/lib/AppInfo/Application.php
@@ -2,6 +2,9 @@
/**
* @copyright Copyright (c) 2016 Lukas Reschke <lukas@statuscode.ch>
*
+ * @author Lukas Reschke <lukas@statuscode.ch>
+ * @author Roeland Jago Douma <roeland@famdouma.nl>
+ *
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
@@ -15,7 +18,7 @@
* 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/>.
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
diff --git a/lib/Controller/OCSController.php b/lib/Controller/OCSController.php
index 14651409..f4156a66 100644
--- a/lib/Controller/OCSController.php
+++ b/lib/Controller/OCSController.php
@@ -3,6 +3,7 @@
* @copyright Copyright (c) 2018, Roeland Jago Douma <roeland@famdouma.nl>
*
* @author Roeland Jago Douma <roeland@famdouma.nl>
+ * @author John Molakvoæ <skjnldsv@protonmail.com>
*
* @license GNU AGPL version 3 or any later version
*
@@ -91,4 +92,22 @@ class OCSController extends \OCP\AppFramework\OCSController {
throw new OCSNotFoundException();
}
}
+
+ /**
+ * @NoAdminRequired
+ *
+ * @param string $type The template id
+ */
+ public function getTemplates(string $type) {
+
+ }
+
+ /**
+ * @NoAdminRequired
+ *
+ * @param string $path Where to create the document
+ * @param int $template The template id
+ */
+ public function createFromTemplate(string $path, int $template) {
+ }
}
diff --git a/lib/Controller/TemplatesController.php b/lib/Controller/TemplatesController.php
new file mode 100644
index 00000000..dabcdcea
--- /dev/null
+++ b/lib/Controller/TemplatesController.php
@@ -0,0 +1,154 @@
+<?php
+/**
+ * @copyright Copyright (c) 2018 John Molakvoæ <skjnldsv@protonmail.com>
+ *
+ * @author John Molakvoæ <skjnldsv@protonmail.com>
+ *
+ * @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\Controller;
+
+use OCA\Richdocuments\TemplateManager;
+use OCP\AppFramework\Controller;
+use OCP\AppFramework\Http;
+use OCP\AppFramework\Http\DataDisplayResponse;
+use OCP\AppFramework\Http\JSONResponse;
+use OCP\AppFramework\Http\NotFoundResponse;
+use OCP\IL10N;
+use OCP\IRequest;
+use OC\Files\Filesystem;
+
+class TemplatesController extends Controller {
+
+ /** @var string */
+ protected $appName;
+
+ /** @var IRequest */
+ protected $request;
+
+ /** @var IL10N */
+ private $l10n;
+
+ /** @var TemplateManager */
+ private $manager;
+
+ /** @var int Max template size */
+ private $maxSize = 20 * 1024 * 1024;
+
+ /**
+ * Controller
+ *
+ * @param string $appName
+ * @param IRequest $request
+ * @param L10N $l10n
+ * @param TemplateManager $templateManager
+ */
+ public function __construct(string $appName,
+ IRequest $request,
+ IL10N $l10n,
+ TemplateManager $templateManager) {
+ parent::__construct($appName, $request);
+
+ $this->appName = $appName;
+ $this->request = $request;
+ $this->l10n = $l10n;
+ $this->manager = $templateManager;
+ }
+
+ /**
+ * @NoAdminRequired
+ * @NoCSRFRequired
+ *
+ * Get preview for a specific template
+ *
+ * @param string $templateName The template id
+ * @return DataDisplayResponse|NotFoundResponse
+ */
+ public function getPreview(string $templateName) {
+ try {
+ $template = $this->manager->get($templateName);
+
+ //return DataDisplayResponse($template->getPreview(), Http::STATUS_OK, ['Content-Type' => 'image/png']);
+ } catch (NotFoundException $e) {
+ return new NotFoundResponse();
+ }
+ }
+
+ /**
+ * Add a global template
+ *
+ * @return JSONResponse
+ */
+ public function add() {
+ $files = $this->request->getUploadedFile('files');
+
+ if (!is_null($files)) {
+ if ($files['error'][0] === 0
+ && is_uploaded_file($files['tmp_name'][0])
+ && !Filesystem::isFileBlacklisted($files['tmp_name'][0])) {
+
+ // TODO: ensure the size limit is decent for preview
+ if ($files['size'][0] > $this->maxSize) {
+ return new JSONResponse(
+ ['data' => ['message' => $this->l10n->t('File is too big')]],
+ Http::STATUS_BAD_REQUEST
+ );
+ }
+
+ $templateName = $files['name'][0];
+ $templateFile = file_get_contents($files['tmp_name'][0]);
+
+ unlink($files['tmp_name'][0]);
+
+ $template = $this->manager->add($templateName, $templateFile);
+
+ return new JSONResponse(
+ ['data' => ['data' => $template]],
+ Http::STATUS_CREATED
+ );
+ }
+ }
+
+ return new JSONResponse(
+ ['data' => ['message' => $this->l10n->t('Invalid file provided')]],
+ Http::STATUS_BAD_REQUEST
+ );
+ }
+
+ /**
+ * Delete a global template
+ *
+ * @param string $templateName
+ * @return JSONResponse
+ */
+ public function delete(string $templateName) {
+ try {
+ $manager->delete($templateName);
+
+ return new JSONResponse(
+ ['data' => ['status' => 'success']],
+ Http::STATUS_NO_CONTENT
+ );
+ } catch (NotFoundException $e) {
+ return new JSONResponse(
+ ['data' => ['message' => $this->l10n->t('Template not found')]],
+ Http::STATUS_NOT_FOUND
+ );
+ }
+ }
+}
diff --git a/lib/Settings/Admin.php b/lib/Settings/Admin.php
index 0bad4f7c..bb95371d 100644
--- a/lib/Settings/Admin.php
+++ b/lib/Settings/Admin.php
@@ -23,6 +23,7 @@
namespace OCA\Richdocuments\Settings;
+use OCA\Richdocuments\TemplateManager;
use OCP\AppFramework\Http\TemplateResponse;
use OCP\Defaults;
use OCP\IConfig;
@@ -30,13 +31,20 @@ use OCP\IL10N;
use OCP\Settings\ISettings;
class Admin implements ISettings {
+
/** @var IConfig */
private $config;
+
+ /** @var TemplateManager */
+ private $templateManager;
/**
* @param IConfig $config
+ * @param TemplateManager $templateManager
*/
- public function __construct(IConfig $config) {
+ public function __construct(IConfig $config,
+ TemplateManager $templateManager) {
$this->config = $config;
+ $this->templateManager = $templateManager;
}
/**
* @return TemplateResponse
@@ -52,6 +60,7 @@ class Admin implements ISettings {
'doc_format' => $this->config->getAppValue('richdocuments', 'doc_format'),
'external_apps' => $this->config->getAppValue('richdocuments', 'external_apps'),
'canonical_webroot' => $this->config->getAppValue('richdocuments', 'canonical_webroot'),
+ 'templates' => $this->templateManager->getGlobals(),
],
'blank'
);
diff --git a/lib/TemplateManager.php b/lib/TemplateManager.php
new file mode 100644
index 00000000..64899f12
--- /dev/null
+++ b/lib/TemplateManager.php
@@ -0,0 +1,192 @@
+<?php
+/**
+ * @copyright Copyright (c) 2018 John Molakvoæ <skjnldsv@protonmail.com>
+ *
+ * @author John Molakvoæ <skjnldsv@protonmail.com>
+ *
+ * @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;
+
+use OCP\Files\Folder;
+use OCP\Files\IAppData;
+use OCP\Files\NotFoundException;
+use OCP\Files\SimpleFS\ISimpleFile;
+use OCP\Files\SimpleFS\ISimpleFolder;
+use OCP\IConfig;
+use OCP\IPreview;
+use OCP\IURLGenerator;
+use OC\Files\AppData\Factory;
+
+class TemplateManager {
+
+ /** @var string */
+ protected $appName;
+
+ /** @var string */
+ protected $userId;
+
+ /** @var IConfig */
+ private $config;
+
+ /** @var IAppData */
+ private $appData;
+
+ /** @var IURLGenerator */
+ private $urlGenerator;
+
+ /** @var ISimpleFolder */
+ private $folder;
+
+ /** @var Folder */
+ private $userFolder;
+
+ /** @var IPreview */
+ private $previewManager;
+
+ /** Accepted templates mime types */
+ const MIME_DOCUMENTS = [
+ 'a', 'b', 'c'
+ ];
+ const MIME_SHEETS = [
+ 'aa', 'bb', 'cc'
+ ];
+ const MIME_PRESENTATIONS = [
+ 'aaa', 'bbb', 'ccc'
+ ];
+
+ /** @var array Template mime types match */
+ static public $tplTypes = [
+ 'document' => self::MIME_DOCUMENTS,
+ 'sheet' => self::MIME_SHEETS,
+ 'presentation' => self::MIME_PRESENTATIONS
+ ];
+
+ /**
+ * Template manager
+ *
+ * @param string $appName
+ * @param string $userId
+ * @param IConfig $config
+ * @param Factory $appDataFactory
+ * @param IURLGenerator $urlGenerator
+ * @param IRootFolder $rootFolder
+ * @param IPreview $previewManager
+ */
+ public function __construct(string $appName,
+ string $userId,
+ IConfig $config,
+ Factory $appDataFactory,
+ IURLGenerator $urlGenerator,
+ IRootFolder $rootFolder,
+ IPreview $previewManager) {
+ $this->appName = $appName;
+ $this->userId = $userId;
+ $this->config = $config;
+ $this->appData = $appDataFactory->get($appName);
+ $this->urlGenerator = $urlGenerator;
+ $this->userFolder = $rootFolder->getUserFolder($userId);
+ $this->previewManager = $previewManager;
+
+ try {
+ $this->folder = $this->appData->getFolder('templates');
+ } catch (NotFoundException $e) {
+ $this->folder = $this->appData->newFolder('templates');
+ }
+ }
+
+ /**
+ * Get template info
+ *
+ * @param string $templateName
+ * @return array
+ */
+ public function get(string $templateName) {
+ try {
+ // is this a global template ?
+ $template = $this->folder->getFile($templateName);
+ } catch (NotFoundException $e) {
+ // user defined template dir?
+ $templateDirID = $this->config->getUserValue($this->userId, $this->appName, 'template_dir', false);
+ try {
+ $templateDir = $this->userFolder->getById($templateDirID);
+ } catch (NotFoundException $e) {
+ // fallback to default template dir
+ try {
+ $templateDir = $this->userFolder->get('Templates');
+ $template = $templateDir;
+ } catch (NotFoundException $e) {
+ return new NotFoundException();
+ }
+ }
+ }
+
+ var_dump($template);
+
+ return true;
+
+ }
+
+ /**
+ * Get all global templates
+ */
+ public function getGlobals() {
+ $templateFiles = $this->folder->getDirectoryListing();
+
+ return array_map(function (ISimpleFile $templateFile) {
+ return [
+ 'name' => $templateFile->getName(),
+ 'preview' => $this->urlGenerator->linkToRoute('richdocuments.templates.getPreview', ['templateName' => $templateFile->getName()]),
+ 'ext' => $this->flipTypes[$templateFile->getMimeType()]
+ ];
+ }, $templateFiles);
+ }
+
+ /**
+ * Add a template to the global template folder
+ *
+ * @param string $templateName
+ * @param string $templateFile
+ * @return void
+ */
+ public function add(string $templateName, string $templateFile) {
+ try {
+ $template = $this->folder->getFile($templateName);
+ } catch (NotFoundException $e) {
+ $template = $this->folder->newFile($templateName);
+ }
+ $template->putContent($templateFile);
+
+ return $this->getGlobals;
+ }
+
+ /**
+ * Flip tplTypes to retrieve types by mime
+ *
+ * @return array
+ */
+ private function flipTypes(): array{
+ $result = array();
+ foreach ($this::$tplTypes as $type => &$mime) {
+ $mime = array_fill_keys($mime, $type);
+ $result = array_merge($result, $mime);
+ }
+
+ return $result;
+ }
+}