Browse Source

files: Create files from template API

Signed-off-by: Julius Härtl <jus@bitgrid.net>
tags/v21.0.0beta8
Julius Härtl 3 years ago
parent
commit
4974404774
No account linked to committer's email address

+ 15
- 0
apps/files/appinfo/routes.php View File

@@ -138,6 +138,21 @@ $application->registerRoutes(
'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',

+ 1
- 0
apps/files/composer/composer/autoload_classmap.php View File

@@ -35,6 +35,7 @@ return array(
'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',

+ 1
- 0
apps/files/composer/composer/autoload_static.php View File

@@ -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',

+ 76
- 0
apps/files/lib/Controller/TemplateController.php View File

@@ -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->listCreators());
}

/**
* @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 {
$this->templateManager->setTemplatePath($templatePath);
if ($copySystemTemplates) {
$this->templateManager->initializeTemplateDirectory($templatePath);
}
return new DataResponse();
} catch (GenericFileException $e) {
throw new OCSForbiddenException($e->getMessage());
}
}
}

+ 13
- 1
apps/files/lib/Controller/ViewController.php View File

@@ -44,10 +44,12 @@ use OCP\AppFramework\Http\ContentSecurityPolicy;
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;
@@ -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,
@@ -90,7 +96,9 @@ class ViewController extends Controller {
IUserSession $userSession,
IAppManager $appManager,
IRootFolder $rootFolder,
Helper $activityHelper
Helper $activityHelper,
IInitialState $initialState,
ITemplateManager $templateManager
) {
parent::__construct($appName, $request);
$this->appName = $appName;
@@ -103,6 +111,8 @@ class ViewController extends Controller {
$this->appManager = $appManager;
$this->rootFolder = $rootFolder;
$this->activityHelper = $activityHelper;
$this->initialState = $initialState;
$this->templateManager = $templateManager;
}

/**
@@ -283,6 +293,8 @@ class ViewController extends Controller {
if (class_exists(LoadViewer::class)) {
$this->eventDispatcher->dispatchTyped(new LoadViewer());
}
$this->initialState->provideInitialState('template_path', $this->templateManager->hasTemplateDirectory() ? $this->templateManager->getTemplatePath() : null);
$this->initialState->provideInitialState('templates', $this->templateManager->listCreators());

$params = [];
$params['usedSpacePercent'] = (int) $storageInfo['relative'];

+ 10
- 0
apps/files/tests/Controller/ViewControllerTest.php View File

@@ -36,10 +36,12 @@ use OCA\Files\Activity\Helper;
use OCA\Files\Controller\ViewController;
use OCP\App\IAppManager;
use OCP\AppFramework\Http;
use OCP\AppFramework\Services\IInitialState;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\Files\File;
use OCP\Files\Folder;
use OCP\Files\IRootFolder;
use OCP\Files\Template\ITemplateManager;
use OCP\IConfig;
use OCP\IL10N;
use OCP\IRequest;
@@ -78,6 +80,10 @@ class ViewControllerTest extends TestCase {
private $rootFolder;
/** @var Helper|\PHPUnit\Framework\MockObject\MockObject */
private $activityHelper;
/** @var IInitialState|\PHPUnit\Framework\MockObject\MockObject */
private $initialState;
/** @var ITemplateManager|\PHPUnit\Framework\MockObject\MockObject */
private $templateManager;

