ResourceProvider.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2018 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OCA\Files\Collaboration\Resources;
  8. use OCP\Collaboration\Resources\IProvider;
  9. use OCP\Collaboration\Resources\IResource;
  10. use OCP\Collaboration\Resources\ResourceException;
  11. use OCP\Files\IRootFolder;
  12. use OCP\Files\Node;
  13. use OCP\IPreview;
  14. use OCP\IURLGenerator;
  15. use OCP\IUser;
  16. class ResourceProvider implements IProvider {
  17. public const RESOURCE_TYPE = 'file';
  18. /** @var IRootFolder */
  19. protected $rootFolder;
  20. /** @var IPreview */
  21. private $preview;
  22. /** @var IURLGenerator */
  23. private $urlGenerator;
  24. /** @var array */
  25. protected $nodes = [];
  26. public function __construct(IRootFolder $rootFolder,
  27. IPreview $preview,
  28. IURLGenerator $urlGenerator) {
  29. $this->rootFolder = $rootFolder;
  30. $this->preview = $preview;
  31. $this->urlGenerator = $urlGenerator;
  32. }
  33. private function getNode(IResource $resource): ?Node {
  34. if (isset($this->nodes[(int) $resource->getId()])) {
  35. return $this->nodes[(int) $resource->getId()];
  36. }
  37. $node = $this->rootFolder->getFirstNodeById((int) $resource->getId());
  38. if ($node) {
  39. $this->nodes[(int) $resource->getId()] = $node;
  40. return $this->nodes[(int) $resource->getId()];
  41. }
  42. return null;
  43. }
  44. /**
  45. * @param IResource $resource
  46. * @return array
  47. * @since 16.0.0
  48. */
  49. public function getResourceRichObject(IResource $resource): array {
  50. if (isset($this->nodes[(int) $resource->getId()])) {
  51. $node = $this->nodes[(int) $resource->getId()]->getPath();
  52. } else {
  53. $node = $this->getNode($resource);
  54. }
  55. if ($node instanceof Node) {
  56. $link = $this->urlGenerator->linkToRouteAbsolute(
  57. 'files.viewcontroller.showFile',
  58. ['fileid' => $resource->getId()]
  59. );
  60. return [
  61. 'type' => 'file',
  62. 'id' => $resource->getId(),
  63. 'name' => $node->getName(),
  64. 'path' => $node->getInternalPath(),
  65. 'link' => $link,
  66. 'mimetype' => $node->getMimetype(),
  67. 'preview-available' => $this->preview->isAvailable($node),
  68. ];
  69. }
  70. throw new ResourceException('File not found');
  71. }
  72. /**
  73. * Can a user/guest access the collection
  74. *
  75. * @param IResource $resource
  76. * @param IUser $user
  77. * @return bool
  78. * @since 16.0.0
  79. */
  80. public function canAccessResource(IResource $resource, ?IUser $user = null): bool {
  81. if (!$user instanceof IUser) {
  82. return false;
  83. }
  84. $userFolder = $this->rootFolder->getUserFolder($user->getUID());
  85. $node = $userFolder->getById((int) $resource->getId());
  86. if ($node) {
  87. $this->nodes[(int) $resource->getId()] = $node;
  88. return true;
  89. }
  90. return false;
  91. }
  92. /**
  93. * Get the resource type of the provider
  94. *
  95. * @return string
  96. * @since 16.0.0
  97. */
  98. public function getType(): string {
  99. return self::RESOURCE_TYPE;
  100. }
  101. }