Passed
Push — master ( 4209dc...b3eb0b )
by Julius
16:39 queued 12s
created

FileReferenceProvider::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
dl 0
loc 5
rs 10
c 0
b 0
f 0
eloc 4
nc 2
nop 4
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\Collaboration\Reference\Reference;
28
use OC\User\NoUserException;
29
use OCP\Collaboration\Reference\IReference;
30
use OCP\Collaboration\Reference\IReferenceProvider;
31
use OCP\Files\InvalidPathException;
32
use OCP\Files\IRootFolder;
33
use OCP\Files\Node;
34
use OCP\Files\NotFoundException;
35
use OCP\Files\NotPermittedException;
36
use OCP\IPreview;
37
use OCP\IURLGenerator;
38
use OCP\IUserSession;
39
40
class FileReferenceProvider implements IReferenceProvider {
41
	private IURLGenerator $urlGenerator;
42
	private IRootFolder $rootFolder;
43
	private ?string $userId;
44
	private IPreview $previewManager;
45
46
	public function __construct(IURLGenerator $urlGenerator, IRootFolder $rootFolder, IUserSession $userSession, IPreview $previewManager) {
47
		$this->urlGenerator = $urlGenerator;
48
		$this->rootFolder = $rootFolder;
49
		$this->userId = $userSession->getUser() ? $userSession->getUser()->getUID() : null;
50
		$this->previewManager = $previewManager;
51
	}
52
53
	public function matchReference(string $referenceText): bool {
54
		return $this->getFilesAppLinkId($referenceText) !== null;
55
	}
56
57
	private function getFilesAppLinkId(string $referenceText): ?int {
58
		$start = $this->urlGenerator->getAbsoluteURL('/apps/files');
59
		$startIndex = $this->urlGenerator->getAbsoluteURL('/index.php/apps/files');
60
61
		$fileId = null;
62
63
		if (mb_strpos($referenceText, $start) === 0) {
64
			$parts = parse_url($referenceText);
65
			parse_str($parts['query'], $query);
66
			$fileId = isset($query['fileid']) ? (int)$query['fileid'] : $fileId;
67
			$fileId = isset($query['openfile']) ? (int)$query['openfile'] : $fileId;
68
		}
69
70
		if (mb_strpos($referenceText, $startIndex) === 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, $this->urlGenerator->getAbsoluteURL('/index.php/f/')) === 0) {
78
			$fileId = str_replace($this->urlGenerator->getAbsoluteURL('/index.php/f/'), '', $referenceText);
79
		}
80
81
		if (mb_strpos($referenceText, $this->urlGenerator->getAbsoluteURL('/f/')) === 0) {
82
			$fileId = str_replace($this->urlGenerator->getAbsoluteURL('/f/'), '', $referenceText);
83
		}
84
85
		return $fileId !== null ? (int)$fileId : null;
86
	}
87
88
	public function resolveReference(string $referenceText): ?IReference {
89
		if ($this->matchReference($referenceText)) {
90
			$reference = new Reference($referenceText);
91
			try {
92
				$this->fetchReference($reference);
93
			} catch (NotFoundException $e) {
94
				$reference->setRichObject('file', null);
95
				$reference->setAccessible(false);
96
			}
97
			return $reference;
98
		}
99
100
		return null;
101
	}
102
103
	/**
104
	 * @throws NotFoundException
105
	 */
106
	private function fetchReference(Reference $reference): void {
107
		if ($this->userId === null) {
108
			throw new NotFoundException();
109
		}
110
111
		$fileId = $this->getFilesAppLinkId($reference->getId());
112
		if ($fileId === null) {
113
			throw new NotFoundException();
114
		}
115
116
		try {
117
			$userFolder = $this->rootFolder->getUserFolder($this->userId);
118
			$files = $userFolder->getById($fileId);
119
120
			if (empty($files)) {
121
				throw new NotFoundException();
122
			}
123
124
			/** @var Node $file */
125
			$file = array_shift($files);
126
127
			$reference->setTitle($file->getName());
128
			$reference->setDescription($file->getMimetype());
129
			$reference->setUrl($this->urlGenerator->getAbsoluteURL('/index.php/f/' . $fileId));
130
			$reference->setImageUrl($this->urlGenerator->linkToRouteAbsolute('core.Preview.getPreviewByFileId', ['x' => 1600, 'y' => 630, 'fileId' => $fileId]));
131
132
			$reference->setRichObject('file', [
133
				'id' => $file->getId(),
134
				'name' => $file->getName(),
135
				'size' => $file->getSize(),
136
				'path' => $file->getPath(),
137
				'link' => $reference->getUrl(),
138
				'mimetype' => $file->getMimetype(),
139
				'preview-available' => $this->previewManager->isAvailable($file)
140
			]);
141
		} catch (InvalidPathException|NotFoundException|NotPermittedException|NoUserException $e) {
142
			throw new NotFoundException();
143
		}
144
	}
145
146
	public function getCachePrefix(string $referenceId): string {
147
		return (string)$this->getFilesAppLinkId($referenceId);
148
	}
149
150
	public function getCacheKey(string $referenceId): ?string {
151
		return $this->userId ?? '';
152
	}
153
}
154