DirectEditingService.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2019 Julius Härtl <jus@bitgrid.net>
  4. *
  5. * @author Julius Härtl <jus@bitgrid.net>
  6. * @author Tobias Kaminsky <tobias@kaminsky.me>
  7. *
  8. * @license GNU AGPL version 3 or any later version
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License as
  12. * published by the Free Software Foundation, either version 3 of the
  13. * License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. *
  23. */
  24. namespace OCA\Files\Service;
  25. use OCP\DirectEditing\ACreateEmpty;
  26. use OCP\DirectEditing\ACreateFromTemplate;
  27. use OCP\DirectEditing\IEditor;
  28. use OCP\DirectEditing\IManager;
  29. use OCP\DirectEditing\RegisterDirectEditorEvent;
  30. use OCP\EventDispatcher\IEventDispatcher;
  31. class DirectEditingService {
  32. /** @var IManager */
  33. private $directEditingManager;
  34. /** @var IEventDispatcher */
  35. private $eventDispatcher;
  36. public function __construct(IEventDispatcher $eventDispatcher, IManager $directEditingManager) {
  37. $this->directEditingManager = $directEditingManager;
  38. $this->eventDispatcher = $eventDispatcher;
  39. }
  40. public function getDirectEditingETag(): string {
  41. return \md5(\json_encode($this->getDirectEditingCapabilitites()));
  42. }
  43. public function getDirectEditingCapabilitites(): array {
  44. $this->eventDispatcher->dispatchTyped(new RegisterDirectEditorEvent($this->directEditingManager));
  45. $capabilities = [
  46. 'editors' => [],
  47. 'creators' => []
  48. ];
  49. /**
  50. * @var string $id
  51. * @var IEditor $editor
  52. */
  53. foreach ($this->directEditingManager->getEditors() as $id => $editor) {
  54. $capabilities['editors'][$id] = [
  55. 'id' => $editor->getId(),
  56. 'name' => $editor->getName(),
  57. 'mimetypes' => $editor->getMimetypes(),
  58. 'optionalMimetypes' => $editor->getMimetypesOptional(),
  59. 'secure' => $editor->isSecure(),
  60. ];
  61. /** @var ACreateEmpty|ACreateFromTemplate $creator */
  62. foreach ($editor->getCreators() as $creator) {
  63. $id = $creator->getId();
  64. $capabilities['creators'][$id] = [
  65. 'id' => $id,
  66. 'editor' => $editor->getId(),
  67. 'name' => $creator->getName(),
  68. 'extension' => $creator->getExtension(),
  69. 'templates' => $creator instanceof ACreateFromTemplate,
  70. 'mimetype' => $creator->getMimetype()
  71. ];
  72. }
  73. }
  74. return $capabilities;
  75. }
  76. }