You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

153 lines
5.0 KiB

  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2022 Julius Härtl <jus@bitgrid.net>
  5. *
  6. * @author Julius Härtl <jus@bitgrid.net>
  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. namespace OC\Collaboration\Reference\File;
  24. use OC\Collaboration\Reference\Reference;
  25. use OC\User\NoUserException;
  26. use OCP\Collaboration\Reference\IReference;
  27. use OCP\Collaboration\Reference\IReferenceProvider;
  28. use OCP\Files\InvalidPathException;
  29. use OCP\Files\IRootFolder;
  30. use OCP\Files\Node;
  31. use OCP\Files\NotFoundException;
  32. use OCP\Files\NotPermittedException;
  33. use OCP\IPreview;
  34. use OCP\IURLGenerator;
  35. use OCP\IUserSession;
  36. class FileReferenceProvider implements IReferenceProvider {
  37. private IURLGenerator $urlGenerator;
  38. private IRootFolder $rootFolder;
  39. private ?string $userId;
  40. private IPreview $previewManager;
  41. public function __construct(IURLGenerator $urlGenerator, IRootFolder $rootFolder, IUserSession $userSession, IPreview $previewManager) {
  42. $this->urlGenerator = $urlGenerator;
  43. $this->rootFolder = $rootFolder;
  44. $this->userId = $userSession->getUser() ? $userSession->getUser()->getUID() : null;
  45. $this->previewManager = $previewManager;
  46. }
  47. public function matchReference(string $referenceText): bool {
  48. return $this->getFilesAppLinkId($referenceText) !== null;
  49. }
  50. private function getFilesAppLinkId(string $referenceText): ?int {
  51. $start = $this->urlGenerator->getAbsoluteURL('/apps/files');
  52. $startIndex = $this->urlGenerator->getAbsoluteURL('/index.php/apps/files');
  53. $fileId = null;
  54. if (mb_strpos($referenceText, $start) === 0) {
  55. $parts = parse_url($referenceText);
  56. parse_str($parts['query'], $query);
  57. $fileId = isset($query['fileid']) ? (int)$query['fileid'] : $fileId;
  58. $fileId = isset($query['openfile']) ? (int)$query['openfile'] : $fileId;
  59. }
  60. if (mb_strpos($referenceText, $startIndex) === 0) {
  61. $parts = parse_url($referenceText);
  62. parse_str($parts['query'], $query);
  63. $fileId = isset($query['fileid']) ? (int)$query['fileid'] : $fileId;
  64. $fileId = isset($query['openfile']) ? (int)$query['openfile'] : $fileId;
  65. }
  66. if (mb_strpos($referenceText, $this->urlGenerator->getAbsoluteURL('/index.php/f/')) === 0) {
  67. $fileId = str_replace($this->urlGenerator->getAbsoluteURL('/index.php/f/'), '', $referenceText);
  68. }
  69. if (mb_strpos($referenceText, $this->urlGenerator->getAbsoluteURL('/f/')) === 0) {
  70. $fileId = str_replace($this->urlGenerator->getAbsoluteURL('/f/'), '', $referenceText);
  71. }
  72. return $fileId !== null ? (int)$fileId : null;
  73. }
  74. public function resolveReference(string $referenceText): ?IReference {
  75. if ($this->matchReference($referenceText)) {
  76. $reference = new Reference($referenceText);
  77. try {
  78. $this->fetchReference($reference);
  79. } catch (NotFoundException $e) {
  80. $reference->setRichObject('file', null);
  81. $reference->setAccessible(false);
  82. }
  83. return $reference;
  84. }
  85. return null;
  86. }
  87. /**
  88. * @throws NotFoundException
  89. */
  90. private function fetchReference(Reference $reference) {
  91. if ($this->userId === null) {
  92. throw new NotFoundException();
  93. }
  94. $fileId = $this->getFilesAppLinkId($reference->getId());
  95. if ($fileId === null) {
  96. throw new NotFoundException();
  97. }
  98. try {
  99. $userFolder = $this->rootFolder->getUserFolder($this->userId);
  100. $files = $userFolder->getById($fileId);
  101. if (empty($files)) {
  102. throw new NotFoundException();
  103. }
  104. /** @var Node $file */
  105. $file = array_shift($files);
  106. $reference->setTitle($file->getName());
  107. $reference->setDescription($file->getMimetype());
  108. $reference->setUrl($this->urlGenerator->getAbsoluteURL('/index.php/f/' . $fileId));
  109. $reference->setImageUrl($this->urlGenerator->linkToRouteAbsolute('core.Preview.getPreviewByFileId', ['x' => 1600, 'y' => 630, 'fileId' => $fileId]));
  110. $reference->setRichObject('file', [
  111. 'id' => $file->getId(),
  112. 'name' => $file->getName(),
  113. 'size' => $file->getSize(),
  114. 'path' => $file->getPath(),
  115. 'link' => $reference->getUrl(),
  116. 'mimetype' => $file->getMimetype(),
  117. 'preview-available' => $this->previewManager->isAvailable($file)
  118. ]);
  119. } catch (InvalidPathException|NotFoundException|NotPermittedException|NoUserException $e) {
  120. throw new NotFoundException();
  121. }
  122. }
  123. public function getCachePrefix(string $referenceId): string {
  124. return (string)$this->getFilesAppLinkId($referenceId);
  125. }
  126. public function getCacheKey(string $referenceId): ?string {
  127. return $this->userId ?? '';
  128. }
  129. }