|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | +/** |
| 5 | + * SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors |
| 6 | + * SPDX-License-Identifier: AGPL-3.0-or-later |
| 7 | + */ |
| 8 | + |
| 9 | +namespace OCA\Files_Sharing\Reference; |
| 10 | + |
| 11 | +use OCP\Collaboration\Reference\ADiscoverableReferenceProvider; |
| 12 | +use OCP\Collaboration\Reference\IReference; |
| 13 | +use OCP\Collaboration\Reference\Reference; |
| 14 | +use OCP\Files\IMimeTypeDetector; |
| 15 | +use OCP\Files\InvalidPathException; |
| 16 | +use OCP\Files\NotFoundException; |
| 17 | +use OCP\Files\NotPermittedException; |
| 18 | +use OCP\IL10N; |
| 19 | +use OCP\IPreview; |
| 20 | +use OCP\IURLGenerator; |
| 21 | +use OCP\Share\Exceptions\ShareNotFound; |
| 22 | +use OCP\Share\IManager as ShareManager; |
| 23 | + |
| 24 | +class ShareReferenceProvider extends ADiscoverableReferenceProvider { |
| 25 | + |
| 26 | + public function __construct( |
| 27 | + private IL10N $l10n, |
| 28 | + private IURLGenerator $urlGenerator, |
| 29 | + private ShareManager $shareManager, |
| 30 | + private IMimeTypeDetector $mimeTypeDetector, |
| 31 | + private IPreview $previewManager, |
| 32 | + ) { |
| 33 | + } |
| 34 | + |
| 35 | + public function matchReference(string $referenceText): bool { |
| 36 | + return $this->getShareToken($referenceText) !== null; |
| 37 | + } |
| 38 | + |
| 39 | + public function resolveReference(string $referenceText): ?IReference { |
| 40 | + if ($this->matchReference($referenceText)) { |
| 41 | + $reference = new Reference($referenceText); |
| 42 | + try { |
| 43 | + $this->fetchReference($reference); |
| 44 | + } catch (NotFoundException $e) { |
| 45 | + $reference->setRichObject('file', null); |
| 46 | + $reference->setAccessible(false); |
| 47 | + } |
| 48 | + return $reference; |
| 49 | + } |
| 50 | + |
| 51 | + return null; |
| 52 | + } |
| 53 | + |
| 54 | + public function getCachePrefix(string $referenceId): string { |
| 55 | + $token = $this->getShareToken($referenceId); |
| 56 | + return $token !== null ? $token : ''; |
| 57 | + } |
| 58 | + |
| 59 | + public function getCacheKey(string $referenceId): ?string { |
| 60 | + // Cache key depends on the share token |
| 61 | + return $this->getCachePrefix($referenceId); |
| 62 | + } |
| 63 | + |
| 64 | + public function getId(): string { |
| 65 | + return 'files_sharing'; |
| 66 | + } |
| 67 | + |
| 68 | + public function getTitle(): string { |
| 69 | + return $this->l10n->t('Shared files'); |
| 70 | + } |
| 71 | + |
| 72 | + public function getOrder(): int { |
| 73 | + return 5; |
| 74 | + } |
| 75 | + |
| 76 | + public function getIconUrl(): string { |
| 77 | + return $this->urlGenerator->imagePath('files_sharing', 'app.svg'); |
| 78 | + } |
| 79 | + |
| 80 | + private function getShareToken(string $referenceText): ?string { |
| 81 | + $start = $this->urlGenerator->getAbsoluteURL('/s/'); |
| 82 | + $startIndex = $this->urlGenerator->getAbsoluteURL('/index.php/s/'); |
| 83 | + |
| 84 | + if (mb_strpos($referenceText, $start) === 0) { |
| 85 | + return $this->extractToken($referenceText, $start); |
| 86 | + } |
| 87 | + |
| 88 | + if (mb_strpos($referenceText, $startIndex) === 0) { |
| 89 | + return $this->extractToken($referenceText, $startIndex); |
| 90 | + } |
| 91 | + |
| 92 | + return null; |
| 93 | + } |
| 94 | + |
| 95 | + private function extractToken(string $referenceText, string $prefix): ?string { |
| 96 | + $token = mb_substr($referenceText, mb_strlen($prefix)); |
| 97 | + // Token might be followed by query parameters or path |
| 98 | + $parts = parse_url($referenceText); |
| 99 | + if (isset($parts['path'])) { |
| 100 | + $token = mb_substr($parts['path'], mb_strlen(parse_url($prefix, PHP_URL_PATH))); |
| 101 | + } |
| 102 | + // Remove any trailing slash or additional path components |
| 103 | + $token = strtok($token, '/'); |
| 104 | + return $token !== '' ? $token : null; |
| 105 | + } |
| 106 | + |
| 107 | + /** |
| 108 | + * @throws NotFoundException |
| 109 | + */ |
| 110 | + private function fetchReference(Reference $reference): void { |
| 111 | + $token = $this->getShareToken($reference->getId()); |
| 112 | + if ($token === null) { |
| 113 | + throw new NotFoundException(); |
| 114 | + } |
| 115 | + |
| 116 | + try { |
| 117 | + $share = $this->shareManager->getShareByToken($token); |
| 118 | + $node = $share->getNode(); |
| 119 | + |
| 120 | + $reference->setTitle($node->getName()); |
| 121 | + $reference->setDescription($node->getMimetype()); |
| 122 | + $reference->setUrl($this->urlGenerator->getAbsoluteURL('/s/' . $token)); |
| 123 | + |
| 124 | + if ($this->previewManager->isMimeSupported($node->getMimeType())) { |
| 125 | + $reference->setImageUrl($this->urlGenerator->linkToRouteAbsolute('core.Preview.getPreviewByFileId', [ |
| 126 | + 'x' => 1600, |
| 127 | + 'y' => 630, |
| 128 | + 'fileId' => $node->getId(), |
| 129 | + ])); |
| 130 | + } else { |
| 131 | + $fileTypeIconUrl = $this->mimeTypeDetector->mimeTypeIcon($node->getMimeType()); |
| 132 | + $reference->setImageUrl($fileTypeIconUrl); |
| 133 | + } |
| 134 | + |
| 135 | + $reference->setRichObject('file', [ |
| 136 | + 'id' => $node->getId(), |
| 137 | + 'name' => $node->getName(), |
| 138 | + 'size' => $node->getSize(), |
| 139 | + 'path' => $node->getPath(), |
| 140 | + 'link' => $reference->getUrl(), |
| 141 | + 'mimetype' => $node->getMimetype(), |
| 142 | + 'mtime' => $node->getMTime(), |
| 143 | + 'preview-available' => $this->previewManager->isAvailable($node), |
| 144 | + 'share-token' => $token, |
| 145 | + ]); |
| 146 | + } catch (ShareNotFound|InvalidPathException|NotFoundException|NotPermittedException $e) { |
| 147 | + throw new NotFoundException(previous: $e); |
| 148 | + } |
| 149 | + } |
| 150 | +} |
0 commit comments