TemplateController.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. * 200: Available templates returned
  54. */
  55. public function list(): DataResponse {
  56. return new DataResponse($this->templateManager->listTemplates());
  57. }
  58. /**
  59. * @NoAdminRequired
  60. *
  61. * Create a template
  62. *
  63. * @param string $filePath Path of the file
  64. * @param string $templatePath Name of the template
  65. * @param string $templateType Type of the template
  66. *
  67. * @return DataResponse<Http::STATUS_OK, FilesTemplateFile, array{}>
  68. * @throws OCSForbiddenException Creating template is not allowed
  69. *
  70. * 200: Template created successfully
  71. */
  72. public function create(string $filePath, string $templatePath = '', string $templateType = 'user'): DataResponse {
  73. try {
  74. return new DataResponse($this->templateManager->createFromTemplate($filePath, $templatePath, $templateType));
  75. } catch (GenericFileException $e) {
  76. throw new OCSForbiddenException($e->getMessage());
  77. }
  78. }
  79. /**
  80. * @NoAdminRequired
  81. *
  82. * Initialize the template directory
  83. *
  84. * @param string $templatePath Path of the template directory
  85. * @param bool $copySystemTemplates Whether to copy the system templates to the template directory
  86. *
  87. * @return DataResponse<Http::STATUS_OK, array{template_path: string, templates: FilesTemplateFileCreator[]}, array{}>
  88. * @throws OCSForbiddenException Initializing the template directory is not allowed
  89. *
  90. * 200: Template directory initialized successfully
  91. */
  92. public function path(string $templatePath = '', bool $copySystemTemplates = false) {
  93. try {
  94. /** @var string $templatePath */
  95. $templatePath = $this->templateManager->initializeTemplateDirectory($templatePath, null, $copySystemTemplates);
  96. return new DataResponse([
  97. 'template_path' => $templatePath,
  98. 'templates' => array_map(fn(TemplateFileCreator $creator) => $creator->jsonSerialize(), $this->templateManager->listCreators()),
  99. ]);
  100. } catch (\Exception $e) {
  101. throw new OCSForbiddenException($e->getMessage());
  102. }
  103. }
  104. }