1
0

DirectEditingService.php 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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\Service;
  7. use OCP\DirectEditing\ACreateEmpty;
  8. use OCP\DirectEditing\ACreateFromTemplate;
  9. use OCP\DirectEditing\IEditor;
  10. use OCP\DirectEditing\IManager;
  11. use OCP\DirectEditing\RegisterDirectEditorEvent;
  12. use OCP\EventDispatcher\IEventDispatcher;
  13. class DirectEditingService {
  14. /** @var IManager */
  15. private $directEditingManager;
  16. /** @var IEventDispatcher */
  17. private $eventDispatcher;
  18. public function __construct(IEventDispatcher $eventDispatcher, IManager $directEditingManager) {
  19. $this->directEditingManager = $directEditingManager;
  20. $this->eventDispatcher = $eventDispatcher;
  21. }
  22. public function getDirectEditingETag(): string {
  23. return \md5(\json_encode($this->getDirectEditingCapabilitites()));
  24. }
  25. public function getDirectEditingCapabilitites(): array {
  26. $this->eventDispatcher->dispatchTyped(new RegisterDirectEditorEvent($this->directEditingManager));
  27. $capabilities = [
  28. 'editors' => [],
  29. 'creators' => []
  30. ];
  31. if (!$this->directEditingManager->isEnabled()) {
  32. return $capabilities;
  33. }
  34. /**
  35. * @var string $id
  36. * @var IEditor $editor
  37. */
  38. foreach ($this->directEditingManager->getEditors() as $id => $editor) {
  39. $capabilities['editors'][$id] = [
  40. 'id' => $editor->getId(),
  41. 'name' => $editor->getName(),
  42. 'mimetypes' => $editor->getMimetypes(),
  43. 'optionalMimetypes' => $editor->getMimetypesOptional(),
  44. 'secure' => $editor->isSecure(),
  45. ];
  46. /** @var ACreateEmpty|ACreateFromTemplate $creator */
  47. foreach ($editor->getCreators() as $creator) {
  48. $id = $creator->getId();
  49. $capabilities['creators'][$id] = [
  50. 'id' => $id,
  51. 'editor' => $editor->getId(),
  52. 'name' => $creator->getName(),
  53. 'extension' => $creator->getExtension(),
  54. 'templates' => $creator instanceof ACreateFromTemplate,
  55. 'mimetype' => $creator->getMimetype()
  56. ];
  57. }
  58. }
  59. return $capabilities;
  60. }
  61. }