diff options
author | John Molakvoæ (skjnldsv) <skjnldsv@protonmail.com> | 2018-09-18 11:45:45 +0300 |
---|---|---|
committer | Roeland Jago Douma <roeland@famdouma.nl> | 2018-12-20 09:38:41 +0300 |
commit | e334a2420ede7fdbff6b2ed34a6ecc138a3eddb6 (patch) | |
tree | f888da6c0bc51f7956bd3ad466eaf9d90f3828f6 | |
parent | 94701e018cb53f6a633a1bd4d1ec0b16a40765de (diff) |
Template manager init
Signed-off-by: John Molakvoæ (skjnldsv) <skjnldsv@protonmail.com>
-rw-r--r-- | appinfo/routes.php | 31 | ||||
-rw-r--r-- | css/admin.css | 6 | ||||
-rw-r--r-- | css/admin.scss | 43 | ||||
-rw-r--r-- | js/admin.js | 37 | ||||
-rw-r--r-- | lib/AppInfo/Application.php | 5 | ||||
-rw-r--r-- | lib/Controller/OCSController.php | 19 | ||||
-rw-r--r-- | lib/Controller/TemplatesController.php | 154 | ||||
-rw-r--r-- | lib/Settings/Admin.php | 11 | ||||
-rw-r--r-- | lib/TemplateManager.php | 192 |
9 files changed, 485 insertions, 13 deletions
diff --git a/appinfo/routes.php b/appinfo/routes.php index ba2f0601..b74e2a1d 100644 --- a/appinfo/routes.php +++ b/appinfo/routes.php @@ -1,12 +1,26 @@ <?php /** - * ownCloud - Richdocuments App + * @copyright Copyright (c) 2013-2014 Victor Dubiniuk <victor.dubiniuk@gmail.com> * - * @author Victor Dubiniuk - * @copyright 2013-2014 Victor Dubiniuk victor.dubiniuk@gmail.com + * @author Victor Dubiniuk <victor.dubiniuk@gmail.com> + * @author Roeland Jago Douma <roeland@famdouma.nl> + * @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/>. * - * This file is licensed under the Affero General Public License version 3 or - * later. */ namespace OCA\Richdocuments\AppInfo; @@ -37,8 +51,15 @@ return [ //assets ['name' => 'assets#create', 'url' => 'assets', 'verb' => 'POST'], ['name' => 'assets#get', 'url' => 'assets/{token}', 'verb' => 'GET'], + + // templates + ['name' => 'templates#getPreview', 'url' => '/template/{templateName}', 'verb' => 'GET'], + ['name' => 'templates#add', 'url' => '/template', 'verb' => 'POST'], + ['name' => 'templates#delete', 'url' => '/template/{templateName}', 'verb' => 'DELETE'], ], 'ocs' => [ ['name' => 'OCS#create', 'url' => '/api/v1/document', 'verb' => 'POST'], + ['name' => 'OCS#getTemplates', 'url' => '/api/v1/templates/{type}', 'verb' => 'GET'], + ['name' => 'OCS#createFromTemplate', 'url' => '/api/v1/templates/new', 'verb' => 'POST'] ], ]; diff --git a/css/admin.css b/css/admin.css deleted file mode 100644 index 6a5d4af2..00000000 --- a/css/admin.css +++ /dev/null @@ -1,6 +0,0 @@ -.rd-settings-documentation { - max-width: 50em; -}; -#richdocuments h2 { - display: inline-block; -}
\ No newline at end of file diff --git a/css/admin.scss b/css/admin.scss new file mode 100644 index 00000000..3c4e065f --- /dev/null +++ b/css/admin.scss @@ -0,0 +1,43 @@ +.rd-settings-documentation { + max-width: 50em; +}; +#richdocuments, +#richdocuments-templates { + // inline buttons on section headers + > h2 { + display: inline-flex; + align-items: center; + + .icon-info, + .icon-add, + .icon-loading-small { + width: 44px; + height: 44px; + margin: -6px; + margin-left: 10px; + opacity: .5; + &:hover, + &:focus, + &:active { + opacity: .7; + } + } + } +} + +#richdocuments-templates { + > input { + // feedback for keyboard navigation + &:hover, + &:focus, + &:active { + + h2 .icon-add, + + h2 .icon-loading-small { + opacity: .7; + } + + #emptycontent label { + color: var(--color-text-light); + } + } + } +}
\ No newline at end of file diff --git a/js/admin.js b/js/admin.js index 30f6cf89..6c7f3b49 100644 --- a/js/admin.js +++ b/js/admin.js @@ -262,6 +262,43 @@ var documentsSettings = { } }; +function initTemplateManager() { + var inputElmt = document.querySelector('#add-template'); + var buttonElmt = document.querySelector('.icon-add'); + + $('#richdocuments-templates').fileupload({ + dataType: 'json', + url: OC.generateUrl(OC.linkTo('richdocuments', 'template')), + type: 'POST', + + add: function (e, data) { + // submit on file selection + data.submit(); + console.log(e, data) + }, + + submit: function(e, data) { + + data.formData = _.extend(data.formData || {}, { + requesttoken: OC.requestToken + }); + }, + + done: function(e, data) { + inputElmt.disabled = false; + buttonElmt.className = 'icon-add'; + console.log(e, data) + }, + + fail: function(e, data) { + inputElmt.disabled = false; + buttonElmt.className = 'icon-add'; + console.log(e, data) + } + }); +} + $(document).ready(function(){ documentsSettings.initialize(); + initTemplateManager(); }); 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; + } +} |