protected function setUp(): void {
parent::setUp();
@@ -97,6 +103,8 @@ class ViewControllerTest extends TestCase {
->willReturn($this->user);
$this->rootFolder = $this->getMockBuilder('\OCP\Files\IRootFolder')->getMock();
$this->activityHelper = $this->createMock(Helper::class);
$this->initialState = $this->createMock(IInitialState::class);
$this->templateManager = $this->createMock(ITemplateManager::class);
$this->viewController = $this->getMockBuilder('\OCA\Files\Controller\ViewController')
->setConstructorArgs([
'files',
@@ -109,6 +117,8 @@ class ViewControllerTest extends TestCase {
$this->appManager,
$this->rootFolder,
$this->activityHelper,
$this->initialState,
$this->templateManager,
])
->setMethods([
'getStorageInfo',

+ 6
- 0
lib/composer/composer/autoload_classmap.php View File

@@ -315,6 +315,11 @@ return array(
'OCP\\Files\\Storage\\IStorage' => $baseDir . '/lib/public/Files/Storage/IStorage.php',
'OCP\\Files\\Storage\\IStorageFactory' => $baseDir . '/lib/public/Files/Storage/IStorageFactory.php',
'OCP\\Files\\Storage\\IWriteStreamStorage' => $baseDir . '/lib/public/Files/Storage/IWriteStreamStorage.php',
'OCP\\Files\\Template\\CreatedFromTemplateEvent' => $baseDir . '/lib/public/Files/Template/CreatedFromTemplateEvent.php',
'OCP\\Files\\Template\\ICustomTemplateProvider' => $baseDir . '/lib/public/Files/Template/ICustomTemplateProvider.php',
'OCP\\Files\\Template\\ITemplateManager' => $baseDir . '/lib/public/Files/Template/ITemplateManager.php',
'OCP\\Files\\Template\\Template' => $baseDir . '/lib/public/Files/Template/Template.php',
'OCP\\Files\\Template\\TemplateFileCreator' => $baseDir . '/lib/public/Files/Template/TemplateFileCreator.php',
'OCP\\Files\\UnseekableException' => $baseDir . '/lib/public/Files/UnseekableException.php',
'OCP\\Files_FullTextSearch\\Model\\AFilesDocument' => $baseDir . '/lib/public/Files_FullTextSearch/Model/AFilesDocument.php',
'OCP\\FullTextSearch\\Exceptions\\FullTextSearchAppNotAvailableException' => $baseDir . '/lib/public/FullTextSearch/Exceptions/FullTextSearchAppNotAvailableException.php',
@@ -1125,6 +1130,7 @@ return array(
'OC\\Files\\Stream\\HashWrapper' => $baseDir . '/lib/private/Files/Stream/HashWrapper.php',
'OC\\Files\\Stream\\Quota' => $baseDir . '/lib/private/Files/Stream/Quota.php',
'OC\\Files\\Stream\\SeekableHttpStream' => $baseDir . '/lib/private/Files/Stream/SeekableHttpStream.php',
'OC\\Files\\Template\\TemplateManager' => $baseDir . '/lib/private/Files/Template/TemplateManager.php',
'OC\\Files\\Type\\Detection' => $baseDir . '/lib/private/Files/Type/Detection.php',
'OC\\Files\\Type\\Loader' => $baseDir . '/lib/private/Files/Type/Loader.php',
'OC\\Files\\Type\\TemplateManager' => $baseDir . '/lib/private/Files/Type/TemplateManager.php',

+ 6
- 0
lib/composer/composer/autoload_static.php View File

@@ -344,6 +344,11 @@ class ComposerStaticInit53792487c5a8370acc0b06b1a864ff4c
'OCP\\Files\\Storage\\IStorage' => __DIR__ . '/../../..' . '/lib/public/Files/Storage/IStorage.php',
'OCP\\Files\\Storage\\IStorageFactory' => __DIR__ . '/../../..' . '/lib/public/Files/Storage/IStorageFactory.php',
'OCP\\Files\\Storage\\IWriteStreamStorage' => __DIR__ . '/../../..' . '/lib/public/Files/Storage/IWriteStreamStorage.php',
'OCP\\Files\\Template\\CreatedFromTemplateEvent' => __DIR__ . '/../../..' . '/lib/public/Files/Template/CreatedFromTemplateEvent.php',
'OCP\\Files\\Template\\ICustomTemplateProvider' => __DIR__ . '/../../..' . '/lib/public/Files/Template/ICustomTemplateProvider.php',
'OCP\\Files\\Template\\ITemplateManager' => __DIR__ . '/../../..' . '/lib/public/Files/Template/ITemplateManager.php',
'OCP\\Files\\Template\\Template' => __DIR__ . '/../../..' . '/lib/public/Files/Template/Template.php',
'OCP\\Files\\Template\\TemplateFileCreator' => __DIR__ . '/../../..' . '/lib/public/Files/Template/TemplateFileCreator.php',
'OCP\\Files\\UnseekableException' => __DIR__ . '/../../..' . '/lib/public/Files/UnseekableException.php',
'OCP\\Files_FullTextSearch\\Model\\AFilesDocument' => __DIR__ . '/../../..' . '/lib/public/Files_FullTextSearch/Model/AFilesDocument.php',
'OCP\\FullTextSearch\\Exceptions\\FullTextSearchAppNotAvailableException' => __DIR__ . '/../../..' . '/lib/public/FullTextSearch/Exceptions/FullTextSearchAppNotAvailableException.php',
@@ -1154,6 +1159,7 @@ class ComposerStaticInit53792487c5a8370acc0b06b1a864ff4c
'OC\\Files\\Stream\\HashWrapper' => __DIR__ . '/../../..' . '/lib/private/Files/Stream/HashWrapper.php',
'OC\\Files\\Stream\\Quota' => __DIR__ . '/../../..' . '/lib/private/Files/Stream/Quota.php',
'OC\\Files\\Stream\\SeekableHttpStream' => __DIR__ . '/../../..' . '/lib/private/Files/Stream/SeekableHttpStream.php',
'OC\\Files\\Template\\TemplateManager' => __DIR__ . '/../../..' . '/lib/private/Files/Template/TemplateManager.php',
'OC\\Files\\Type\\Detection' => __DIR__ . '/../../..' . '/lib/private/Files/Type/Detection.php',
'OC\\Files\\Type\\Loader' => __DIR__ . '/../../..' . '/lib/private/Files/Type/Loader.php',
'OC\\Files\\Type\\TemplateManager' => __DIR__ . '/../../..' . '/lib/private/Files/Type/TemplateManager.php',

+ 237
- 0
lib/private/Files/Template/TemplateManager.php View File

@@ -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);
}
}

+ 3
- 0
lib/private/Server.php View File

@@ -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);


+ 3
- 0
lib/private/legacy/OC_Util.php View File

@@ -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);
}
}


+ 64
- 0
lib/public/Files/Template/CreatedFromTemplateEvent.php View File

@@ -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;
}
}

+ 51
- 0
lib/public/Files/Template/ICustomTemplateProvider.php View File

@@ -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;
}

+ 94
- 0
lib/public/Files/Template/ITemplateManager.php View File

@@ -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;
}

+ 68
- 0
lib/public/Files/Template/Template.php View File

@@ -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
];
}
}

+ 73
- 0
lib/public/Files/Template/TemplateFileCreator.php View File

@@ -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
];
}
}

Loading…
Cancel
Save