FileReferenceProvider.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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\User\NoUserException;
  25. use OCP\Collaboration\Reference\ADiscoverableReferenceProvider;
  26. use OCP\Collaboration\Reference\IReference;
  27. use OCP\Collaboration\Reference\Reference;
  28. use OCP\Files\IMimeTypeDetector;
  29. use OCP\Files\InvalidPathException;
  30. use OCP\Files\IRootFolder;
  31. use OCP\Files\NotFoundException;
  32. use OCP\Files\NotPermittedException;
  33. use OCP\IL10N;
  34. use OCP\IPreview;
  35. use OCP\IURLGenerator;
  36. use OCP\IUserSession;
  37. use OCP\L10N\IFactory;
  38. class FileReferenceProvider extends ADiscoverableReferenceProvider {
  39. private ?string $userId;
  40. private IL10N $l10n;
  41. public function __construct(
  42. private IURLGenerator $urlGenerator,
  43. private IRootFolder $rootFolder,
  44. IUserSession $userSession,
  45. private IMimeTypeDetector $mimeTypeDetector,
  46. private IPreview $previewManager,
  47. IFactory $l10n,
  48. ) {
  49. $this->userId = $userSession->getUser()?->getUID();
  50. $this->l10n = $l10n->get('files');
  51. }
  52. public function matchReference(string $referenceText): bool {
  53. return $this->getFilesAppLinkId($referenceText) !== null;
  54. }
  55. private function getFilesAppLinkId(string $referenceText): ?int {
  56. $start = $this->urlGenerator->getAbsoluteURL('/apps/files/');
  57. $startIndex = $this->urlGenerator->getAbsoluteURL('/index.php/apps/files/');
  58. $fileId = null;
  59. if (mb_strpos($referenceText, $start) === 0) {
  60. $parts = parse_url($referenceText);
  61. parse_str($parts['query'] ?? '', $query);
  62. $fileId = isset($query['fileid']) ? (int)$query['fileid'] : $fileId;
  63. $fileId = isset($query['openfile']) ? (int)$query['openfile'] : $fileId;
  64. }
  65. if (mb_strpos($referenceText, $startIndex) === 0) {
  66. $parts = parse_url($referenceText);
  67. parse_str($parts['query'] ?? '', $query);
  68. $fileId = isset($query['fileid']) ? (int)$query['fileid'] : $fileId;
  69. $fileId = isset($query['openfile']) ? (int)$query['openfile'] : $fileId;
  70. }
  71. if (mb_strpos($referenceText, $this->urlGenerator->getAbsoluteURL('/index.php/f/')) === 0) {
  72. $fileId = str_replace($this->urlGenerator->getAbsoluteURL('/index.php/f/'), '', $referenceText);
  73. }
  74. if (mb_strpos($referenceText, $this->urlGenerator->getAbsoluteURL('/f/')) === 0) {
  75. $fileId = str_replace($this->urlGenerator->getAbsoluteURL('/f/'), '', $referenceText);
  76. }
  77. return $fileId !== null ? (int)$fileId : null;
  78. }
  79. public function resolveReference(string $referenceText): ?IReference {
  80. if ($this->matchReference($referenceText)) {
  81. $reference = new Reference($referenceText);
  82. try {
  83. $this->fetchReference($reference);
  84. } catch (NotFoundException $e) {
  85. $reference->setRichObject('file', null);
  86. $reference->setAccessible(false);
  87. }
  88. return $reference;
  89. }
  90. return null;
  91. }
  92. /**
  93. * @throws NotFoundException
  94. */
  95. private function fetchReference(Reference $reference): void {
  96. if ($this->userId === null) {
  97. throw new NotFoundException();
  98. }
  99. $fileId = $this->getFilesAppLinkId($reference->getId());
  100. if ($fileId === null) {
  101. throw new NotFoundException();
  102. }
  103. try {
  104. $userFolder = $this->rootFolder->getUserFolder($this->userId);
  105. $file = $userFolder->getFirstNodeById($fileId);
  106. if (!$file) {
  107. throw new NotFoundException();
  108. }
  109. $reference->setTitle($file->getName());
  110. $reference->setDescription($file->getMimetype());
  111. $reference->setUrl($this->urlGenerator->getAbsoluteURL('/index.php/f/' . $fileId));
  112. if ($this->previewManager->isMimeSupported($file->getMimeType())) {
  113. $reference->setImageUrl($this->urlGenerator->linkToRouteAbsolute('core.Preview.getPreviewByFileId', ['x' => 1600, 'y' => 630, 'fileId' => $fileId]));
  114. } else {
  115. $fileTypeIconUrl = $this->mimeTypeDetector->mimeTypeIcon($file->getMimeType());
  116. $reference->setImageUrl($fileTypeIconUrl);
  117. }
  118. $reference->setRichObject('file', [
  119. 'id' => $file->getId(),
  120. 'name' => $file->getName(),
  121. 'size' => $file->getSize(),
  122. 'path' => $userFolder->getRelativePath($file->getPath()),
  123. 'link' => $reference->getUrl(),
  124. 'mimetype' => $file->getMimetype(),
  125. 'mtime' => $file->getMTime(),
  126. 'preview-available' => $this->previewManager->isAvailable($file)
  127. ]);
  128. } catch (InvalidPathException|NotFoundException|NotPermittedException|NoUserException $e) {
  129. throw new NotFoundException();
  130. }
  131. }
  132. public function getCachePrefix(string $referenceId): string {
  133. return (string)$this->getFilesAppLinkId($referenceId);
  134. }
  135. public function getCacheKey(string $referenceId): ?string {
  136. return $this->userId ?? '';
  137. }
  138. public function getId(): string {
  139. return 'files';
  140. }
  141. public function getTitle(): string {
  142. return $this->l10n->t('Files');
  143. }
  144. public function getOrder(): int {
  145. return 0;
  146. }
  147. public function getIconUrl(): string {
  148. return $this->urlGenerator->imagePath('files', 'folder.svg');
  149. }
  150. }