-
-
Notifications
You must be signed in to change notification settings - Fork 5.1k
Expand file tree
/
Copy pathSharedMount.php
More file actions
189 lines (159 loc) · 4.61 KB
/
Copy pathSharedMount.php
File metadata and controls
189 lines (159 loc) · 4.61 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
<?php
/**
* SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
* SPDX-FileCopyrightText: 2016 ownCloud, Inc.
* SPDX-License-Identifier: AGPL-3.0-only
*/
namespace OCA\Files_Sharing;
use OC\Files\Filesystem;
use OC\Files\Mount\MountPoint;
use OCA\Files_Sharing\Exceptions\BrokenPath;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\Files\Events\InvalidateMountCacheEvent;
use OCP\Files\Mount\IMovableMount;
use OCP\Files\Storage\IStorageFactory;
use OCP\IDBConnection;
use OCP\IUser;
use OCP\Server;
use OCP\Share\IShare;
use Override;
use Psr\Log\LoggerInterface;
/**
* Shared mount points can be moved by the user
*/
class SharedMount extends MountPoint implements IMovableMount, ISharedMountPoint {
/**
* @var ?SharedStorage $storage
*/
protected $storage = null;
/** @var IShare */
private $superShare;
/** @var IShare[] */
private $groupedShares;
public function __construct(
$storage,
$arguments,
IStorageFactory $loader,
private IEventDispatcher $eventDispatcher,
private IUser $user,
) {
$this->superShare = $arguments['superShare'];
$this->groupedShares = $arguments['groupedShares'];
$absMountPoint = '/' . $user->getUID() . '/files/' . trim($this->superShare->getTarget(), '/') . '/';
parent::__construct($storage, $absMountPoint, $arguments, $loader, null, null, MountProvider::class);
}
/**
* update fileTarget in the database if the mount point changed
*
* @param string $newPath
* @param IShare $share
* @return bool
*/
protected function updateFileTarget($newPath, &$share) {
$share->setTarget($newPath);
foreach ($this->groupedShares as $tmpShare) {
$tmpShare->setTarget($newPath);
Server::get(\OCP\Share\IManager::class)->moveShare($tmpShare, $this->user->getUID());
}
$this->eventDispatcher->dispatchTyped(new InvalidateMountCacheEvent($this->user));
}
/**
* Format a path to be relative to the /user/files/ directory
*
* @param string $path the absolute path
* @return string e.g. turns '/admin/files/test.txt' into '/test.txt'
* @throws BrokenPath
*/
protected function stripUserFilesPath(string $path): string {
$trimmed = ltrim($path, '/');
$split = explode('/', $trimmed);
// it is not a file relative to data/user/files
if (count($split) < 3 || $split[1] !== 'files') {
Server::get(LoggerInterface::class)->error('Can not strip userid and "files/" from path: ' . $path, ['app' => 'files_sharing']);
throw new BrokenPath('Path does not start with /user/files', 10);
}
// skip 'user' and 'files'
$sliced = array_slice($split, 2);
$relPath = implode('/', $sliced);
return '/' . $relPath;
}
#[Override]
public function moveMount(string $target): bool {
$relTargetPath = $this->stripUserFilesPath($target);
$share = $this->storage->getShare();
$result = true;
try {
$this->updateFileTarget($relTargetPath, $share);
$this->setMountPoint($target);
$this->storage->setMountPoint($relTargetPath);
} catch (\Exception $e) {
Server::get(LoggerInterface::class)->error(
'Could not rename mount point for shared folder "' . $this->getMountPoint() . '" to "' . $target . '"',
[
'app' => 'files_sharing',
'exception' => $e,
]
);
$result = false;
}
return $result;
}
#[Override]
public function removeMount(): bool {
$mountManager = Filesystem::getMountManager();
/** @var SharedStorage $storage */
$storage = $this->getStorage();
$result = $storage->unshareStorage();
$mountManager->removeMount($this->mountPoint);
return $result;
}
/**
* @return IShare
*/
public function getShare() {
return $this->superShare;
}
/**
* @return IShare[]
*/
public function getGroupedShares(): array {
return $this->groupedShares;
}
/**
* Get the file id of the root of the storage
*
* @return int
*/
#[\Override]
public function getStorageRootId() {
return $this->getShare()->getNodeId();
}
/**
* @return int
*/
#[\Override]
public function getNumericStorageId() {
if (!is_null($this->getShare()->getNodeCacheEntry())) {
return $this->getShare()->getNodeCacheEntry()->getStorageId();
} else {
$builder = Server::get(IDBConnection::class)->getQueryBuilder();
$query = $builder->select('storage')
->from('filecache')
->where($builder->expr()->eq('fileid', $builder->createNamedParameter($this->getStorageRootId())));
$result = $query->executeQuery();
$row = $result->fetchAssociative();
$result->closeCursor();
if ($row) {
return (int)$row['storage'];
}
return -1;
}
}
#[Override]
public function getMountType(): string {
return 'shared';
}
public function getUser(): IUser {
return $this->user;
}
}