Skip to content

Commit

Permalink
Merge pull request #25090 from nextcloud/enh/file-templates
Browse files Browse the repository at this point in the history
File templates
  • Loading branch information
rullzer authored Jan 28, 2021
2 parents 7e7284d + 63a2ea8 commit 62fa85c
Show file tree
Hide file tree
Showing 39 changed files with 1,782 additions and 16 deletions.
15 changes: 15 additions & 0 deletions apps/files/appinfo/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,21 @@
'url' => '/api/v1/directEditing/create',
'verb' => 'POST'
],
[
'name' => 'Template#list',
'url' => '/api/v1/templates',
'verb' => 'GET'
],
[
'name' => 'Template#create',
'url' => '/api/v1/templates/create',
'verb' => 'POST'
],
[
'name' => 'Template#path',
'url' => '/api/v1/templates/path',
'verb' => 'POST'
],
[
'name' => 'TransferOwnership#transfer',
'url' => '/api/v1/transferownership',
Expand Down
1 change: 1 addition & 0 deletions apps/files/composer/composer/autoload_classmap.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
'OCA\\Files\\Controller\\ApiController' => $baseDir . '/../lib/Controller/ApiController.php',
'OCA\\Files\\Controller\\DirectEditingController' => $baseDir . '/../lib/Controller/DirectEditingController.php',
'OCA\\Files\\Controller\\DirectEditingViewController' => $baseDir . '/../lib/Controller/DirectEditingViewController.php',
'OCA\\Files\\Controller\\TemplateController' => $baseDir . '/../lib/Controller/TemplateController.php',
'OCA\\Files\\Controller\\TransferOwnershipController' => $baseDir . '/../lib/Controller/TransferOwnershipController.php',
'OCA\\Files\\Controller\\ViewController' => $baseDir . '/../lib/Controller/ViewController.php',
'OCA\\Files\\Db\\TransferOwnership' => $baseDir . '/../lib/Db/TransferOwnership.php',
Expand Down
1 change: 1 addition & 0 deletions apps/files/composer/composer/autoload_static.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ class ComposerStaticInitFiles
'OCA\\Files\\Controller\\ApiController' => __DIR__ . '/..' . '/../lib/Controller/ApiController.php',
'OCA\\Files\\Controller\\DirectEditingController' => __DIR__ . '/..' . '/../lib/Controller/DirectEditingController.php',
'OCA\\Files\\Controller\\DirectEditingViewController' => __DIR__ . '/..' . '/../lib/Controller/DirectEditingViewController.php',
'OCA\\Files\\Controller\\TemplateController' => __DIR__ . '/..' . '/../lib/Controller/TemplateController.php',
'OCA\\Files\\Controller\\TransferOwnershipController' => __DIR__ . '/..' . '/../lib/Controller/TransferOwnershipController.php',
'OCA\\Files\\Controller\\ViewController' => __DIR__ . '/..' . '/../lib/Controller/ViewController.php',
'OCA\\Files\\Db\\TransferOwnership' => __DIR__ . '/..' . '/../lib/Db/TransferOwnership.php',
Expand Down
2 changes: 1 addition & 1 deletion apps/files/js/dist/files-app-settings.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion apps/files/js/dist/files-app-settings.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion apps/files/js/dist/personal-settings.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion apps/files/js/dist/personal-settings.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion apps/files/js/dist/sidebar.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion apps/files/js/dist/sidebar.js.map

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions apps/files/js/dist/templates.js

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions apps/files/js/dist/templates.js.map

Large diffs are not rendered by default.

21 changes: 20 additions & 1 deletion apps/files/js/newfilemenu.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,11 @@

var lastPos;
var checkInput = function () {
// Special handling for the setup template directory
if ($target.attr('data-action') === 'template-init') {
return true;
}

var filename = $input.val();
try {
if (!Files.isFileNameValid(filename)) {
Expand Down Expand Up @@ -198,7 +203,21 @@
iconClass: actionSpec.iconClass,
fileType: actionSpec.fileType,
actionHandler: actionSpec.actionHandler,
});
checkFilename: actionSpec.checkFilename
});
},

/**
* Remove a menu item from the "New" file menu
* @param {string} actionId
*/
removeMenuEntry: function(actionId) {
var index = this._menuItems.findIndex(function (actionSpec) {
return actionSpec.id === actionId;
});
if (index > -1) {
this._menuItems.splice(index, 1);
}
},

