1
0

TemplateController.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2021 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OCA\Files\Controller;
  8. use OCA\Files\ResponseDefinitions;
  9. use OCP\AppFramework\Http;
  10. use OCP\AppFramework\Http\DataResponse;
  11. use OCP\AppFramework\OCS\OCSForbiddenException;
  12. use OCP\AppFramework\OCSController;
  13. use OCP\Files\GenericFileException;
  14. use OCP\Files\Template\ITemplateManager;
  15. use OCP\Files\Template\TemplateFileCreator;
  16. use OCP\IRequest;
  17. /**
  18. * @psalm-import-type FilesTemplateFile from ResponseDefinitions
  19. * @psalm-import-type FilesTemplateFileCreator from ResponseDefinitions
  20. */
  21. class TemplateController extends OCSController {
  22. protected $templateManager;
  23. public function __construct($appName, IRequest $request, ITemplateManager $templateManager) {
  24. parent::__construct($appName, $request);
  25. $this->templateManager = $templateManager;
  26. }
  27. /**
  28. * @NoAdminRequired
  29. *
  30. * List the available templates
  31. *
  32. * @return DataResponse<Http::STATUS_OK, array<FilesTemplateFileCreator>, array{}>
  33. *
  34. * 200: Available templates returned
  35. */
  36. public function list(): DataResponse {
  37. return new DataResponse($this->templateManager->listTemplates());
  38. }
  39. /**
  40. * @NoAdminRequired
  41. *
  42. * Create a template
  43. *
  44. * @param string $filePath Path of the file
  45. * @param string $templatePath Name of the template
  46. * @param string $templateType Type of the template
  47. *
  48. * @return DataResponse<Http::STATUS_OK, FilesTemplateFile, array{}>
  49. * @throws OCSForbiddenException Creating template is not allowed
  50. *
  51. * 200: Template created successfully
  52. */
  53. public function create(string $filePath, string $templatePath = '', string $templateType = 'user'): DataResponse {
  54. try {
  55. return new DataResponse($this->templateManager->createFromTemplate($filePath, $templatePath, $templateType));
  56. } catch (GenericFileException $e) {
  57. throw new OCSForbiddenException($e->getMessage());
  58. }
  59. }
  60. /**
  61. * @NoAdminRequired
  62. *
  63. * Initialize the template directory
  64. *
  65. * @param string $templatePath Path of the template directory
  66. * @param bool $copySystemTemplates Whether to copy the system templates to the template directory
  67. *
  68. * @return DataResponse<Http::STATUS_OK, array{template_path: string, templates: FilesTemplateFileCreator[]}, array{}>
  69. * @throws OCSForbiddenException Initializing the template directory is not allowed
  70. *
  71. * 200: Template directory initialized successfully
  72. */
  73. public function path(string $templatePath = '', bool $copySystemTemplates = false) {
  74. try {
  75. /** @var string $templatePath */
  76. $templatePath = $this->templateManager->initializeTemplateDirectory($templatePath, null, $copySystemTemplates);
  77. return new DataResponse([
  78. 'template_path' => $templatePath,
  79. 'templates' => array_map(fn (TemplateFileCreator $creator) => $creator->jsonSerialize(), $this->templateManager->listCreators()),
  80. ]);
  81. } catch (\Exception $e) {
  82. throw new OCSForbiddenException($e->getMessage());
  83. }
  84. }
  85. }