ResourceProvider.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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 array */
  19. protected $nodes = [];
  20. public function __construct(
  21. protected IRootFolder $rootFolder,
  22. private IPreview $preview,
  23. private IURLGenerator $urlGenerator,
  24. ) {
  25. }
  26. private function getNode(IResource $resource): ?Node {
  27. if (isset($this->nodes[(int)$resource->getId()])) {
  28. return $this->nodes[(int)$resource->getId()];
  29. }
  30. $node = $this->rootFolder->getFirstNodeById((int)$resource->getId());
  31. if ($node) {
  32. $this->nodes[(int)$resource->getId()] = $node;
  33. return $this->nodes[(int)$resource->getId()];
  34. }
  35. return null;
  36. }
  37. /**
  38. * @param IResource $resource
  39. * @return array
  40. * @since 16.0.0
  41. */
  42. public function getResourceRichObject(IResource $resource): array {
  43. if (isset($this->nodes[(int)$resource->getId()])) {
  44. $node = $this->nodes[(int)$resource->getId()]->getPath();
  45. } else {
  46. $node = $this->getNode($resource);
  47. }
  48. if ($node instanceof Node) {
  49. $link = $this->urlGenerator->linkToRouteAbsolute(
  50. 'files.viewcontroller.showFile',
  51. ['fileid' => $resource->getId()]
  52. );
  53. return [
  54. 'type' => 'file',
  55. 'id' => $resource->getId(),
  56. 'name' => $node->getName(),
  57. 'path' => $node->getInternalPath(),
  58. 'link' => $link,
  59. 'mimetype' => $node->getMimetype(),
  60. 'preview-available' => $this->preview->isAvailable($node),
  61. ];
  62. }
  63. throw new ResourceException('File not found');
  64. }
  65. /**
  66. * Can a user/guest access the collection
  67. *
  68. * @param IResource $resource
  69. * @param IUser $user
  70. * @return bool
  71. * @since 16.0.0
  72. */
  73. public function canAccessResource(IResource $resource, ?IUser $user = null): bool {
  74. if (!$user instanceof IUser) {
  75. return false;
  76. }
  77. $userFolder = $this->rootFolder->getUserFolder($user->getUID());
  78. $node = $userFolder->getById((int)$resource->getId());
  79. if ($node) {
  80. $this->nodes[(int)$resource->getId()] = $node;
  81. return true;
  82. }
  83. return false;
  84. }
  85. /**
  86. * Get the resource type of the provider
  87. *
  88. * @return string
  89. * @since 16.0.0
  90. */
  91. public function getType(): string {
  92. return self::RESOURCE_TYPE;
  93. }
  94. }