/**
Expand Down
76 changes: 76 additions & 0 deletions apps/files/lib/Controller/TemplateController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?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 OCA\Files\Controller;

use OCP\AppFramework\Http\DataResponse;
use OCP\AppFramework\OCS\OCSForbiddenException;
use OCP\AppFramework\OCSController;
use OCP\Files\GenericFileException;
use OCP\Files\Template\ITemplateManager;
use OCP\IRequest;

class TemplateController extends OCSController {
protected $templateManager;

public function __construct($appName, IRequest $request, ITemplateManager $templateManager) {
parent::__construct($appName, $request);
$this->templateManager = $templateManager;
}

/**
* @NoAdminRequired
*/
public function list(): DataResponse {
return new DataResponse($this->templateManager->listTemplates());
}

/**
* @NoAdminRequired
* @throws OCSForbiddenException
*/
public function create(string $filePath, string $templatePath = '', string $templateType = 'user'): DataResponse {
try {
return new DataResponse($this->templateManager->createFromTemplate($filePath, $templatePath, $templateType));
} catch (GenericFileException $e) {
throw new OCSForbiddenException($e->getMessage());
}
}

/**
* @NoAdminRequired
*/
public function path(string $templatePath = '', bool $copySystemTemplates = false) {
try {
$templatePath = $this->templateManager->initializeTemplateDirectory($templatePath, null, $copySystemTemplates);
return new DataResponse([
'template_path' => $templatePath,
'templates' => $this->templateManager->listCreators()
]);
} catch (\Exception $e) {
throw new OCSForbiddenException($e->getMessage());
}
}
}
15 changes: 14 additions & 1 deletion apps/files/lib/Controller/ViewController.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,12 @@
use OCP\AppFramework\Http\RedirectResponse;
use OCP\AppFramework\Http\Response;
use OCP\AppFramework\Http\TemplateResponse;
use OCP\AppFramework\Services\IInitialState;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\Files\Folder;
use OCP\Files\IRootFolder;
use OCP\Files\NotFoundException;
use OCP\Files\Template\ITemplateManager;
use OCP\IConfig;
use OCP\IL10N;
use OCP\IRequest;
Expand Down Expand Up @@ -80,6 +82,10 @@ class ViewController extends Controller {
protected $rootFolder;
/** @var Helper */
protected $activityHelper;
/** @var IInitialState */
private $initialState;
/** @var ITemplateManager */
private $templateManager;

public function __construct(string $appName,
IRequest $request,
Expand All @@ -90,7 +96,9 @@ public function __construct(string $appName,
IUserSession $userSession,
IAppManager $appManager,
IRootFolder $rootFolder,
Helper $activityHelper
Helper $activityHelper,
IInitialState $initialState,
ITemplateManager $templateManager
) {
parent::__construct($appName, $request);
$this->appName = $appName;
Expand All @@ -103,6 +111,8 @@ public function __construct(string $appName,
$this->appManager = $appManager;
$this->rootFolder = $rootFolder;
$this->activityHelper = $activityHelper;
$this->initialState = $initialState;
$this->templateManager = $templateManager;
}

/**
Expand Down Expand Up @@ -180,6 +190,7 @@ public function index($dir = '', $view = '', $fileid = null, $fileNotFound = fal
// Load the files we need
\OCP\Util::addStyle('files', 'merged');
\OCP\Util::addScript('files', 'merged-index');
\OCP\Util::addScript('files', 'dist/templates');

// mostly for the home storage's free space
// FIXME: Make non static
Expand Down Expand Up @@ -283,6 +294,8 @@ public function index($dir = '', $view = '', $fileid = null, $fileNotFound = fal
if (class_exists(LoadViewer::class)) {
$this->eventDispatcher->dispatchTyped(new LoadViewer());
}
$this->initialState->provideInitialState('templates_path', $this->templateManager->hasTemplateDirectory() ? $this->templateManager->getTemplatePath() : null);
$this->initialState->provideInitialState('templates', $this->templateManager->listCreators());

$params = [];
$params['usedSpacePercent'] = (int) $storageInfo['relative'];
Expand Down
3 changes: 2 additions & 1 deletion apps/files/lib/Event/LoadAdditionalScriptsEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@
use OCP\EventDispatcher\Event;

/**
* This event is triggered when the files app is rendered. It canb e used to add additional scripts to the files app.
* This event is triggered when the files app is rendered.
* It can be used to add additional scripts to the files app.
*
* @since 17.0.0
*/
Expand Down
Loading

0 comments on commit 62fa85c

Please sign in to comment.