DirectEditingController.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2019 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-License-Identifier: AGPL-3.0-or-later
  5. */
  6. namespace OCA\Files\Controller;
  7. use Exception;
  8. use OCA\Files\Service\DirectEditingService;
  9. use OCP\AppFramework\Http;
  10. use OCP\AppFramework\Http\DataResponse;
  11. use OCP\AppFramework\OCSController;
  12. use OCP\DirectEditing\IManager;
  13. use OCP\DirectEditing\RegisterDirectEditorEvent;
  14. use OCP\EventDispatcher\IEventDispatcher;
  15. use OCP\IRequest;
  16. use OCP\IURLGenerator;
  17. use Psr\Log\LoggerInterface;
  18. class DirectEditingController extends OCSController {
  19. public function __construct(
  20. string $appName,
  21. IRequest $request,
  22. string $corsMethods,
  23. string $corsAllowedHeaders,
  24. int $corsMaxAge,
  25. private IEventDispatcher $eventDispatcher,
  26. private IURLGenerator $urlGenerator,
  27. private IManager $directEditingManager,
  28. private DirectEditingService $directEditingService,
  29. private LoggerInterface $logger
  30. ) {
  31. parent::__construct($appName, $request, $corsMethods, $corsAllowedHeaders, $corsMaxAge);
  32. }
  33. /**
  34. * @NoAdminRequired
  35. *
  36. * Get the direct editing capabilities
  37. * @return DataResponse<Http::STATUS_OK, array{editors: array<string, array{id: string, name: string, mimetypes: string[], optionalMimetypes: string[], secure: bool}>, creators: array<string, array{id: string, editor: string, name: string, extension: string, templates: bool, mimetypes: string[]}>}, array{}>
  38. *
  39. * 200: Direct editing capabilities returned
  40. */
  41. public function info(): DataResponse {
  42. $response = new DataResponse($this->directEditingService->getDirectEditingCapabilitites());
  43. $response->setETag($this->directEditingService->getDirectEditingETag());
  44. return $response;
  45. }
  46. /**
  47. * @NoAdminRequired
  48. *
  49. * Create a file for direct editing
  50. *
  51. * @param string $path Path of the file
  52. * @param string $editorId ID of the editor
  53. * @param string $creatorId ID of the creator
  54. * @param ?string $templateId ID of the template
  55. *
  56. * @return DataResponse<Http::STATUS_OK, array{url: string}, array{}>|DataResponse<Http::STATUS_FORBIDDEN|Http::STATUS_INTERNAL_SERVER_ERROR, array{message: string}, array{}>
  57. *
  58. * 200: URL for direct editing returned
  59. * 403: Opening file is not allowed
  60. */
  61. public function create(string $path, string $editorId, string $creatorId, ?string $templateId = null): DataResponse {
  62. if (!$this->directEditingManager->isEnabled()) {
  63. return new DataResponse(['message' => 'Direct editing is not enabled'], Http::STATUS_INTERNAL_SERVER_ERROR);
  64. }
  65. $this->eventDispatcher->dispatchTyped(new RegisterDirectEditorEvent($this->directEditingManager));
  66. try {
  67. $token = $this->directEditingManager->create($path, $editorId, $creatorId, $templateId);
  68. return new DataResponse([
  69. 'url' => $this->urlGenerator->linkToRouteAbsolute('files.DirectEditingView.edit', ['token' => $token])
  70. ]);
  71. } catch (Exception $e) {
  72. $this->logger->error(
  73. 'Exception when creating a new file through direct editing',
  74. [
  75. 'exception' => $e
  76. ],
  77. );
  78. return new DataResponse(['message' => 'Failed to create file: ' . $e->getMessage()], Http::STATUS_FORBIDDEN);
  79. }
  80. }
  81. /**
  82. * @NoAdminRequired
  83. *
  84. * Open a file for direct editing
  85. *
  86. * @param string $path Path of the file
  87. * @param ?string $editorId ID of the editor
  88. * @param ?int $fileId ID of the file
  89. *
  90. * @return DataResponse<Http::STATUS_OK, array{url: string}, array{}>|DataResponse<Http::STATUS_FORBIDDEN|Http::STATUS_INTERNAL_SERVER_ERROR, array{message: string}, array{}>
  91. *
  92. * 200: URL for direct editing returned
  93. * 403: Opening file is not allowed
  94. */
  95. public function open(string $path, ?string $editorId = null, ?int $fileId = null): DataResponse {
  96. if (!$this->directEditingManager->isEnabled()) {
  97. return new DataResponse(['message' => 'Direct editing is not enabled'], Http::STATUS_INTERNAL_SERVER_ERROR);
  98. }
  99. $this->eventDispatcher->dispatchTyped(new RegisterDirectEditorEvent($this->directEditingManager));
  100. try {
  101. $token = $this->directEditingManager->open($path, $editorId, $fileId);
  102. return new DataResponse([
  103. 'url' => $this->urlGenerator->linkToRouteAbsolute('files.DirectEditingView.edit', ['token' => $token])
  104. ]);
  105. } catch (Exception $e) {
  106. $this->logger->error(
  107. 'Exception when opening a file through direct editing',
  108. [
  109. 'exception' => $e
  110. ],
  111. );
  112. return new DataResponse(['message' => 'Failed to open file: ' . $e->getMessage()], Http::STATUS_FORBIDDEN);
  113. }
  114. }
  115. /**
  116. * @NoAdminRequired
  117. *
  118. * Get the templates for direct editing
  119. *
  120. * @param string $editorId ID of the editor
  121. * @param string $creatorId ID of the creator
  122. *
  123. * @return DataResponse<Http::STATUS_OK, array{templates: array<string, array{id: string, title: string, preview: ?string, extension: string, mimetype: string}>}, array{}>|DataResponse<Http::STATUS_INTERNAL_SERVER_ERROR, array{message: string}, array{}>
  124. *
  125. * 200: Templates returned
  126. */
  127. public function templates(string $editorId, string $creatorId): DataResponse {
  128. if (!$this->directEditingManager->isEnabled()) {
  129. return new DataResponse(['message' => 'Direct editing is not enabled'], Http::STATUS_INTERNAL_SERVER_ERROR);
  130. }
  131. $this->eventDispatcher->dispatchTyped(new RegisterDirectEditorEvent($this->directEditingManager));
  132. try {
  133. return new DataResponse($this->directEditingManager->getTemplates($editorId, $creatorId));
  134. } catch (Exception $e) {
  135. $this->logger->error(
  136. $e->getMessage(),
  137. [
  138. 'exception' => $e
  139. ],
  140. );
  141. return new DataResponse(['message' => 'Failed to obtain template list: ' . $e->getMessage()], Http::STATUS_INTERNAL_SERVER_ERROR);
  142. }
  143. }
  144. }