DirectEditingController.php 5.5 KB

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