DirectEditingController.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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\ACreateEmpty;
  30. use OCP\DirectEditing\ACreateFromTemplate;
  31. use OCP\DirectEditing\IEditor;
  32. use OCP\DirectEditing\IManager;
  33. use OCP\DirectEditing\RegisterDirectEditorEvent;
  34. use OCP\EventDispatcher\IEventDispatcher;
  35. use OCP\ILogger;
  36. use OCP\IRequest;
  37. use OCP\IURLGenerator;
  38. class DirectEditingController extends OCSController {
  39. /** @var IEventDispatcher */
  40. private $eventDispatcher;
  41. /** @var IManager */
  42. private $directEditingManager;
  43. /** @var IURLGenerator */
  44. private $urlGenerator;
  45. /** @var ILogger */
  46. private $logger;
  47. /** @var DirectEditingService */
  48. private $directEditingService;
  49. public function __construct($appName, IRequest $request, $corsMethods, $corsAllowedHeaders, $corsMaxAge,
  50. IEventDispatcher $eventDispatcher, IURLGenerator $urlGenerator, IManager $manager, DirectEditingService $directEditingService, ILogger $logger) {
  51. parent::__construct($appName, $request, $corsMethods, $corsAllowedHeaders, $corsMaxAge);
  52. $this->eventDispatcher = $eventDispatcher;
  53. $this->directEditingManager = $manager;
  54. $this->directEditingService = $directEditingService;
  55. $this->logger = $logger;
  56. $this->urlGenerator = $urlGenerator;
  57. }
  58. /**
  59. * @NoAdminRequired
  60. */
  61. public function info(): DataResponse {
  62. $response = new DataResponse($this->directEditingService->getDirectEditingCapabilitites());
  63. $response->setETag($this->directEditingService->getDirectEditingETag());
  64. return $response;
  65. }
  66. /**
  67. * @NoAdminRequired
  68. */
  69. public function create(string $path, string $editorId, string $creatorId, string $templateId = null): DataResponse {
  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('Failed to create file', Http::STATUS_FORBIDDEN);
  79. }
  80. }
  81. /**
  82. * @NoAdminRequired
  83. */
  84. public function open(int $fileId, string $editorId = null): DataResponse {
  85. $this->eventDispatcher->dispatchTyped(new RegisterDirectEditorEvent($this->directEditingManager));
  86. try {
  87. $token = $this->directEditingManager->open($fileId, $editorId);
  88. return new DataResponse([
  89. 'url' => $this->urlGenerator->linkToRouteAbsolute('files.DirectEditingView.edit', ['token' => $token])
  90. ]);
  91. } catch (Exception $e) {
  92. $this->logger->logException($e, ['message' => 'Exception when opening a file through direct editing']);
  93. return new DataResponse('Failed to open file', Http::STATUS_FORBIDDEN);
  94. }
  95. }
  96. /**
  97. * @NoAdminRequired
  98. */
  99. public function templates(string $editorId, string $creatorId): DataResponse {
  100. $this->eventDispatcher->dispatchTyped(new RegisterDirectEditorEvent($this->directEditingManager));
  101. try {
  102. return new DataResponse($this->directEditingManager->getTemplates($editorId, $creatorId));
  103. } catch (Exception $e) {
  104. $this->logger->logException($e);
  105. return new DataResponse('Failed to open file', Http::STATUS_INTERNAL_SERVER_ERROR);
  106. }
  107. }
  108. }