|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Files_FullTextSearch - Index the content of your files |
|
4
|
|
|
* |
|
5
|
|
|
* This file is licensed under the Affero General Public License version 3 or |
|
6
|
|
|
* later. See the COPYING file. |
|
7
|
|
|
* |
|
8
|
|
|
* @author Maxence Lange <[email protected]> |
|
9
|
|
|
* @copyright 2018 |
|
10
|
|
|
* @license GNU AGPL version 3 or any later version |
|
11
|
|
|
* |
|
12
|
|
|
* This program is free software: you can redistribute it and/or modify |
|
13
|
|
|
* it under the terms of the GNU Affero General Public License as |
|
14
|
|
|
* published by the Free Software Foundation, either version 3 of the |
|
15
|
|
|
* License, or (at your option) any later version. |
|
16
|
|
|
* |
|
17
|
|
|
* This program is distributed in the hope that it will be useful, |
|
18
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
19
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
20
|
|
|
* GNU Affero General Public License for more details. |
|
21
|
|
|
* |
|
22
|
|
|
* You should have received a copy of the GNU Affero General Public License |
|
23
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
24
|
|
|
* |
|
25
|
|
|
*/ |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Created by PhpStorm. |
|
29
|
|
|
* User: maxence |
|
30
|
|
|
* Date: 12/13/17 |
|
31
|
|
|
* Time: 4:11 PM |
|
32
|
|
|
*/ |
|
33
|
|
|
|
|
34
|
|
|
namespace OCA\Files_FullTextSearch\Service; |
|
35
|
|
|
|
|
36
|
|
|
|
|
37
|
|
|
use OCA\Files_FullTextSearch\Exceptions\FileIsNotIndexableException; |
|
38
|
|
|
use OCA\Files_FullTextSearch\Exceptions\KnownFileSourceException; |
|
39
|
|
|
use OCA\Files_FullTextSearch\Model\MountPoint; |
|
40
|
|
|
use OCA\Files_FullTextSearch\Model\FilesDocument; |
|
41
|
|
|
use OCP\App; |
|
42
|
|
|
use OCP\Files\IRootFolder; |
|
43
|
|
|
use OCP\Files\Node; |
|
44
|
|
|
use OCP\Files\NotFoundException; |
|
45
|
|
|
use OCP\IUserManager; |
|
46
|
|
|
use OCP\Share\IManager; |
|
47
|
|
|
|
|
48
|
|
|
class ExternalFilesService { |
|
49
|
|
|
|
|
50
|
|
|
|
|
51
|
|
|
const DOCUMENT_SOURCE = 'external'; |
|
52
|
|
|
|
|
53
|
|
|
/** @var IRootFolder */ |
|
54
|
|
|
private $rootFolder; |
|
55
|
|
|
|
|
56
|
|
|
/** @var IUserManager */ |
|
57
|
|
|
private $userManager; |
|
58
|
|
|
|
|
59
|
|
|
/** @var IManager */ |
|
60
|
|
|
private $shareManager; |
|
61
|
|
|
|
|
62
|
|
|
/** @var LocalFilesService */ |
|
63
|
|
|
private $localFilesService; |
|
64
|
|
|
|
|
65
|
|
|
/** @var ConfigService */ |
|
66
|
|
|
private $configService; |
|
67
|
|
|
|
|
68
|
|
|
/** @var MiscService */ |
|
69
|
|
|
private $miscService; |
|
70
|
|
|
|
|
71
|
|
|
|
|
72
|
|
|
/** @var MountPoint[] */ |
|
73
|
|
|
private $externalMounts = []; |
|
74
|
|
|
|
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* ExternalFilesService constructor. |
|
78
|
|
|
* |
|
79
|
|
|
* @param IRootFolder $rootFolder |
|
80
|
|
|
* @param IUserManager $userManager |
|
81
|
|
|
* @param IManager $shareManager |
|
82
|
|
|
* @param LocalFilesService $localFilesService |
|
83
|
|
|
* @param ConfigService $configService |
|
84
|
|
|
* @param MiscService $miscService |
|
85
|
|
|
*/ |
|
86
|
|
View Code Duplication |
public function __construct( |
|
|
|
|
|
|
87
|
|
|
IRootFolder $rootFolder, IUserManager $userManager, IManager $shareManager, |
|
88
|
|
|
LocalFilesService $localFilesService, ConfigService $configService, MiscService $miscService |
|
89
|
|
|
) { |
|
90
|
|
|
$this->rootFolder = $rootFolder; |
|
91
|
|
|
$this->userManager = $userManager; |
|
92
|
|
|
$this->shareManager = $shareManager; |
|
93
|
|
|
|
|
94
|
|
|
$this->localFilesService = $localFilesService; |
|
95
|
|
|
|
|
96
|
|
|
$this->configService = $configService; |
|
97
|
|
|
$this->miscService = $miscService; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* @param string $userId |
|
103
|
|
|
*/ |
|
104
|
|
|
public function initExternalFilesForUser($userId) { |
|
105
|
|
|
$this->externalMounts = []; |
|
106
|
|
|
if (!App::isEnabled('files_external')) { |
|
107
|
|
|
return; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
if ($this->configService->getAppValue(ConfigService::FILES_EXTERNAL) !== '1') { |
|
111
|
|
|
return; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
$this->externalMounts = $this->getMountPoints($userId); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* @param Node $file |
|
120
|
|
|
* |
|
121
|
|
|
* @param string $source |
|
122
|
|
|
* |
|
123
|
|
|
* @throws FileIsNotIndexableException |
|
124
|
|
|
* @throws NotFoundException |
|
125
|
|
|
* @throws KnownFileSourceException |
|
126
|
|
|
*/ |
|
127
|
|
|
public function getFileSource(Node $file, &$source) { |
|
128
|
|
|
if ($file->getStorage() |
|
129
|
|
|
->isLocal() === true) { |
|
130
|
|
|
return; |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
if (!$this->configService->optionIsSelected(ConfigService::FILES_EXTERNAL)) { |
|
134
|
|
|
throw new FileIsNotIndexableException(); |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
$this->getMountPoint($file); |
|
138
|
|
|
$source = self::DOCUMENT_SOURCE; |
|
139
|
|
|
|
|
140
|
|
|
throw new KnownFileSourceException(); |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
|
|
144
|
|
|
/** |
|
145
|
|
|
* @param FilesDocument $document |
|
146
|
|
|
* @param array $users |
|
147
|
|
|
*/ |
|
148
|
|
|
public function getShareUsers(FilesDocument $document, &$users) { |
|
149
|
|
|
|
|
150
|
|
|
if ($document->getSource() !== self::DOCUMENT_SOURCE) { |
|
151
|
|
|
return; |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
$this->localFilesService->getSharedUsersFromAccess($document->getAccess(), $users); |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
|
|
158
|
|
|
/** |
|
159
|
|
|
* @param FilesDocument $document |
|
160
|
|
|
* @param Node $file |
|
161
|
|
|
*/ |
|
162
|
|
|
public function updateDocumentAccess(FilesDocument &$document, Node $file) { |
|
163
|
|
|
|
|
164
|
|
|
if ($document->getSource() !== self::DOCUMENT_SOURCE) { |
|
165
|
|
|
return; |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
try { |
|
169
|
|
|
$mount = $this->getMountPoint($file); |
|
170
|
|
|
} catch (FileIsNotIndexableException $e) { |
|
171
|
|
|
return; |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
|
|
$access = $document->getAccess(); |
|
175
|
|
|
|
|
176
|
|
|
if ($this->isMountFullGlobal($mount)) { |
|
177
|
|
|
$access->addUsers(['__all']); |
|
178
|
|
|
} else { |
|
179
|
|
|
$access->addUsers($mount->getUsers()); |
|
180
|
|
|
$access->addGroups($mount->getGroups()); |
|
181
|
|
|
// $access->addCircles($mount->getCircles()); |
|
|
|
|
|
|
182
|
|
|
} |
|
183
|
|
|
|
|
184
|
|
|
// twist 'n tweak. |
|
185
|
|
|
if (!$mount->isGlobal()) { |
|
186
|
|
|
$access->setOwnerId($mount->getUsers()[0]); |
|
187
|
|
|
} |
|
188
|
|
|
|
|
189
|
|
|
$document->setAccess($access); |
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
|
|
|
|
193
|
|
|
/** |
|
194
|
|
|
* @param MountPoint $mount |
|
195
|
|
|
* |
|
196
|
|
|
* @return bool |
|
197
|
|
|
*/ |
|
198
|
|
|
public function isMountFullGlobal(MountPoint $mount) { |
|
199
|
|
|
if (sizeof($mount->getGroups()) > 0) { |
|
200
|
|
|
return false; |
|
201
|
|
|
} |
|
202
|
|
|
|
|
203
|
|
|
if (sizeof($mount->getUsers()) !== 1) { |
|
204
|
|
|
return false; |
|
205
|
|
|
} |
|
206
|
|
|
|
|
207
|
|
|
if ($mount->getUsers()[0] === 'all') { |
|
|
|
|
|
|
208
|
|
|
return true; |
|
209
|
|
|
} |
|
210
|
|
|
|
|
211
|
|
|
return false; |
|
212
|
|
|
} |
|
213
|
|
|
|
|
214
|
|
|
|
|
215
|
|
|
/** |
|
216
|
|
|
* @param Node $file |
|
217
|
|
|
* |
|
218
|
|
|
* @return MountPoint |
|
219
|
|
|
* @throws FileIsNotIndexableException |
|
220
|
|
|
*/ |
|
221
|
|
|
private function getMountPoint(Node $file) { |
|
222
|
|
|
|
|
223
|
|
|
foreach ($this->externalMounts as $mount) { |
|
224
|
|
|
if (strpos($file->getPath(), $mount->getPath()) === 0) { |
|
225
|
|
|
return $mount; |
|
226
|
|
|
} |
|
227
|
|
|
} |
|
228
|
|
|
|
|
229
|
|
|
throw new FileIsNotIndexableException(); |
|
230
|
|
|
} |
|
231
|
|
|
|
|
232
|
|
|
|
|
233
|
|
|
/** |
|
234
|
|
|
* @param $userId |
|
235
|
|
|
* |
|
236
|
|
|
* @return MountPoint[] |
|
237
|
|
|
*/ |
|
238
|
|
|
private function getMountPoints($userId) { |
|
239
|
|
|
|
|
240
|
|
|
$mountPoints = []; |
|
241
|
|
|
|
|
242
|
|
|
// TODO: deprecated - use UserGlobalStoragesService::getStorages() and UserStoragesService::getStorages() |
|
|
|
|
|
|
243
|
|
|
$mounts = \OC_Mount_Config::getAbsoluteMountPoints($userId); |
|
244
|
|
|
foreach ($mounts as $path => $mount) { |
|
245
|
|
|
$mountPoint = new MountPoint(); |
|
246
|
|
|
$mountPoint->setId($mount['id']) |
|
247
|
|
|
->setPath('/' . $userId . '/files/' . $path) |
|
248
|
|
|
->setGroups($mount['applicable']['groups']) |
|
249
|
|
|
->setUsers($mount['applicable']['users']) |
|
250
|
|
|
->setGlobal((!$mount['personal'])); |
|
251
|
|
|
$mountPoints[] = $mountPoint; |
|
252
|
|
|
} |
|
253
|
|
|
|
|
254
|
|
|
return $mountPoints; |
|
255
|
|
|
} |
|
256
|
|
|
|
|
257
|
|
|
} |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.