FileReferenceProvider.php 4.8 KB

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