DirectEditingController.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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\IRequest;
  33. use OCP\IURLGenerator;
  34. use Psr\Log\LoggerInterface;
  35. class DirectEditingController extends OCSController {
  36. public function __construct(
  37. string $appName,
  38. IRequest $request,
  39. string $corsMethods,
  40. string $corsAllowedHeaders,
  41. int $corsMaxAge,
  42. private IEventDispatcher $eventDispatcher,
  43. private IURLGenerator $urlGenerator,
  44. private IManager $directEditingManager,
  45. private DirectEditingService $directEditingService,
  46. private LoggerInterface $logger
  47. ) {
  48. parent::__construct($appName, $request, $corsMethods, $corsAllowedHeaders, $corsMaxAge);
  49. }
  50. /**
  51. * @NoAdminRequired
  52. *
  53. * Get the direct editing capabilities
  54. * @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{}>
  55. *
  56. * 200: Direct editing capabilities returned
  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. * Create a file for direct editing
  67. *
  68. * @param string $path Path of the file
  69. * @param string $editorId ID of the editor
  70. * @param string $creatorId ID of the creator
  71. * @param ?string $templateId ID of the template
  72. *
  73. * @return DataResponse<Http::STATUS_OK, array{url: string}, array{}>|DataResponse<Http::STATUS_FORBIDDEN|Http::STATUS_INTERNAL_SERVER_ERROR, array{message: string}, array{}>
  74. *
  75. * 200: URL for direct editing returned
  76. * 403: Opening file is not allowed
  77. */
  78. public function create(string $path, string $editorId, string $creatorId, ?string $templateId = null): DataResponse {
  79. if (!$this->directEditingManager->isEnabled()) {
  80. return new DataResponse(['message' => 'Direct editing is not enabled'], Http::STATUS_INTERNAL_SERVER_ERROR);
  81. }
  82. $this->eventDispatcher->dispatchTyped(new RegisterDirectEditorEvent($this->directEditingManager));
  83. try {
  84. $token = $this->directEditingManager->create($path, $editorId, $creatorId, $templateId);
  85. return new DataResponse([
  86. 'url' => $this->urlGenerator->linkToRouteAbsolute('files.DirectEditingView.edit', ['token' => $token])
  87. ]);
  88. } catch (Exception $e) {
  89. $this->logger->error(
  90. 'Exception when creating a new file through direct editing',
  91. [
  92. 'exception' => $e
  93. ],
  94. );
  95. return new DataResponse(['message' => 'Failed to create file: ' . $e->getMessage()], Http::STATUS_FORBIDDEN);
  96. }
  97. }
  98. /**
  99. * @NoAdminRequired
  100. *
  101. * Open a file for direct editing
  102. *
  103. * @param string $path Path of the file
  104. * @param ?string $editorId ID of the editor
  105. * @param ?int $fileId ID of the file
  106. *
  107. * @return DataResponse<Http::STATUS_OK, array{url: string}, array{}>|DataResponse<Http::STATUS_FORBIDDEN|Http::STATUS_INTERNAL_SERVER_ERROR, array{message: string}, array{}>
  108. *
  109. * 200: URL for direct editing returned
  110. * 403: Opening file is not allowed
  111. */
  112. public function open(string $path, ?string $editorId = null, ?int $fileId = null): DataResponse {
  113. if (!$this->directEditingManager->isEnabled()) {
  114. return new DataResponse(['message' => 'Direct editing is not enabled'], Http::STATUS_INTERNAL_SERVER_ERROR);
  115. }
  116. $this->eventDispatcher->dispatchTyped(new RegisterDirectEditorEvent($this->directEditingManager));
  117. try {
  118. $token = $this->directEditingManager->open($path, $editorId, $fileId);
  119. return new DataResponse([
  120. 'url' => $this->urlGenerator->linkToRouteAbsolute('files.DirectEditingView.edit', ['token' => $token])
  121. ]);
  122. } catch (Exception $e) {
  123. $this->logger->error(
  124. 'Exception when opening a file through direct editing',
  125. [
  126. 'exception' => $e
  127. ],
  128. );
  129. return new DataResponse(['message' => 'Failed to open file: ' . $e->getMessage()], Http::STATUS_FORBIDDEN);
  130. }
  131. }
  132. /**
  133. * @NoAdminRequired
  134. *
  135. * Get the templates for direct editing
  136. *
  137. * @param string $editorId ID of the editor
  138. * @param string $creatorId ID of the creator
  139. *
  140. * @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{}>
  141. *
  142. * 200: Templates returned
  143. */
  144. public function templates(string $editorId, string $creatorId): DataResponse {
  145. if (!$this->directEditingManager->isEnabled()) {
  146. return new DataResponse(['message' => 'Direct editing is not enabled'], Http::STATUS_INTERNAL_SERVER_ERROR);
  147. }
  148. $this->eventDispatcher->dispatchTyped(new RegisterDirectEditorEvent($this->directEditingManager));
  149. try {
  150. return new DataResponse($this->directEditingManager->getTemplates($editorId, $creatorId));
  151. } catch (Exception $e) {
  152. $this->logger->error(
  153. $e->getMessage(),
  154. [
  155. 'exception' => $e
  156. ],
  157. );
  158. return new DataResponse(['message' => 'Failed to obtain template list: ' . $e->getMessage()], Http::STATUS_INTERNAL_SERVER_ERROR);
  159. }
  160. }
  161. }