1
0

DirectEditingService.php 1.8 KB

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