1
0

DirectEditingController.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2019 Julius Härtl <jus@bitgrid.net>
  4. *
  5. * @author Julius Härtl <jus@bitgrid.net>
  6. *
  7. * @license GNU AGPL version 3 or any later version
  8. *
  9. * This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License as
  11. * published by the Free Software Foundation, either version 3 of the
  12. * License, or (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. *
  22. */
  23. namespace OCA\Files\Controller;
  24. use Exception;
  25. use OCA\Files\Service\DirectEditingService;
  26. use OCP\AppFramework\Http;
  27. use OCP\AppFramework\Http\DataResponse;
  28. use OCP\AppFramework\OCSController;
  29. use OCP\DirectEditing\IManager;
  30. use OCP\DirectEditing\RegisterDirectEditorEvent;
  31. use OCP\EventDispatcher\IEventDispatcher;
  32. use OCP\ILogger;
  33. use OCP\IRequest;
  34. use OCP\IURLGenerator;
  35. class DirectEditingController extends OCSController {
  36. /** @var IEventDispatcher */
  37. private $eventDispatcher;
  38. /** @var IManager */
  39. private $directEditingManager;
  40. /** @var IURLGenerator */
  41. private $urlGenerator;
  42. /** @var ILogger */
  43. private $logger;
  44. /** @var DirectEditingService */
  45. private $directEditingService;
  46. public function __construct($appName, IRequest $request, $corsMethods, $corsAllowedHeaders, $corsMaxAge,
  47. IEventDispatcher $eventDispatcher, IURLGenerator $urlGenerator, IManager $manager, DirectEditingService $directEditingService, ILogger $logger) {
  48. parent::__construct($appName, $request, $corsMethods, $corsAllowedHeaders, $corsMaxAge);
  49. $this->eventDispatcher = $eventDispatcher;
  50. $this->directEditingManager = $manager;
  51. $this->directEditingService = $directEditingService;
  52. $this->logger = $logger;
  53. $this->urlGenerator = $urlGenerator;
  54. }
  55. /**
  56. * @NoAdminRequired
  57. */
  58. public function info(): DataResponse {
  59. $response = new DataResponse($this->directEditingService->getDirectEditingCapabilitites());
  60. $response->setETag($this->directEditingService->getDirectEditingETag());
  61. return $response;
  62. }
  63. /**
  64. * @NoAdminRequired
  65. */
  66. public function create(string $path, string $editorId, string $creatorId, string $templateId = null): DataResponse {
  67. if (!$this->directEditingManager->isEnabled()) {
  68. return new DataResponse(['message' => 'Direct editing is not enabled'], Http::STATUS_INTERNAL_SERVER_ERROR);
  69. }
  70. $this->eventDispatcher->dispatchTyped(new RegisterDirectEditorEvent($this->directEditingManager));
  71. try {
  72. $token = $this->directEditingManager->create($path, $editorId, $creatorId, $templateId);
  73. return new DataResponse([
  74. 'url' => $this->urlGenerator->linkToRouteAbsolute('files.DirectEditingView.edit', ['token' => $token])
  75. ]);
  76. } catch (Exception $e) {
  77. $this->logger->logException($e, ['message' => 'Exception when creating a new file through direct editing']);
  78. return new DataResponse(['message' => 'Failed to create file: ' . $e->getMessage()], Http::STATUS_FORBIDDEN);
  79. }
  80. }
  81. /**
  82. * @NoAdminRequired
  83. */
  84. public function open(string $path, string $editorId = null): DataResponse {
  85. if (!$this->directEditingManager->isEnabled()) {
  86. return new DataResponse(['message' => 'Direct editing is not enabled'], Http::STATUS_INTERNAL_SERVER_ERROR);
  87. }
  88. $this->eventDispatcher->dispatchTyped(new RegisterDirectEditorEvent($this->directEditingManager));
  89. try {
  90. $token = $this->directEditingManager->open($path, $editorId);
  91. return new DataResponse([
  92. 'url' => $this->urlGenerator->linkToRouteAbsolute('files.DirectEditingView.edit', ['token' => $token])
  93. ]);
  94. } catch (Exception $e) {
  95. $this->logger->logException($e, ['message' => 'Exception when opening a file through direct editing']);
  96. return new DataResponse(['message' => 'Failed to open file: ' . $e->getMessage()], Http::STATUS_FORBIDDEN);
  97. }
  98. }
  99. /**
  100. * @NoAdminRequired
  101. */
  102. public function templates(string $editorId, string $creatorId): DataResponse {
  103. if (!$this->directEditingManager->isEnabled()) {
  104. return new DataResponse(['message' => 'Direct editing is not enabled'], Http::STATUS_INTERNAL_SERVER_ERROR);
  105. }
  106. $this->eventDispatcher->dispatchTyped(new RegisterDirectEditorEvent($this->directEditingManager));
  107. try {
  108. return new DataResponse($this->directEditingManager->getTemplates($editorId, $creatorId));
  109. } catch (Exception $e) {
  110. $this->logger->logException($e);
  111. return new DataResponse(['message' => 'Failed to obtain template list: ' . $e->getMessage()], Http::STATUS_INTERNAL_SERVER_ERROR);
  112. }
  113. }
  114. }