ResourceProvider.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2018 Joas Schilling <coding@schilljs.com>
  5. *
  6. * @author Joas Schilling <coding@schilljs.com>
  7. * @author Julius Härtl <jus@bitgrid.net>
  8. *
  9. * @license GNU AGPL version 3 or any later version
  10. *
  11. * This program is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU Affero General Public License as
  13. * published by the Free Software Foundation, either version 3 of the
  14. * License, or (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU Affero General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Affero General Public License
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  23. *
  24. */
  25. namespace OCA\Files\Collaboration\Resources;
  26. use OCP\Collaboration\Resources\IProvider;
  27. use OCP\Collaboration\Resources\IResource;
  28. use OCP\Collaboration\Resources\ResourceException;
  29. use OCP\Files\IRootFolder;
  30. use OCP\Files\Node;
  31. use OCP\IPreview;
  32. use OCP\IURLGenerator;
  33. use OCP\IUser;
  34. class ResourceProvider implements IProvider {
  35. public const RESOURCE_TYPE = 'file';
  36. /** @var IRootFolder */
  37. protected $rootFolder;
  38. /** @var IPreview */
  39. private $preview;
  40. /** @var IURLGenerator */
  41. private $urlGenerator;
  42. /** @var array */
  43. protected $nodes = [];
  44. public function __construct(IRootFolder $rootFolder,
  45. IPreview $preview,
  46. IURLGenerator $urlGenerator) {
  47. $this->rootFolder = $rootFolder;
  48. $this->preview = $preview;
  49. $this->urlGenerator = $urlGenerator;
  50. }
  51. private function getNode(IResource $resource): ?Node {
  52. if (isset($this->nodes[(int) $resource->getId()])) {
  53. return $this->nodes[(int) $resource->getId()];
  54. }
  55. $node = $this->rootFolder->getFirstNodeById((int) $resource->getId());
  56. if ($node) {
  57. $this->nodes[(int) $resource->getId()] = $node;
  58. return $this->nodes[(int) $resource->getId()];
  59. }
  60. return null;
  61. }
  62. /**
  63. * @param IResource $resource
  64. * @return array
  65. * @since 16.0.0
  66. */
  67. public function getResourceRichObject(IResource $resource): array {
  68. if (isset($this->nodes[(int) $resource->getId()])) {
  69. $node = $this->nodes[(int) $resource->getId()]->getPath();
  70. } else {
  71. $node = $this->getNode($resource);
  72. }
  73. if ($node instanceof Node) {
  74. $link = $this->urlGenerator->linkToRouteAbsolute(
  75. 'files.viewcontroller.showFile',
  76. ['fileid' => $resource->getId()]
  77. );
  78. return [
  79. 'type' => 'file',
  80. 'id' => $resource->getId(),
  81. 'name' => $node->getName(),
  82. 'path' => $node->getInternalPath(),
  83. 'link' => $link,
  84. 'mimetype' => $node->getMimetype(),
  85. 'preview-available' => $this->preview->isAvailable($node),
  86. ];
  87. }
  88. throw new ResourceException('File not found');
  89. }
  90. /**
  91. * Can a user/guest access the collection
  92. *
  93. * @param IResource $resource
  94. * @param IUser $user
  95. * @return bool
  96. * @since 16.0.0
  97. */
  98. public function canAccessResource(IResource $resource, IUser $user = null): bool {
  99. if (!$user instanceof IUser) {
  100. return false;
  101. }
  102. $userFolder = $this->rootFolder->getUserFolder($user->getUID());
  103. $node = $userFolder->getById((int) $resource->getId());
  104. if ($node) {
  105. $this->nodes[(int) $resource->getId()] = $node;
  106. return true;
  107. }
  108. return false;
  109. }
  110. /**
  111. * Get the resource type of the provider
  112. *
  113. * @return string
  114. * @since 16.0.0
  115. */
  116. public function getType(): string {
  117. return self::RESOURCE_TYPE;
  118. }
  119. }