@@ -39,127 +39,127 @@ |
||
| 39 | 39 | use OCP\IUserSession; |
| 40 | 40 | |
| 41 | 41 | class FileReferenceProvider implements IReferenceProvider { |
| 42 | - private IURLGenerator $urlGenerator; |
|
| 43 | - private IRootFolder $rootFolder; |
|
| 44 | - private ?string $userId; |
|
| 45 | - private IPreview $previewManager; |
|
| 46 | - private IMimeTypeDetector $mimeTypeDetector; |
|
| 47 | - |
|
| 48 | - public function __construct(IURLGenerator $urlGenerator, |
|
| 49 | - IRootFolder $rootFolder, |
|
| 50 | - IUserSession $userSession, |
|
| 51 | - IMimeTypeDetector $mimeTypeDetector, |
|
| 52 | - IPreview $previewManager) { |
|
| 53 | - $this->urlGenerator = $urlGenerator; |
|
| 54 | - $this->rootFolder = $rootFolder; |
|
| 55 | - $this->userId = $userSession->getUser() ? $userSession->getUser()->getUID() : null; |
|
| 56 | - $this->previewManager = $previewManager; |
|
| 57 | - $this->mimeTypeDetector = $mimeTypeDetector; |
|
| 58 | - } |
|
| 59 | - |
|
| 60 | - public function matchReference(string $referenceText): bool { |
|
| 61 | - return $this->getFilesAppLinkId($referenceText) !== null; |
|
| 62 | - } |
|
| 63 | - |
|
| 64 | - private function getFilesAppLinkId(string $referenceText): ?int { |
|
| 65 | - $start = $this->urlGenerator->getAbsoluteURL('/apps/files'); |
|
| 66 | - $startIndex = $this->urlGenerator->getAbsoluteURL('/index.php/apps/files'); |
|
| 67 | - |
|
| 68 | - $fileId = null; |
|
| 69 | - |
|
| 70 | - if (mb_strpos($referenceText, $start) === 0) { |
|
| 71 | - $parts = parse_url($referenceText); |
|
| 72 | - parse_str($parts['query'], $query); |
|
| 73 | - $fileId = isset($query['fileid']) ? (int)$query['fileid'] : $fileId; |
|
| 74 | - $fileId = isset($query['openfile']) ? (int)$query['openfile'] : $fileId; |
|
| 75 | - } |
|
| 76 | - |
|
| 77 | - if (mb_strpos($referenceText, $startIndex) === 0) { |
|
| 78 | - $parts = parse_url($referenceText); |
|
| 79 | - parse_str($parts['query'], $query); |
|
| 80 | - $fileId = isset($query['fileid']) ? (int)$query['fileid'] : $fileId; |
|
| 81 | - $fileId = isset($query['openfile']) ? (int)$query['openfile'] : $fileId; |
|
| 82 | - } |
|
| 83 | - |
|
| 84 | - if (mb_strpos($referenceText, $this->urlGenerator->getAbsoluteURL('/index.php/f/')) === 0) { |
|
| 85 | - $fileId = str_replace($this->urlGenerator->getAbsoluteURL('/index.php/f/'), '', $referenceText); |
|
| 86 | - } |
|
| 87 | - |
|
| 88 | - if (mb_strpos($referenceText, $this->urlGenerator->getAbsoluteURL('/f/')) === 0) { |
|
| 89 | - $fileId = str_replace($this->urlGenerator->getAbsoluteURL('/f/'), '', $referenceText); |
|
| 90 | - } |
|
| 91 | - |
|
| 92 | - return $fileId !== null ? (int)$fileId : null; |
|
| 93 | - } |
|
| 94 | - |
|
| 95 | - public function resolveReference(string $referenceText): ?IReference { |
|
| 96 | - if ($this->matchReference($referenceText)) { |
|
| 97 | - $reference = new Reference($referenceText); |
|
| 98 | - try { |
|
| 99 | - $this->fetchReference($reference); |
|
| 100 | - } catch (NotFoundException $e) { |
|
| 101 | - $reference->setRichObject('file', null); |
|
| 102 | - $reference->setAccessible(false); |
|
| 103 | - } |
|
| 104 | - return $reference; |
|
| 105 | - } |
|
| 106 | - |
|
| 107 | - return null; |
|
| 108 | - } |
|
| 109 | - |
|
| 110 | - /** |
|
| 111 | - * @throws NotFoundException |
|
| 112 | - */ |
|
| 113 | - private function fetchReference(Reference $reference): void { |
|
| 114 | - if ($this->userId === null) { |
|
| 115 | - throw new NotFoundException(); |
|
| 116 | - } |
|
| 117 | - |
|
| 118 | - $fileId = $this->getFilesAppLinkId($reference->getId()); |
|
| 119 | - if ($fileId === null) { |
|
| 120 | - throw new NotFoundException(); |
|
| 121 | - } |
|
| 122 | - |
|
| 123 | - try { |
|
| 124 | - $userFolder = $this->rootFolder->getUserFolder($this->userId); |
|
| 125 | - $files = $userFolder->getById($fileId); |
|
| 126 | - |
|
| 127 | - if (empty($files)) { |
|
| 128 | - throw new NotFoundException(); |
|
| 129 | - } |
|
| 130 | - |
|
| 131 | - /** @var Node $file */ |
|
| 132 | - $file = array_shift($files); |
|
| 133 | - |
|
| 134 | - $reference->setTitle($file->getName()); |
|
| 135 | - $reference->setDescription($file->getMimetype()); |
|
| 136 | - $reference->setUrl($this->urlGenerator->getAbsoluteURL('/index.php/f/' . $fileId)); |
|
| 137 | - if ($this->previewManager->isMimeSupported($file->getMimeType())) { |
|
| 138 | - $reference->setImageUrl($this->urlGenerator->linkToRouteAbsolute('core.Preview.getPreviewByFileId', ['x' => 1600, 'y' => 630, 'fileId' => $fileId])); |
|
| 139 | - } else { |
|
| 140 | - $fileTypeIconUrl = $this->mimeTypeDetector->mimeTypeIcon($file->getMimeType()); |
|
| 141 | - $reference->setImageUrl($fileTypeIconUrl); |
|
| 142 | - } |
|
| 143 | - |
|
| 144 | - $reference->setRichObject('file', [ |
|
| 145 | - 'id' => $file->getId(), |
|
| 146 | - 'name' => $file->getName(), |
|
| 147 | - 'size' => $file->getSize(), |
|
| 148 | - 'path' => $file->getPath(), |
|
| 149 | - 'link' => $reference->getUrl(), |
|
| 150 | - 'mimetype' => $file->getMimetype(), |
|
| 151 | - 'preview-available' => $this->previewManager->isAvailable($file) |
|
| 152 | - ]); |
|
| 153 | - } catch (InvalidPathException|NotFoundException|NotPermittedException|NoUserException $e) { |
|
| 154 | - throw new NotFoundException(); |
|
| 155 | - } |
|
| 156 | - } |
|
| 157 | - |
|
| 158 | - public function getCachePrefix(string $referenceId): string { |
|
| 159 | - return (string)$this->getFilesAppLinkId($referenceId); |
|
| 160 | - } |
|
| 161 | - |
|
| 162 | - public function getCacheKey(string $referenceId): ?string { |
|
| 163 | - return $this->userId ?? ''; |
|
| 164 | - } |
|
| 42 | + private IURLGenerator $urlGenerator; |
|
| 43 | + private IRootFolder $rootFolder; |
|
| 44 | + private ?string $userId; |
|
| 45 | + private IPreview $previewManager; |
|
| 46 | + private IMimeTypeDetector $mimeTypeDetector; |
|
| 47 | + |
|
| 48 | + public function __construct(IURLGenerator $urlGenerator, |
|
| 49 | + IRootFolder $rootFolder, |
|
| 50 | + IUserSession $userSession, |
|
| 51 | + IMimeTypeDetector $mimeTypeDetector, |
|
| 52 | + IPreview $previewManager) { |
|
| 53 | + $this->urlGenerator = $urlGenerator; |
|
| 54 | + $this->rootFolder = $rootFolder; |
|
| 55 | + $this->userId = $userSession->getUser() ? $userSession->getUser()->getUID() : null; |
|
| 56 | + $this->previewManager = $previewManager; |
|
| 57 | + $this->mimeTypeDetector = $mimeTypeDetector; |
|
| 58 | + } |
|
| 59 | + |
|
| 60 | + public function matchReference(string $referenceText): bool { |
|
| 61 | + return $this->getFilesAppLinkId($referenceText) !== null; |
|
| 62 | + } |
|
| 63 | + |
|
| 64 | + private function getFilesAppLinkId(string $referenceText): ?int { |
|
| 65 | + $start = $this->urlGenerator->getAbsoluteURL('/apps/files'); |
|
| 66 | + $startIndex = $this->urlGenerator->getAbsoluteURL('/index.php/apps/files'); |
|
| 67 | + |
|
| 68 | + $fileId = null; |
|
| 69 | + |
|
| 70 | + if (mb_strpos($referenceText, $start) === 0) { |
|
| 71 | + $parts = parse_url($referenceText); |
|
| 72 | + parse_str($parts['query'], $query); |
|
| 73 | + $fileId = isset($query['fileid']) ? (int)$query['fileid'] : $fileId; |
|
| 74 | + $fileId = isset($query['openfile']) ? (int)$query['openfile'] : $fileId; |
|
| 75 | + } |
|
| 76 | + |
|
| 77 | + if (mb_strpos($referenceText, $startIndex) === 0) { |
|
| 78 | + $parts = parse_url($referenceText); |
|
| 79 | + parse_str($parts['query'], $query); |
|
| 80 | + $fileId = isset($query['fileid']) ? (int)$query['fileid'] : $fileId; |
|
| 81 | + $fileId = isset($query['openfile']) ? (int)$query['openfile'] : $fileId; |
|
| 82 | + } |
|
| 83 | + |
|
| 84 | + if (mb_strpos($referenceText, $this->urlGenerator->getAbsoluteURL('/index.php/f/')) === 0) { |
|
| 85 | + $fileId = str_replace($this->urlGenerator->getAbsoluteURL('/index.php/f/'), '', $referenceText); |
|
| 86 | + } |
|
| 87 | + |
|
| 88 | + if (mb_strpos($referenceText, $this->urlGenerator->getAbsoluteURL('/f/')) === 0) { |
|
| 89 | + $fileId = str_replace($this->urlGenerator->getAbsoluteURL('/f/'), '', $referenceText); |
|
| 90 | + } |
|
| 91 | + |
|
| 92 | + return $fileId !== null ? (int)$fileId : null; |
|
| 93 | + } |
|
| 94 | + |
|
| 95 | + public function resolveReference(string $referenceText): ?IReference { |
|
| 96 | + if ($this->matchReference($referenceText)) { |
|
| 97 | + $reference = new Reference($referenceText); |
|
| 98 | + try { |
|
| 99 | + $this->fetchReference($reference); |
|
| 100 | + } catch (NotFoundException $e) { |
|
| 101 | + $reference->setRichObject('file', null); |
|
| 102 | + $reference->setAccessible(false); |
|
| 103 | + } |
|
| 104 | + return $reference; |
|
| 105 | + } |
|
| 106 | + |
|
| 107 | + return null; |
|
| 108 | + } |
|
| 109 | + |
|
| 110 | + /** |
|
| 111 | + * @throws NotFoundException |
|
| 112 | + */ |
|
| 113 | + private function fetchReference(Reference $reference): void { |
|
| 114 | + if ($this->userId === null) { |
|
| 115 | + throw new NotFoundException(); |
|
| 116 | + } |
|
| 117 | + |
|
| 118 | + $fileId = $this->getFilesAppLinkId($reference->getId()); |
|
| 119 | + if ($fileId === null) { |
|
| 120 | + throw new NotFoundException(); |
|
| 121 | + } |
|
| 122 | + |
|
| 123 | + try { |
|
| 124 | + $userFolder = $this->rootFolder->getUserFolder($this->userId); |
|
| 125 | + $files = $userFolder->getById($fileId); |
|
| 126 | + |
|
| 127 | + if (empty($files)) { |
|
| 128 | + throw new NotFoundException(); |
|
| 129 | + } |
|
| 130 | + |
|
| 131 | + /** @var Node $file */ |
|
| 132 | + $file = array_shift($files); |
|
| 133 | + |
|
| 134 | + $reference->setTitle($file->getName()); |
|
| 135 | + $reference->setDescription($file->getMimetype()); |
|
| 136 | + $reference->setUrl($this->urlGenerator->getAbsoluteURL('/index.php/f/' . $fileId)); |
|
| 137 | + if ($this->previewManager->isMimeSupported($file->getMimeType())) { |
|
| 138 | + $reference->setImageUrl($this->urlGenerator->linkToRouteAbsolute('core.Preview.getPreviewByFileId', ['x' => 1600, 'y' => 630, 'fileId' => $fileId])); |
|
| 139 | + } else { |
|
| 140 | + $fileTypeIconUrl = $this->mimeTypeDetector->mimeTypeIcon($file->getMimeType()); |
|
| 141 | + $reference->setImageUrl($fileTypeIconUrl); |
|
| 142 | + } |
|
| 143 | + |
|
| 144 | + $reference->setRichObject('file', [ |
|
| 145 | + 'id' => $file->getId(), |
|
| 146 | + 'name' => $file->getName(), |
|
| 147 | + 'size' => $file->getSize(), |
|
| 148 | + 'path' => $file->getPath(), |
|
| 149 | + 'link' => $reference->getUrl(), |
|
| 150 | + 'mimetype' => $file->getMimetype(), |
|
| 151 | + 'preview-available' => $this->previewManager->isAvailable($file) |
|
| 152 | + ]); |
|
| 153 | + } catch (InvalidPathException|NotFoundException|NotPermittedException|NoUserException $e) { |
|
| 154 | + throw new NotFoundException(); |
|
| 155 | + } |
|
| 156 | + } |
|
| 157 | + |
|
| 158 | + public function getCachePrefix(string $referenceId): string { |
|
| 159 | + return (string)$this->getFilesAppLinkId($referenceId); |
|
| 160 | + } |
|
| 161 | + |
|
| 162 | + public function getCacheKey(string $referenceId): ?string { |
|
| 163 | + return $this->userId ?? ''; |
|
| 164 | + } |
|
| 165 | 165 | } |
@@ -70,15 +70,15 @@ discard block |
||
| 70 | 70 | if (mb_strpos($referenceText, $start) === 0) { |
| 71 | 71 | $parts = parse_url($referenceText); |
| 72 | 72 | parse_str($parts['query'], $query); |
| 73 | - $fileId = isset($query['fileid']) ? (int)$query['fileid'] : $fileId; |
|
| 74 | - $fileId = isset($query['openfile']) ? (int)$query['openfile'] : $fileId; |
|
| 73 | + $fileId = isset($query['fileid']) ? (int) $query['fileid'] : $fileId; |
|
| 74 | + $fileId = isset($query['openfile']) ? (int) $query['openfile'] : $fileId; |
|
| 75 | 75 | } |
| 76 | 76 | |
| 77 | 77 | if (mb_strpos($referenceText, $startIndex) === 0) { |
| 78 | 78 | $parts = parse_url($referenceText); |
| 79 | 79 | parse_str($parts['query'], $query); |
| 80 | - $fileId = isset($query['fileid']) ? (int)$query['fileid'] : $fileId; |
|
| 81 | - $fileId = isset($query['openfile']) ? (int)$query['openfile'] : $fileId; |
|
| 80 | + $fileId = isset($query['fileid']) ? (int) $query['fileid'] : $fileId; |
|
| 81 | + $fileId = isset($query['openfile']) ? (int) $query['openfile'] : $fileId; |
|
| 82 | 82 | } |
| 83 | 83 | |
| 84 | 84 | if (mb_strpos($referenceText, $this->urlGenerator->getAbsoluteURL('/index.php/f/')) === 0) { |
@@ -89,7 +89,7 @@ discard block |
||
| 89 | 89 | $fileId = str_replace($this->urlGenerator->getAbsoluteURL('/f/'), '', $referenceText); |
| 90 | 90 | } |
| 91 | 91 | |
| 92 | - return $fileId !== null ? (int)$fileId : null; |
|
| 92 | + return $fileId !== null ? (int) $fileId : null; |
|
| 93 | 93 | } |
| 94 | 94 | |
| 95 | 95 | public function resolveReference(string $referenceText): ?IReference { |
@@ -133,7 +133,7 @@ discard block |
||
| 133 | 133 | |
| 134 | 134 | $reference->setTitle($file->getName()); |
| 135 | 135 | $reference->setDescription($file->getMimetype()); |
| 136 | - $reference->setUrl($this->urlGenerator->getAbsoluteURL('/index.php/f/' . $fileId)); |
|
| 136 | + $reference->setUrl($this->urlGenerator->getAbsoluteURL('/index.php/f/'.$fileId)); |
|
| 137 | 137 | if ($this->previewManager->isMimeSupported($file->getMimeType())) { |
| 138 | 138 | $reference->setImageUrl($this->urlGenerator->linkToRouteAbsolute('core.Preview.getPreviewByFileId', ['x' => 1600, 'y' => 630, 'fileId' => $fileId])); |
| 139 | 139 | } else { |
@@ -150,13 +150,13 @@ discard block |
||
| 150 | 150 | 'mimetype' => $file->getMimetype(), |
| 151 | 151 | 'preview-available' => $this->previewManager->isAvailable($file) |
| 152 | 152 | ]); |
| 153 | - } catch (InvalidPathException|NotFoundException|NotPermittedException|NoUserException $e) { |
|
| 153 | + } catch (InvalidPathException | NotFoundException | NotPermittedException | NoUserException $e) { |
|
| 154 | 154 | throw new NotFoundException(); |
| 155 | 155 | } |
| 156 | 156 | } |
| 157 | 157 | |
| 158 | 158 | public function getCachePrefix(string $referenceId): string { |
| 159 | - return (string)$this->getFilesAppLinkId($referenceId); |
|
| 159 | + return (string) $this->getFilesAppLinkId($referenceId); |
|
| 160 | 160 | } |
| 161 | 161 | |
| 162 | 162 | public function getCacheKey(string $referenceId): ?string { |