TemplateController.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2021 Julius Härtl <jus@bitgrid.net>
  5. *
  6. * @author John Molakvoæ <skjnldsv@protonmail.com>
  7. * @author Julius Härtl <jus@bitgrid.net>
  8. *
  9. * @license GNU AGPL version 3 or any later version
  10. *
  11. * This program is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU Affero General Public License as
  13. * published by the Free Software Foundation, either version 3 of the
  14. * License, or (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU Affero General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Affero General Public License
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  23. *
  24. */
  25. namespace OCA\Files\Controller;
  26. use OCA\Files\ResponseDefinitions;
  27. use OCP\AppFramework\Http;
  28. use OCP\AppFramework\Http\DataResponse;
  29. use OCP\AppFramework\OCS\OCSForbiddenException;
  30. use OCP\AppFramework\OCSController;
  31. use OCP\Files\GenericFileException;
  32. use OCP\Files\Template\ITemplateManager;
  33. use OCP\Files\Template\TemplateFileCreator;
  34. use OCP\IRequest;
  35. /**
  36. * @psalm-import-type FilesTemplate from ResponseDefinitions
  37. * @psalm-import-type FilesTemplateFile from ResponseDefinitions
  38. * @psalm-import-type FilesTemplateFileCreator from ResponseDefinitions
  39. */
  40. class TemplateController extends OCSController {
  41. protected $templateManager;
  42. public function __construct($appName, IRequest $request, ITemplateManager $templateManager) {
  43. parent::__construct($appName, $request);
  44. $this->templateManager = $templateManager;
  45. }
  46. /**
  47. * @NoAdminRequired
  48. *
  49. * List the available templates
  50. *
  51. * @return DataResponse<Http::STATUS_OK, array<FilesTemplateFileCreator>, array{}>
  52. */
  53. public function list(): DataResponse {
  54. return new DataResponse($this->templateManager->listTemplates());
  55. }
  56. /**
  57. * @NoAdminRequired
  58. *
  59. * Create a template
  60. *
  61. * @param string $filePath Path of the file
  62. * @param string $templatePath Name of the template
  63. * @param string $templateType Type of the template
  64. *
  65. * @return DataResponse<Http::STATUS_OK, FilesTemplateFile, array{}>
  66. * @throws OCSForbiddenException Creating template is not allowed
  67. *
  68. * 200: Template created successfully
  69. */
  70. public function create(string $filePath, string $templatePath = '', string $templateType = 'user'): DataResponse {
  71. try {
  72. return new DataResponse($this->templateManager->createFromTemplate($filePath, $templatePath, $templateType));
  73. } catch (GenericFileException $e) {
  74. throw new OCSForbiddenException($e->getMessage());
  75. }
  76. }
  77. /**
  78. * @NoAdminRequired
  79. *
  80. * Initialize the template directory
  81. *
  82. * @param string $templatePath Path of the template directory
  83. * @param bool $copySystemTemplates Whether to copy the system templates to the template directory
  84. *
  85. * @return DataResponse<Http::STATUS_OK, array{template_path: string, templates: FilesTemplateFileCreator[]}, array{}>
  86. * @throws OCSForbiddenException Initializing the template directory is not allowed
  87. *
  88. * 200: Template directory initialized successfully
  89. */
  90. public function path(string $templatePath = '', bool $copySystemTemplates = false) {
  91. try {
  92. /** @var string $templatePath */
  93. $templatePath = $this->templateManager->initializeTemplateDirectory($templatePath, null, $copySystemTemplates);
  94. return new DataResponse([
  95. 'template_path' => $templatePath,
  96. 'templates' => array_map(fn(TemplateFileCreator $creator) => $creator->jsonSerialize(), $this->templateManager->listCreators()),
  97. ]);
  98. } catch (\Exception $e) {
  99. throw new OCSForbiddenException($e->getMessage());
  100. }
  101. }
  102. }