-
Notifications
You must be signed in to change notification settings - Fork 58
Expand file tree
/
Copy pathShareWrapperService.php
More file actions
377 lines (318 loc) · 9.34 KB
/
ShareWrapperService.php
File metadata and controls
377 lines (318 loc) · 9.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
<?php
declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2021 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace OCA\Circles\Service;
use Exception;
use OCA\Circles\Db\ShareWrapperRequest;
use OCA\Circles\Exceptions\RequestBuilderException;
use OCA\Circles\Exceptions\ShareWrapperNotFoundException;
use OCA\Circles\Model\FederatedUser;
use OCA\Circles\Model\Probes\CircleProbe;
use OCA\Circles\Model\ShareWrapper;
use OCA\Circles\Tools\Exceptions\InvalidItemException;
use OCA\Circles\Tools\Traits\TDeserialize;
use OCA\Circles\Tools\Traits\TStringTools;
use OCP\Files\Folder;
use OCP\Files\NotFoundException;
use OCP\ICache;
use OCP\ICacheFactory;
use OCP\Share\IShare;
/**
* Class ShareWrapperService
*
* @package OCA\Circles\Service
*/
class ShareWrapperService {
use TStringTools;
use TDeserialize;
public const CACHE_SHARED_WITH = 'circles/getSharedWith';
public const CACHE_SHARED_WITH_TTL = 900;
/** @var ShareWrapperRequest */
private $shareWrapperRequest;
private ICache $cache;
/**
* ShareWrapperService constructor.
*
* @param ICacheFactory $cacheFactory
* @param ShareWrapperRequest $shareWrapperRequest
*/
public function __construct(ICacheFactory $cacheFactory, ShareWrapperRequest $shareWrapperRequest) {
$this->cache = $cacheFactory->createDistributed(self::CACHE_SHARED_WITH);
$this->shareWrapperRequest = $shareWrapperRequest;
}
/**
* @param string $singleId
* @param int $nodeId
*
* @return ShareWrapper
* @throws RequestBuilderException
* @throws ShareWrapperNotFoundException
*/
public function searchShare(string $singleId, int $nodeId): ShareWrapper {
return $this->shareWrapperRequest->searchShare($singleId, $nodeId);
}
/**
* @param IShare $share
*
* @throws NotFoundException
*/
public function save(IShare $share): void {
$this->cache->clear('');
$this->shareWrapperRequest->save($share);
}
/**
* @param ShareWrapper $shareWrapper
*/
public function update(ShareWrapper $shareWrapper): void {
$this->cache->clear('');
$this->shareWrapperRequest->update($shareWrapper);
}
public function updateChildPermissions(ShareWrapper $shareWrapper): void {
$this->cache->clear('');
$this->shareWrapperRequest->updateChildPermissions($shareWrapper);
}
/**
* @param ShareWrapper $shareWrapper
*/
public function delete(ShareWrapper $shareWrapper): void {
$this->cache->clear('');
$this->shareWrapperRequest->delete((int)$shareWrapper->getId());
}
/**
* @param string $circleId
* @param string $userId
*
* @throws Exception
*/
public function deleteUserSharesToCircle(string $circleId, string $userId): void {
if ($userId === '') {
throw new Exception('$initiator cannot be empty');
}
$this->cache->clear('');
$this->shareWrapperRequest->deleteSharesToCircle($circleId, $userId);
}
/**
* @param string $circleId
*/
public function deleteAllSharesToCircle(string $circleId): void {
$this->cache->clear('');
$this->shareWrapperRequest->deleteSharesToCircle($circleId, '');
}
/**
* @param string $circleId
* @param FederatedUser|null $shareRecipient
* @param FederatedUser|null $shareInitiator
* @param bool $completeDetails
*
* @return ShareWrapper[]
* @throws RequestBuilderException
*/
public function getSharesToCircle(
string $circleId,
?FederatedUser $shareRecipient = null,
?FederatedUser $shareInitiator = null,
bool $completeDetails = false,
): array {
return $this->shareWrapperRequest->getSharesToCircle(
$circleId,
$shareRecipient,
$shareInitiator,
$completeDetails
);
}
/**
* @return ShareWrapper[]
*/
public function getSharesToCircles(array $circleIds): array {
return $this->shareWrapperRequest->getSharesToCircles($circleIds);
}
/**
* @param int $shareId
* @param FederatedUser|null $federatedUser
*
* @return ShareWrapper
* @throws ShareWrapperNotFoundException
* @throws RequestBuilderException
*/
public function getShareById(int $shareId, ?FederatedUser $federatedUser = null): ShareWrapper {
return $this->shareWrapperRequest->getShareById($shareId, $federatedUser);
}
/**
* @param int $fileId
* @param bool $getData
*
* @return ShareWrapper[]
* @throws RequestBuilderException
*/
public function getSharesByFileId(int $fileId, bool $getData = false): array {
return $this->shareWrapperRequest->getSharesByFileId($fileId, $getData);
}
/**
* @param array $fileIds
* @param bool $getData
*
* @return ShareWrapper[]
* @throws RequestBuilderException
*/
public function getSharesByFileIds(array $fileIds, bool $getData = false, bool $getChild = false): array {
return ($fileIds === []) ? [] : $this->shareWrapperRequest->getSharesByFileIds($fileIds, $getData, $getChild);
}
/**
* @param string $token
* @param FederatedUser|null $federatedUser
*
* @return ShareWrapper
* @throws RequestBuilderException
* @throws ShareWrapperNotFoundException
*/
public function getShareByToken(string $token, ?FederatedUser $federatedUser = null): ShareWrapper {
return $this->shareWrapperRequest->getShareByToken($token, $federatedUser);
}
/**
* @param FederatedUser $federatedUser
* @param int $nodeId
* @param CircleProbe|null $probe
*
* @return ShareWrapper[]
* @throws RequestBuilderException
*/
public function getSharedWith(
FederatedUser $federatedUser,
int $nodeId,
?CircleProbe $probe,
): array {
$key = $this->generateSharedWithCacheKey($federatedUser, $nodeId, $probe->getChecksum());
$cachedData = $this->cache->get($key);
try {
if (!is_string($cachedData)) {
throw new InvalidItemException();
}
return $this->deserializeList($cachedData, ShareWrapper::class);
} catch (InvalidItemException $e) {
}
$shares = $this->shareWrapperRequest->getSharedWith($federatedUser, $nodeId, $probe);
$this->cache->set($key, json_encode($shares), self::CACHE_SHARED_WITH_TTL);
return $shares;
}
public function getSharedWithByPath(
FederatedUser $federatedUser,
string $path,
bool $forChildren,
?CircleProbe $probe,
): array {
$key = $this->generateSharedWithByPathCacheKey($federatedUser, $path, $forChildren, $probe?->getChecksum());
$cachedData = $this->cache->get($key);
try {
if (!is_string($cachedData)) {
throw new InvalidItemException();
}
return $this->deserializeList($cachedData, ShareWrapper::class);
} catch (InvalidItemException $e) {
}
$shares = $this->shareWrapperRequest->getSharedWithByPath($federatedUser, $path, $forChildren, $probe);
$this->cache->set($key, json_encode($shares), self::CACHE_SHARED_WITH_TTL);
return $shares;
}
/**
* @param FederatedUser $federatedUser
* @param int $nodeId
* @param bool $reshares
* @param int $offset
* @param int $limit
* @param bool $getData
* @param bool $completeDetails
*
* @return ShareWrapper[]
* @throws RequestBuilderException
*/
public function getSharesBy(
FederatedUser $federatedUser,
int $nodeId,
bool $reshares,
int $limit,
int $offset,
bool $getData = false,
bool $completeDetails = false,
): array {
return $this->shareWrapperRequest->getSharesBy(
$federatedUser, $nodeId, $reshares, $limit, $offset, $getData, $completeDetails
);
}
/**
* @param FederatedUser $federatedUser
* @param Folder $node
* @param bool $reshares
* @param bool $shallow Whether the method should stop at the first level, or look into sub-folders.
*
* @return ShareWrapper[]
* @throws RequestBuilderException
*/
public function getSharesInFolder(FederatedUser $federatedUser, Folder $node, bool $reshares, bool $shallow = true): array {
return $this->shareWrapperRequest->getSharesInFolder($federatedUser, $node, $reshares, $shallow);
}
/**
* @param FederatedUser $federatedUser
* @param IShare $share
*
* @return ShareWrapper
* @throws NotFoundException
* @throws ShareWrapperNotFoundException
* @throws RequestBuilderException
*/
public function getChild(IShare $share, FederatedUser $federatedUser): ShareWrapper {
try {
return $this->shareWrapperRequest->getChild($federatedUser, (int)$share->getId());
} catch (ShareWrapperNotFoundException $e) {
}
return $this->createChild($share, $federatedUser);
}
public function clearCache(string $singleId): void {
$this->cache->clear($singleId);
}
/**
* @param FederatedUser $federatedUser
* @param IShare $share
*
* @return ShareWrapper
* @throws ShareWrapperNotFoundException
* @throws NotFoundException
* @throws RequestBuilderException
*/
private function createChild(IShare $share, FederatedUser $federatedUser): ShareWrapper {
$this->cache->clear('');
$share->setSharedWith($federatedUser->getSingleId());
$childId = $this->shareWrapperRequest->save($share, (int)$share->getId());
return $this->getShareById($childId, $federatedUser);
}
private function generateSharedWithByPathCacheKey(
FederatedUser $federatedUser,
string $path,
bool $forChildren,
?string $probeSum,
): string {
$pathHash = \md5($path);
return $federatedUser->getSingleId() . '#'
. $pathHash . '#'
. $forChildren . '#'
. $probeSum;
}
/**
* @param FederatedUser $federatedUser
* @param int $nodeId
* @param string $probeSum
*
* @return string
*/
private function generateSharedWithCacheKey(
FederatedUser $federatedUser,
int $nodeId,
string $probeSum,
): string {
return $federatedUser->getSingleId() . '#'
. $nodeId . '#'
. $probeSum;
}
}