Passed
Push — master ( 140194...6ef03f )
by Joas
13:32 queued 13s
created

FileReferenceProvider::fetchReference()   B

Complexity

Conditions 6
Paths 17

Size

Total Lines 42
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 6
eloc 29
nc 17
nop 1
dl 0
loc 42
rs 8.8337
c 1
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
/**
5
 * @copyright Copyright (c) 2022 Julius Härtl <[email protected]>
6
 *
7
 * @author Julius Härtl <[email protected]>
8
 *
9
 * @license GNU AGPL version 3 or any later version
10
 *
11
 * This program is free software: you can redistribute it and/or modify
12
 * it under the terms of the GNU Affero General Public License as
13
 * published by the Free Software Foundation, either version 3 of the
14
 * License, or (at your option) any later version.
15
 *
16
 * This program is distributed in the hope that it will be useful,
17
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19
 * GNU Affero General Public License for more details.
20
 *
21
 * You should have received a copy of the GNU Affero General Public License
22
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
23
 */
24
25
namespace OC\Collaboration\Reference\File;
26
27
use OC\User\NoUserException;
28
use OCP\Collaboration\Reference\IReference;
29
use OCP\Collaboration\Reference\IReferenceProvider;
30
use OCP\Collaboration\Reference\Reference;
31
use OCP\Files\IMimeTypeDetector;
32
use OCP\Files\InvalidPathException;
33
use OCP\Files\IRootFolder;
34
use OCP\Files\Node;
35
use OCP\Files\NotFoundException;
36
use OCP\Files\NotPermittedException;
37
use OCP\IPreview;
38
use OCP\IURLGenerator;
39
use OCP\IUserSession;
40
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
	}
165
}
166