TemplateController.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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 FilesTemplateFile from ResponseDefinitions
  37. * @psalm-import-type FilesTemplateFileCreator from ResponseDefinitions
  38. */
  39. class TemplateController extends OCSController {
  40. protected $templateManager;
  41. public function __construct($appName, IRequest $request, ITemplateManager $templateManager) {
  42. parent::__construct($appName, $request);
  43. $this->templateManager = $templateManager;
  44. }
  45. /**
  46. * @NoAdminRequired
  47. *
  48. * List the available templates
  49. *
  50. * @return DataResponse<Http::STATUS_OK, array<FilesTemplateFileCreator>, array{}>
  51. *
  52. * 200: Available templates returned
  53. */
  54. public function list(): DataResponse {
  55. return new DataResponse($this->templateManager->listTemplates());
  56. }
  57. /**
  58. * @NoAdminRequired
  59. *
  60. * Create a template
  61. *
  62. * @param string $filePath Path of the file
  63. * @param string $templatePath Name of the template
  64. * @param string $templateType Type of the template
  65. *
  66. * @return DataResponse<Http::STATUS_OK, FilesTemplateFile, array{}>
  67. * @throws OCSForbiddenException Creating template is not allowed
  68. *
  69. * 200: Template created successfully
  70. */
  71. public function create(string $filePath, string $templatePath = '', string $templateType = 'user'): DataResponse {
  72. try {
  73. return new DataResponse($this->templateManager->createFromTemplate($filePath, $templatePath, $templateType));
  74. } catch (GenericFileException $e) {
  75. throw new OCSForbiddenException($e->getMessage());
  76. }
  77. }
  78. /**
  79. * @NoAdminRequired
  80. *
  81. * Initialize the template directory
  82. *
  83. * @param string $templatePath Path of the template directory
  84. * @param bool $copySystemTemplates Whether to copy the system templates to the template directory
  85. *
  86. * @return DataResponse<Http::STATUS_OK, array{template_path: string, templates: FilesTemplateFileCreator[]}, array{}>
  87. * @throws OCSForbiddenException Initializing the template directory is not allowed
  88. *
  89. * 200: Template directory initialized successfully
  90. */
  91. public function path(string $templatePath = '', bool $copySystemTemplates = false) {
  92. try {
  93. /** @var string $templatePath */
  94. $templatePath = $this->templateManager->initializeTemplateDirectory($templatePath, null, $copySystemTemplates);
  95. return new DataResponse([
  96. 'template_path' => $templatePath,
  97. 'templates' => array_map(fn (TemplateFileCreator $creator) => $creator->jsonSerialize(), $this->templateManager->listCreators()),
  98. ]);
  99. } catch (\Exception $e) {
  100. throw new OCSForbiddenException($e->getMessage());
  101. }
  102. }
  103. }