@@ -45,246 +45,246 @@ |
||
45 | 45 | * Shared mount points can be moved by the user |
46 | 46 | */ |
47 | 47 | class SharedMount extends MountPoint implements MoveableMount { |
48 | - /** |
|
49 | - * @var \OCA\Files_Sharing\SharedStorage $storage |
|
50 | - */ |
|
51 | - protected $storage = null; |
|
52 | - |
|
53 | - /** |
|
54 | - * @var \OC\Files\View |
|
55 | - */ |
|
56 | - private $recipientView; |
|
57 | - |
|
58 | - private IUser $user; |
|
59 | - |
|
60 | - /** @var \OCP\Share\IShare */ |
|
61 | - private $superShare; |
|
62 | - |
|
63 | - /** @var \OCP\Share\IShare[] */ |
|
64 | - private $groupedShares; |
|
65 | - |
|
66 | - private IEventDispatcher $eventDispatcher; |
|
67 | - |
|
68 | - private ICache $cache; |
|
69 | - |
|
70 | - public function __construct( |
|
71 | - $storage, |
|
72 | - array $mountpoints, |
|
73 | - $arguments, |
|
74 | - IStorageFactory $loader, |
|
75 | - View $recipientView, |
|
76 | - CappedMemoryCache $folderExistCache, |
|
77 | - IEventDispatcher $eventDispatcher, |
|
78 | - IUser $user, |
|
79 | - ICache $cache |
|
80 | - ) { |
|
81 | - $this->user = $user; |
|
82 | - $this->recipientView = $recipientView; |
|
83 | - $this->eventDispatcher = $eventDispatcher; |
|
84 | - $this->cache = $cache; |
|
85 | - |
|
86 | - $this->superShare = $arguments['superShare']; |
|
87 | - $this->groupedShares = $arguments['groupedShares']; |
|
88 | - |
|
89 | - $newMountPoint = $this->verifyMountPoint($this->superShare, $mountpoints, $folderExistCache); |
|
90 | - $absMountPoint = '/' . $user->getUID() . '/files' . $newMountPoint; |
|
91 | - parent::__construct($storage, $absMountPoint, $arguments, $loader, null, null, MountProvider::class); |
|
92 | - } |
|
93 | - |
|
94 | - /** |
|
95 | - * check if the parent folder exists otherwise move the mount point up |
|
96 | - * |
|
97 | - * @param \OCP\Share\IShare $share |
|
98 | - * @param SharedMount[] $mountpoints |
|
99 | - * @return string |
|
100 | - */ |
|
101 | - private function verifyMountPoint( |
|
102 | - \OCP\Share\IShare $share, |
|
103 | - array $mountpoints, |
|
104 | - CappedMemoryCache $folderExistCache |
|
105 | - ) { |
|
106 | - $cacheKey = $this->user->getUID() . '/' . $share->getTarget(); |
|
107 | - $cached = $this->cache->get($cacheKey); |
|
108 | - if ($cached !== null) { |
|
109 | - return $cached; |
|
110 | - } |
|
111 | - |
|
112 | - $mountPoint = basename($share->getTarget()); |
|
113 | - $parent = dirname($share->getTarget()); |
|
114 | - |
|
115 | - $event = new VerifyMountPointEvent($share, $this->recipientView, $parent); |
|
116 | - $this->eventDispatcher->dispatchTyped($event); |
|
117 | - $parent = $event->getParent(); |
|
118 | - |
|
119 | - if ($folderExistCache->hasKey($parent)) { |
|
120 | - $parentExists = $folderExistCache->get($parent); |
|
121 | - } else { |
|
122 | - $parentExists = $this->recipientView->is_dir($parent); |
|
123 | - $folderExistCache->set($parent, $parentExists); |
|
124 | - } |
|
125 | - if (!$parentExists) { |
|
126 | - $parent = Helper::getShareFolder($this->recipientView, $this->user->getUID()); |
|
127 | - } |
|
128 | - |
|
129 | - $newMountPoint = $this->generateUniqueTarget( |
|
130 | - \OC\Files\Filesystem::normalizePath($parent . '/' . $mountPoint), |
|
131 | - $this->recipientView, |
|
132 | - $mountpoints |
|
133 | - ); |
|
134 | - |
|
135 | - if ($newMountPoint !== $share->getTarget()) { |
|
136 | - $this->updateFileTarget($newMountPoint, $share); |
|
137 | - } |
|
138 | - |
|
139 | - $this->cache->set($cacheKey, $newMountPoint, 60 * 60); |
|
140 | - |
|
141 | - return $newMountPoint; |
|
142 | - } |
|
143 | - |
|
144 | - /** |
|
145 | - * update fileTarget in the database if the mount point changed |
|
146 | - * |
|
147 | - * @param string $newPath |
|
148 | - * @param \OCP\Share\IShare $share |
|
149 | - * @return bool |
|
150 | - */ |
|
151 | - private function updateFileTarget($newPath, &$share) { |
|
152 | - $share->setTarget($newPath); |
|
153 | - |
|
154 | - foreach ($this->groupedShares as $tmpShare) { |
|
155 | - $tmpShare->setTarget($newPath); |
|
156 | - \OC::$server->getShareManager()->moveShare($tmpShare, $this->user->getUID()); |
|
157 | - } |
|
158 | - |
|
159 | - $this->eventDispatcher->dispatchTyped(new InvalidateMountCacheEvent($this->user)); |
|
160 | - } |
|
161 | - |
|
162 | - |
|
163 | - /** |
|
164 | - * @param string $path |
|
165 | - * @param View $view |
|
166 | - * @param SharedMount[] $mountpoints |
|
167 | - * @return mixed |
|
168 | - */ |
|
169 | - private function generateUniqueTarget($path, $view, array $mountpoints) { |
|
170 | - $pathinfo = pathinfo($path); |
|
171 | - $ext = isset($pathinfo['extension']) ? '.' . $pathinfo['extension'] : ''; |
|
172 | - $name = $pathinfo['filename']; |
|
173 | - $dir = $pathinfo['dirname']; |
|
174 | - |
|
175 | - $i = 2; |
|
176 | - $absolutePath = $this->recipientView->getAbsolutePath($path) . '/'; |
|
177 | - while ($view->file_exists($path) || isset($mountpoints[$absolutePath])) { |
|
178 | - $path = Filesystem::normalizePath($dir . '/' . $name . ' (' . $i . ')' . $ext); |
|
179 | - $absolutePath = $this->recipientView->getAbsolutePath($path) . '/'; |
|
180 | - $i++; |
|
181 | - } |
|
182 | - |
|
183 | - return $path; |
|
184 | - } |
|
185 | - |
|
186 | - /** |
|
187 | - * Format a path to be relative to the /user/files/ directory |
|
188 | - * |
|
189 | - * @param string $path the absolute path |
|
190 | - * @return string e.g. turns '/admin/files/test.txt' into '/test.txt' |
|
191 | - * @throws \OCA\Files_Sharing\Exceptions\BrokenPath |
|
192 | - */ |
|
193 | - protected function stripUserFilesPath($path) { |
|
194 | - $trimmed = ltrim($path, '/'); |
|
195 | - $split = explode('/', $trimmed); |
|
196 | - |
|
197 | - // it is not a file relative to data/user/files |
|
198 | - if (count($split) < 3 || $split[1] !== 'files') { |
|
199 | - \OC::$server->getLogger()->error('Can not strip userid and "files/" from path: ' . $path, ['app' => 'files_sharing']); |
|
200 | - throw new \OCA\Files_Sharing\Exceptions\BrokenPath('Path does not start with /user/files', 10); |
|
201 | - } |
|
202 | - |
|
203 | - // skip 'user' and 'files' |
|
204 | - $sliced = array_slice($split, 2); |
|
205 | - $relPath = implode('/', $sliced); |
|
206 | - |
|
207 | - return '/' . $relPath; |
|
208 | - } |
|
209 | - |
|
210 | - /** |
|
211 | - * Move the mount point to $target |
|
212 | - * |
|
213 | - * @param string $target the target mount point |
|
214 | - * @return bool |
|
215 | - */ |
|
216 | - public function moveMount($target) { |
|
217 | - $relTargetPath = $this->stripUserFilesPath($target); |
|
218 | - $share = $this->storage->getShare(); |
|
219 | - |
|
220 | - $result = true; |
|
221 | - |
|
222 | - try { |
|
223 | - $this->updateFileTarget($relTargetPath, $share); |
|
224 | - $this->setMountPoint($target); |
|
225 | - $this->storage->setMountPoint($relTargetPath); |
|
226 | - } catch (\Exception $e) { |
|
227 | - \OC::$server->getLogger()->logException($e, ['app' => 'files_sharing', 'message' => 'Could not rename mount point for shared folder "' . $this->getMountPoint() . '" to "' . $target . '"']); |
|
228 | - } |
|
229 | - |
|
230 | - return $result; |
|
231 | - } |
|
232 | - |
|
233 | - /** |
|
234 | - * Remove the mount points |
|
235 | - * |
|
236 | - * @return bool |
|
237 | - */ |
|
238 | - public function removeMount() { |
|
239 | - $mountManager = \OC\Files\Filesystem::getMountManager(); |
|
240 | - /** @var \OCA\Files_Sharing\SharedStorage $storage */ |
|
241 | - $storage = $this->getStorage(); |
|
242 | - $result = $storage->unshareStorage(); |
|
243 | - $mountManager->removeMount($this->mountPoint); |
|
244 | - |
|
245 | - return $result; |
|
246 | - } |
|
247 | - |
|
248 | - /** |
|
249 | - * @return \OCP\Share\IShare |
|
250 | - */ |
|
251 | - public function getShare() { |
|
252 | - return $this->superShare; |
|
253 | - } |
|
254 | - |
|
255 | - /** |
|
256 | - * Get the file id of the root of the storage |
|
257 | - * |
|
258 | - * @return int |
|
259 | - */ |
|
260 | - public function getStorageRootId() { |
|
261 | - return $this->getShare()->getNodeId(); |
|
262 | - } |
|
263 | - |
|
264 | - /** |
|
265 | - * @return int |
|
266 | - */ |
|
267 | - public function getNumericStorageId() { |
|
268 | - if (!is_null($this->getShare()->getNodeCacheEntry())) { |
|
269 | - return $this->getShare()->getNodeCacheEntry()->getStorageId(); |
|
270 | - } else { |
|
271 | - $builder = \OC::$server->getDatabaseConnection()->getQueryBuilder(); |
|
272 | - |
|
273 | - $query = $builder->select('storage') |
|
274 | - ->from('filecache') |
|
275 | - ->where($builder->expr()->eq('fileid', $builder->createNamedParameter($this->getStorageRootId()))); |
|
276 | - |
|
277 | - $result = $query->execute(); |
|
278 | - $row = $result->fetch(); |
|
279 | - $result->closeCursor(); |
|
280 | - if ($row) { |
|
281 | - return (int)$row['storage']; |
|
282 | - } |
|
283 | - return -1; |
|
284 | - } |
|
285 | - } |
|
286 | - |
|
287 | - public function getMountType() { |
|
288 | - return 'shared'; |
|
289 | - } |
|
48 | + /** |
|
49 | + * @var \OCA\Files_Sharing\SharedStorage $storage |
|
50 | + */ |
|
51 | + protected $storage = null; |
|
52 | + |
|
53 | + /** |
|
54 | + * @var \OC\Files\View |
|
55 | + */ |
|
56 | + private $recipientView; |
|
57 | + |
|
58 | + private IUser $user; |
|
59 | + |
|
60 | + /** @var \OCP\Share\IShare */ |
|
61 | + private $superShare; |
|
62 | + |
|
63 | + /** @var \OCP\Share\IShare[] */ |
|
64 | + private $groupedShares; |
|
65 | + |
|
66 | + private IEventDispatcher $eventDispatcher; |
|
67 | + |
|
68 | + private ICache $cache; |
|
69 | + |
|
70 | + public function __construct( |
|
71 | + $storage, |
|
72 | + array $mountpoints, |
|
73 | + $arguments, |
|
74 | + IStorageFactory $loader, |
|
75 | + View $recipientView, |
|
76 | + CappedMemoryCache $folderExistCache, |
|
77 | + IEventDispatcher $eventDispatcher, |
|
78 | + IUser $user, |
|
79 | + ICache $cache |
|
80 | + ) { |
|
81 | + $this->user = $user; |
|
82 | + $this->recipientView = $recipientView; |
|
83 | + $this->eventDispatcher = $eventDispatcher; |
|
84 | + $this->cache = $cache; |
|
85 | + |
|
86 | + $this->superShare = $arguments['superShare']; |
|
87 | + $this->groupedShares = $arguments['groupedShares']; |
|
88 | + |
|
89 | + $newMountPoint = $this->verifyMountPoint($this->superShare, $mountpoints, $folderExistCache); |
|
90 | + $absMountPoint = '/' . $user->getUID() . '/files' . $newMountPoint; |
|
91 | + parent::__construct($storage, $absMountPoint, $arguments, $loader, null, null, MountProvider::class); |
|
92 | + } |
|
93 | + |
|
94 | + /** |
|
95 | + * check if the parent folder exists otherwise move the mount point up |
|
96 | + * |
|
97 | + * @param \OCP\Share\IShare $share |
|
98 | + * @param SharedMount[] $mountpoints |
|
99 | + * @return string |
|
100 | + */ |
|
101 | + private function verifyMountPoint( |
|
102 | + \OCP\Share\IShare $share, |
|
103 | + array $mountpoints, |
|
104 | + CappedMemoryCache $folderExistCache |
|
105 | + ) { |
|
106 | + $cacheKey = $this->user->getUID() . '/' . $share->getTarget(); |
|
107 | + $cached = $this->cache->get($cacheKey); |
|
108 | + if ($cached !== null) { |
|
109 | + return $cached; |
|
110 | + } |
|
111 | + |
|
112 | + $mountPoint = basename($share->getTarget()); |
|
113 | + $parent = dirname($share->getTarget()); |
|
114 | + |
|
115 | + $event = new VerifyMountPointEvent($share, $this->recipientView, $parent); |
|
116 | + $this->eventDispatcher->dispatchTyped($event); |
|
117 | + $parent = $event->getParent(); |
|
118 | + |
|
119 | + if ($folderExistCache->hasKey($parent)) { |
|
120 | + $parentExists = $folderExistCache->get($parent); |
|
121 | + } else { |
|
122 | + $parentExists = $this->recipientView->is_dir($parent); |
|
123 | + $folderExistCache->set($parent, $parentExists); |
|
124 | + } |
|
125 | + if (!$parentExists) { |
|
126 | + $parent = Helper::getShareFolder($this->recipientView, $this->user->getUID()); |
|
127 | + } |
|
128 | + |
|
129 | + $newMountPoint = $this->generateUniqueTarget( |
|
130 | + \OC\Files\Filesystem::normalizePath($parent . '/' . $mountPoint), |
|
131 | + $this->recipientView, |
|
132 | + $mountpoints |
|
133 | + ); |
|
134 | + |
|
135 | + if ($newMountPoint !== $share->getTarget()) { |
|
136 | + $this->updateFileTarget($newMountPoint, $share); |
|
137 | + } |
|
138 | + |
|
139 | + $this->cache->set($cacheKey, $newMountPoint, 60 * 60); |
|
140 | + |
|
141 | + return $newMountPoint; |
|
142 | + } |
|
143 | + |
|
144 | + /** |
|
145 | + * update fileTarget in the database if the mount point changed |
|
146 | + * |
|
147 | + * @param string $newPath |
|
148 | + * @param \OCP\Share\IShare $share |
|
149 | + * @return bool |
|
150 | + */ |
|
151 | + private function updateFileTarget($newPath, &$share) { |
|
152 | + $share->setTarget($newPath); |
|
153 | + |
|
154 | + foreach ($this->groupedShares as $tmpShare) { |
|
155 | + $tmpShare->setTarget($newPath); |
|
156 | + \OC::$server->getShareManager()->moveShare($tmpShare, $this->user->getUID()); |
|
157 | + } |
|
158 | + |
|
159 | + $this->eventDispatcher->dispatchTyped(new InvalidateMountCacheEvent($this->user)); |
|
160 | + } |
|
161 | + |
|
162 | + |
|
163 | + /** |
|
164 | + * @param string $path |
|
165 | + * @param View $view |
|
166 | + * @param SharedMount[] $mountpoints |
|
167 | + * @return mixed |
|
168 | + */ |
|
169 | + private function generateUniqueTarget($path, $view, array $mountpoints) { |
|
170 | + $pathinfo = pathinfo($path); |
|
171 | + $ext = isset($pathinfo['extension']) ? '.' . $pathinfo['extension'] : ''; |
|
172 | + $name = $pathinfo['filename']; |
|
173 | + $dir = $pathinfo['dirname']; |
|
174 | + |
|
175 | + $i = 2; |
|
176 | + $absolutePath = $this->recipientView->getAbsolutePath($path) . '/'; |
|
177 | + while ($view->file_exists($path) || isset($mountpoints[$absolutePath])) { |
|
178 | + $path = Filesystem::normalizePath($dir . '/' . $name . ' (' . $i . ')' . $ext); |
|
179 | + $absolutePath = $this->recipientView->getAbsolutePath($path) . '/'; |
|
180 | + $i++; |
|
181 | + } |
|
182 | + |
|
183 | + return $path; |
|
184 | + } |
|
185 | + |
|
186 | + /** |
|
187 | + * Format a path to be relative to the /user/files/ directory |
|
188 | + * |
|
189 | + * @param string $path the absolute path |
|
190 | + * @return string e.g. turns '/admin/files/test.txt' into '/test.txt' |
|
191 | + * @throws \OCA\Files_Sharing\Exceptions\BrokenPath |
|
192 | + */ |
|
193 | + protected function stripUserFilesPath($path) { |
|
194 | + $trimmed = ltrim($path, '/'); |
|
195 | + $split = explode('/', $trimmed); |
|
196 | + |
|
197 | + // it is not a file relative to data/user/files |
|
198 | + if (count($split) < 3 || $split[1] !== 'files') { |
|
199 | + \OC::$server->getLogger()->error('Can not strip userid and "files/" from path: ' . $path, ['app' => 'files_sharing']); |
|
200 | + throw new \OCA\Files_Sharing\Exceptions\BrokenPath('Path does not start with /user/files', 10); |
|
201 | + } |
|
202 | + |
|
203 | + // skip 'user' and 'files' |
|
204 | + $sliced = array_slice($split, 2); |
|
205 | + $relPath = implode('/', $sliced); |
|
206 | + |
|
207 | + return '/' . $relPath; |
|
208 | + } |
|
209 | + |
|
210 | + /** |
|
211 | + * Move the mount point to $target |
|
212 | + * |
|
213 | + * @param string $target the target mount point |
|
214 | + * @return bool |
|
215 | + */ |
|
216 | + public function moveMount($target) { |
|
217 | + $relTargetPath = $this->stripUserFilesPath($target); |
|
218 | + $share = $this->storage->getShare(); |
|
219 | + |
|
220 | + $result = true; |
|
221 | + |
|
222 | + try { |
|
223 | + $this->updateFileTarget($relTargetPath, $share); |
|
224 | + $this->setMountPoint($target); |
|
225 | + $this->storage->setMountPoint($relTargetPath); |
|
226 | + } catch (\Exception $e) { |
|
227 | + \OC::$server->getLogger()->logException($e, ['app' => 'files_sharing', 'message' => 'Could not rename mount point for shared folder "' . $this->getMountPoint() . '" to "' . $target . '"']); |
|
228 | + } |
|
229 | + |
|
230 | + return $result; |
|
231 | + } |
|
232 | + |
|
233 | + /** |
|
234 | + * Remove the mount points |
|
235 | + * |
|
236 | + * @return bool |
|
237 | + */ |
|
238 | + public function removeMount() { |
|
239 | + $mountManager = \OC\Files\Filesystem::getMountManager(); |
|
240 | + /** @var \OCA\Files_Sharing\SharedStorage $storage */ |
|
241 | + $storage = $this->getStorage(); |
|
242 | + $result = $storage->unshareStorage(); |
|
243 | + $mountManager->removeMount($this->mountPoint); |
|
244 | + |
|
245 | + return $result; |
|
246 | + } |
|
247 | + |
|
248 | + /** |
|
249 | + * @return \OCP\Share\IShare |
|
250 | + */ |
|
251 | + public function getShare() { |
|
252 | + return $this->superShare; |
|
253 | + } |
|
254 | + |
|
255 | + /** |
|
256 | + * Get the file id of the root of the storage |
|
257 | + * |
|
258 | + * @return int |
|
259 | + */ |
|
260 | + public function getStorageRootId() { |
|
261 | + return $this->getShare()->getNodeId(); |
|
262 | + } |
|
263 | + |
|
264 | + /** |
|
265 | + * @return int |
|
266 | + */ |
|
267 | + public function getNumericStorageId() { |
|
268 | + if (!is_null($this->getShare()->getNodeCacheEntry())) { |
|
269 | + return $this->getShare()->getNodeCacheEntry()->getStorageId(); |
|
270 | + } else { |
|
271 | + $builder = \OC::$server->getDatabaseConnection()->getQueryBuilder(); |
|
272 | + |
|
273 | + $query = $builder->select('storage') |
|
274 | + ->from('filecache') |
|
275 | + ->where($builder->expr()->eq('fileid', $builder->createNamedParameter($this->getStorageRootId()))); |
|
276 | + |
|
277 | + $result = $query->execute(); |
|
278 | + $row = $result->fetch(); |
|
279 | + $result->closeCursor(); |
|
280 | + if ($row) { |
|
281 | + return (int)$row['storage']; |
|
282 | + } |
|
283 | + return -1; |
|
284 | + } |
|
285 | + } |
|
286 | + |
|
287 | + public function getMountType() { |
|
288 | + return 'shared'; |
|
289 | + } |
|
290 | 290 | } |
@@ -87,7 +87,7 @@ discard block |
||
87 | 87 | $this->groupedShares = $arguments['groupedShares']; |
88 | 88 | |
89 | 89 | $newMountPoint = $this->verifyMountPoint($this->superShare, $mountpoints, $folderExistCache); |
90 | - $absMountPoint = '/' . $user->getUID() . '/files' . $newMountPoint; |
|
90 | + $absMountPoint = '/'.$user->getUID().'/files'.$newMountPoint; |
|
91 | 91 | parent::__construct($storage, $absMountPoint, $arguments, $loader, null, null, MountProvider::class); |
92 | 92 | } |
93 | 93 | |
@@ -103,7 +103,7 @@ discard block |
||
103 | 103 | array $mountpoints, |
104 | 104 | CappedMemoryCache $folderExistCache |
105 | 105 | ) { |
106 | - $cacheKey = $this->user->getUID() . '/' . $share->getTarget(); |
|
106 | + $cacheKey = $this->user->getUID().'/'.$share->getTarget(); |
|
107 | 107 | $cached = $this->cache->get($cacheKey); |
108 | 108 | if ($cached !== null) { |
109 | 109 | return $cached; |
@@ -127,7 +127,7 @@ discard block |
||
127 | 127 | } |
128 | 128 | |
129 | 129 | $newMountPoint = $this->generateUniqueTarget( |
130 | - \OC\Files\Filesystem::normalizePath($parent . '/' . $mountPoint), |
|
130 | + \OC\Files\Filesystem::normalizePath($parent.'/'.$mountPoint), |
|
131 | 131 | $this->recipientView, |
132 | 132 | $mountpoints |
133 | 133 | ); |
@@ -168,15 +168,15 @@ discard block |
||
168 | 168 | */ |
169 | 169 | private function generateUniqueTarget($path, $view, array $mountpoints) { |
170 | 170 | $pathinfo = pathinfo($path); |
171 | - $ext = isset($pathinfo['extension']) ? '.' . $pathinfo['extension'] : ''; |
|
171 | + $ext = isset($pathinfo['extension']) ? '.'.$pathinfo['extension'] : ''; |
|
172 | 172 | $name = $pathinfo['filename']; |
173 | 173 | $dir = $pathinfo['dirname']; |
174 | 174 | |
175 | 175 | $i = 2; |
176 | - $absolutePath = $this->recipientView->getAbsolutePath($path) . '/'; |
|
176 | + $absolutePath = $this->recipientView->getAbsolutePath($path).'/'; |
|
177 | 177 | while ($view->file_exists($path) || isset($mountpoints[$absolutePath])) { |
178 | - $path = Filesystem::normalizePath($dir . '/' . $name . ' (' . $i . ')' . $ext); |
|
179 | - $absolutePath = $this->recipientView->getAbsolutePath($path) . '/'; |
|
178 | + $path = Filesystem::normalizePath($dir.'/'.$name.' ('.$i.')'.$ext); |
|
179 | + $absolutePath = $this->recipientView->getAbsolutePath($path).'/'; |
|
180 | 180 | $i++; |
181 | 181 | } |
182 | 182 | |
@@ -196,7 +196,7 @@ discard block |
||
196 | 196 | |
197 | 197 | // it is not a file relative to data/user/files |
198 | 198 | if (count($split) < 3 || $split[1] !== 'files') { |
199 | - \OC::$server->getLogger()->error('Can not strip userid and "files/" from path: ' . $path, ['app' => 'files_sharing']); |
|
199 | + \OC::$server->getLogger()->error('Can not strip userid and "files/" from path: '.$path, ['app' => 'files_sharing']); |
|
200 | 200 | throw new \OCA\Files_Sharing\Exceptions\BrokenPath('Path does not start with /user/files', 10); |
201 | 201 | } |
202 | 202 | |
@@ -204,7 +204,7 @@ discard block |
||
204 | 204 | $sliced = array_slice($split, 2); |
205 | 205 | $relPath = implode('/', $sliced); |
206 | 206 | |
207 | - return '/' . $relPath; |
|
207 | + return '/'.$relPath; |
|
208 | 208 | } |
209 | 209 | |
210 | 210 | /** |
@@ -224,7 +224,7 @@ discard block |
||
224 | 224 | $this->setMountPoint($target); |
225 | 225 | $this->storage->setMountPoint($relTargetPath); |
226 | 226 | } catch (\Exception $e) { |
227 | - \OC::$server->getLogger()->logException($e, ['app' => 'files_sharing', 'message' => 'Could not rename mount point for shared folder "' . $this->getMountPoint() . '" to "' . $target . '"']); |
|
227 | + \OC::$server->getLogger()->logException($e, ['app' => 'files_sharing', 'message' => 'Could not rename mount point for shared folder "'.$this->getMountPoint().'" to "'.$target.'"']); |
|
228 | 228 | } |
229 | 229 | |
230 | 230 | return $result; |
@@ -278,7 +278,7 @@ discard block |
||
278 | 278 | $row = $result->fetch(); |
279 | 279 | $result->closeCursor(); |
280 | 280 | if ($row) { |
281 | - return (int)$row['storage']; |
|
281 | + return (int) $row['storage']; |
|
282 | 282 | } |
283 | 283 | return -1; |
284 | 284 | } |
@@ -42,230 +42,230 @@ |
||
42 | 42 | use OCP\Share\IShare; |
43 | 43 | |
44 | 44 | class MountProvider implements IMountProvider { |
45 | - /** |
|
46 | - * @var \OCP\IConfig |
|
47 | - */ |
|
48 | - protected $config; |
|
49 | - |
|
50 | - /** |
|
51 | - * @var IManager |
|
52 | - */ |
|
53 | - protected $shareManager; |
|
54 | - |
|
55 | - /** |
|
56 | - * @var ILogger |
|
57 | - */ |
|
58 | - protected $logger; |
|
59 | - |
|
60 | - /** @var IEventDispatcher */ |
|
61 | - protected $eventDispatcher; |
|
62 | - |
|
63 | - /** @var ICacheFactory */ |
|
64 | - protected $cacheFactory; |
|
65 | - |
|
66 | - /** |
|
67 | - * @param \OCP\IConfig $config |
|
68 | - * @param IManager $shareManager |
|
69 | - * @param ILogger $logger |
|
70 | - */ |
|
71 | - public function __construct( |
|
72 | - IConfig $config, |
|
73 | - IManager $shareManager, |
|
74 | - ILogger $logger, |
|
75 | - IEventDispatcher $eventDispatcher, |
|
76 | - ICacheFactory $cacheFactory |
|
77 | - ) { |
|
78 | - $this->config = $config; |
|
79 | - $this->shareManager = $shareManager; |
|
80 | - $this->logger = $logger; |
|
81 | - $this->eventDispatcher = $eventDispatcher; |
|
82 | - $this->cacheFactory = $cacheFactory; |
|
83 | - } |
|
84 | - |
|
85 | - |
|
86 | - /** |
|
87 | - * Get all mountpoints applicable for the user and check for shares where we need to update the etags |
|
88 | - * |
|
89 | - * @param \OCP\IUser $user |
|
90 | - * @param \OCP\Files\Storage\IStorageFactory $loader |
|
91 | - * @return \OCP\Files\Mount\IMountPoint[] |
|
92 | - */ |
|
93 | - public function getMountsForUser(IUser $user, IStorageFactory $loader) { |
|
94 | - $shares = $this->shareManager->getSharedWith($user->getUID(), IShare::TYPE_USER, null, -1); |
|
95 | - $shares = array_merge($shares, $this->shareManager->getSharedWith($user->getUID(), IShare::TYPE_GROUP, null, -1)); |
|
96 | - $shares = array_merge($shares, $this->shareManager->getSharedWith($user->getUID(), IShare::TYPE_CIRCLE, null, -1)); |
|
97 | - $shares = array_merge($shares, $this->shareManager->getSharedWith($user->getUID(), IShare::TYPE_ROOM, null, -1)); |
|
98 | - $shares = array_merge($shares, $this->shareManager->getSharedWith($user->getUID(), IShare::TYPE_DECK, null, -1)); |
|
99 | - |
|
100 | - |
|
101 | - // filter out excluded shares and group shares that includes self |
|
102 | - $shares = array_filter($shares, function (\OCP\Share\IShare $share) use ($user) { |
|
103 | - return $share->getPermissions() > 0 && $share->getShareOwner() !== $user->getUID(); |
|
104 | - }); |
|
105 | - |
|
106 | - $superShares = $this->buildSuperShares($shares, $user); |
|
107 | - |
|
108 | - $mounts = []; |
|
109 | - $view = new View('/' . $user->getUID() . '/files'); |
|
110 | - $ownerViews = []; |
|
111 | - $sharingDisabledForUser = $this->shareManager->sharingDisabledForUser($user->getUID()); |
|
112 | - $foldersExistCache = new CappedMemoryCache(); |
|
113 | - foreach ($superShares as $share) { |
|
114 | - try { |
|
115 | - /** @var \OCP\Share\IShare $parentShare */ |
|
116 | - $parentShare = $share[0]; |
|
117 | - |
|
118 | - if ($parentShare->getStatus() !== IShare::STATUS_ACCEPTED && |
|
119 | - ($parentShare->getShareType() === IShare::TYPE_GROUP || |
|
120 | - $parentShare->getShareType() === IShare::TYPE_USERGROUP || |
|
121 | - $parentShare->getShareType() === IShare::TYPE_USER)) { |
|
122 | - continue; |
|
123 | - } |
|
124 | - |
|
125 | - $owner = $parentShare->getShareOwner(); |
|
126 | - if (!isset($ownerViews[$owner])) { |
|
127 | - $ownerViews[$owner] = new View('/' . $parentShare->getShareOwner() . '/files'); |
|
128 | - } |
|
129 | - $mount = new SharedMount( |
|
130 | - '\OCA\Files_Sharing\SharedStorage', |
|
131 | - $mounts, |
|
132 | - [ |
|
133 | - 'user' => $user->getUID(), |
|
134 | - // parent share |
|
135 | - 'superShare' => $parentShare, |
|
136 | - // children/component of the superShare |
|
137 | - 'groupedShares' => $share[1], |
|
138 | - 'ownerView' => $ownerViews[$owner], |
|
139 | - 'sharingDisabledForUser' => $sharingDisabledForUser |
|
140 | - ], |
|
141 | - $loader, |
|
142 | - $view, |
|
143 | - $foldersExistCache, |
|
144 | - $this->eventDispatcher, |
|
145 | - $user, |
|
146 | - $this->cacheFactory->createLocal('share-valid-mountpoint') |
|
147 | - ); |
|
148 | - |
|
149 | - $event = new ShareMountedEvent($mount); |
|
150 | - $this->eventDispatcher->dispatchTyped($event); |
|
151 | - |
|
152 | - $mounts[$mount->getMountPoint()] = $mount; |
|
153 | - foreach ($event->getAdditionalMounts() as $additionalMount) { |
|
154 | - $mounts[$additionalMount->getMountPoint()] = $additionalMount; |
|
155 | - } |
|
156 | - } catch (\Exception $e) { |
|
157 | - $this->logger->logException($e); |
|
158 | - $this->logger->error('Error while trying to create shared mount'); |
|
159 | - } |
|
160 | - } |
|
161 | - |
|
162 | - // array_filter removes the null values from the array |
|
163 | - return array_values(array_filter($mounts)); |
|
164 | - } |
|
165 | - |
|
166 | - /** |
|
167 | - * Groups shares by path (nodeId) and target path |
|
168 | - * |
|
169 | - * @param \OCP\Share\IShare[] $shares |
|
170 | - * @return \OCP\Share\IShare[][] array of grouped shares, each element in the |
|
171 | - * array is a group which itself is an array of shares |
|
172 | - */ |
|
173 | - private function groupShares(array $shares) { |
|
174 | - $tmp = []; |
|
175 | - |
|
176 | - foreach ($shares as $share) { |
|
177 | - if (!isset($tmp[$share->getNodeId()])) { |
|
178 | - $tmp[$share->getNodeId()] = []; |
|
179 | - } |
|
180 | - $tmp[$share->getNodeId()][] = $share; |
|
181 | - } |
|
182 | - |
|
183 | - $result = []; |
|
184 | - // sort by stime, the super share will be based on the least recent share |
|
185 | - foreach ($tmp as &$tmp2) { |
|
186 | - @usort($tmp2, function ($a, $b) { |
|
187 | - $aTime = $a->getShareTime()->getTimestamp(); |
|
188 | - $bTime = $b->getShareTime()->getTimestamp(); |
|
189 | - if ($aTime === $bTime) { |
|
190 | - return $a->getId() < $b->getId() ? -1 : 1; |
|
191 | - } |
|
192 | - return $aTime < $bTime ? -1 : 1; |
|
193 | - }); |
|
194 | - $result[] = $tmp2; |
|
195 | - } |
|
196 | - |
|
197 | - return array_values($result); |
|
198 | - } |
|
199 | - |
|
200 | - /** |
|
201 | - * Build super shares (virtual share) by grouping them by node id and target, |
|
202 | - * then for each group compute the super share and return it along with the matching |
|
203 | - * grouped shares. The most permissive permissions are used based on the permissions |
|
204 | - * of all shares within the group. |
|
205 | - * |
|
206 | - * @param \OCP\Share\IShare[] $allShares |
|
207 | - * @param \OCP\IUser $user user |
|
208 | - * @return array Tuple of [superShare, groupedShares] |
|
209 | - */ |
|
210 | - private function buildSuperShares(array $allShares, \OCP\IUser $user) { |
|
211 | - $result = []; |
|
212 | - |
|
213 | - $groupedShares = $this->groupShares($allShares); |
|
214 | - |
|
215 | - /** @var \OCP\Share\IShare[] $shares */ |
|
216 | - foreach ($groupedShares as $shares) { |
|
217 | - if (count($shares) === 0) { |
|
218 | - continue; |
|
219 | - } |
|
220 | - |
|
221 | - $superShare = $this->shareManager->newShare(); |
|
222 | - |
|
223 | - // compute super share based on first entry of the group |
|
224 | - $superShare->setId($shares[0]->getId()) |
|
225 | - ->setShareOwner($shares[0]->getShareOwner()) |
|
226 | - ->setNodeId($shares[0]->getNodeId()) |
|
227 | - ->setShareType($shares[0]->getShareType()) |
|
228 | - ->setTarget($shares[0]->getTarget()); |
|
229 | - |
|
230 | - // use most permissive permissions |
|
231 | - $permissions = 0; |
|
232 | - $status = IShare::STATUS_PENDING; |
|
233 | - foreach ($shares as $share) { |
|
234 | - $permissions |= $share->getPermissions(); |
|
235 | - $status = max($status, $share->getStatus()); |
|
236 | - |
|
237 | - if ($share->getTarget() !== $superShare->getTarget()) { |
|
238 | - // adjust target, for database consistency |
|
239 | - $share->setTarget($superShare->getTarget()); |
|
240 | - try { |
|
241 | - $this->shareManager->moveShare($share, $user->getUID()); |
|
242 | - } catch (\InvalidArgumentException $e) { |
|
243 | - // ignore as it is not important and we don't want to |
|
244 | - // block FS setup |
|
245 | - |
|
246 | - // the subsequent code anyway only uses the target of the |
|
247 | - // super share |
|
248 | - |
|
249 | - // such issue can usually happen when dealing with |
|
250 | - // null groups which usually appear with group backend |
|
251 | - // caching inconsistencies |
|
252 | - $this->logger->debug( |
|
253 | - 'Could not adjust share target for share ' . $share->getId() . ' to make it consistent: ' . $e->getMessage(), |
|
254 | - ['app' => 'files_sharing'] |
|
255 | - ); |
|
256 | - } |
|
257 | - } |
|
258 | - if (!is_null($share->getNodeCacheEntry())) { |
|
259 | - $superShare->setNodeCacheEntry($share->getNodeCacheEntry()); |
|
260 | - } |
|
261 | - } |
|
262 | - |
|
263 | - $superShare->setPermissions($permissions) |
|
264 | - ->setStatus($status); |
|
265 | - |
|
266 | - $result[] = [$superShare, $shares]; |
|
267 | - } |
|
268 | - |
|
269 | - return $result; |
|
270 | - } |
|
45 | + /** |
|
46 | + * @var \OCP\IConfig |
|
47 | + */ |
|
48 | + protected $config; |
|
49 | + |
|
50 | + /** |
|
51 | + * @var IManager |
|
52 | + */ |
|
53 | + protected $shareManager; |
|
54 | + |
|
55 | + /** |
|
56 | + * @var ILogger |
|
57 | + */ |
|
58 | + protected $logger; |
|
59 | + |
|
60 | + /** @var IEventDispatcher */ |
|
61 | + protected $eventDispatcher; |
|
62 | + |
|
63 | + /** @var ICacheFactory */ |
|
64 | + protected $cacheFactory; |
|
65 | + |
|
66 | + /** |
|
67 | + * @param \OCP\IConfig $config |
|
68 | + * @param IManager $shareManager |
|
69 | + * @param ILogger $logger |
|
70 | + */ |
|
71 | + public function __construct( |
|
72 | + IConfig $config, |
|
73 | + IManager $shareManager, |
|
74 | + ILogger $logger, |
|
75 | + IEventDispatcher $eventDispatcher, |
|
76 | + ICacheFactory $cacheFactory |
|
77 | + ) { |
|
78 | + $this->config = $config; |
|
79 | + $this->shareManager = $shareManager; |
|
80 | + $this->logger = $logger; |
|
81 | + $this->eventDispatcher = $eventDispatcher; |
|
82 | + $this->cacheFactory = $cacheFactory; |
|
83 | + } |
|
84 | + |
|
85 | + |
|
86 | + /** |
|
87 | + * Get all mountpoints applicable for the user and check for shares where we need to update the etags |
|
88 | + * |
|
89 | + * @param \OCP\IUser $user |
|
90 | + * @param \OCP\Files\Storage\IStorageFactory $loader |
|
91 | + * @return \OCP\Files\Mount\IMountPoint[] |
|
92 | + */ |
|
93 | + public function getMountsForUser(IUser $user, IStorageFactory $loader) { |
|
94 | + $shares = $this->shareManager->getSharedWith($user->getUID(), IShare::TYPE_USER, null, -1); |
|
95 | + $shares = array_merge($shares, $this->shareManager->getSharedWith($user->getUID(), IShare::TYPE_GROUP, null, -1)); |
|
96 | + $shares = array_merge($shares, $this->shareManager->getSharedWith($user->getUID(), IShare::TYPE_CIRCLE, null, -1)); |
|
97 | + $shares = array_merge($shares, $this->shareManager->getSharedWith($user->getUID(), IShare::TYPE_ROOM, null, -1)); |
|
98 | + $shares = array_merge($shares, $this->shareManager->getSharedWith($user->getUID(), IShare::TYPE_DECK, null, -1)); |
|
99 | + |
|
100 | + |
|
101 | + // filter out excluded shares and group shares that includes self |
|
102 | + $shares = array_filter($shares, function (\OCP\Share\IShare $share) use ($user) { |
|
103 | + return $share->getPermissions() > 0 && $share->getShareOwner() !== $user->getUID(); |
|
104 | + }); |
|
105 | + |
|
106 | + $superShares = $this->buildSuperShares($shares, $user); |
|
107 | + |
|
108 | + $mounts = []; |
|
109 | + $view = new View('/' . $user->getUID() . '/files'); |
|
110 | + $ownerViews = []; |
|
111 | + $sharingDisabledForUser = $this->shareManager->sharingDisabledForUser($user->getUID()); |
|
112 | + $foldersExistCache = new CappedMemoryCache(); |
|
113 | + foreach ($superShares as $share) { |
|
114 | + try { |
|
115 | + /** @var \OCP\Share\IShare $parentShare */ |
|
116 | + $parentShare = $share[0]; |
|
117 | + |
|
118 | + if ($parentShare->getStatus() !== IShare::STATUS_ACCEPTED && |
|
119 | + ($parentShare->getShareType() === IShare::TYPE_GROUP || |
|
120 | + $parentShare->getShareType() === IShare::TYPE_USERGROUP || |
|
121 | + $parentShare->getShareType() === IShare::TYPE_USER)) { |
|
122 | + continue; |
|
123 | + } |
|
124 | + |
|
125 | + $owner = $parentShare->getShareOwner(); |
|
126 | + if (!isset($ownerViews[$owner])) { |
|
127 | + $ownerViews[$owner] = new View('/' . $parentShare->getShareOwner() . '/files'); |
|
128 | + } |
|
129 | + $mount = new SharedMount( |
|
130 | + '\OCA\Files_Sharing\SharedStorage', |
|
131 | + $mounts, |
|
132 | + [ |
|
133 | + 'user' => $user->getUID(), |
|
134 | + // parent share |
|
135 | + 'superShare' => $parentShare, |
|
136 | + // children/component of the superShare |
|
137 | + 'groupedShares' => $share[1], |
|
138 | + 'ownerView' => $ownerViews[$owner], |
|
139 | + 'sharingDisabledForUser' => $sharingDisabledForUser |
|
140 | + ], |
|
141 | + $loader, |
|
142 | + $view, |
|
143 | + $foldersExistCache, |
|
144 | + $this->eventDispatcher, |
|
145 | + $user, |
|
146 | + $this->cacheFactory->createLocal('share-valid-mountpoint') |
|
147 | + ); |
|
148 | + |
|
149 | + $event = new ShareMountedEvent($mount); |
|
150 | + $this->eventDispatcher->dispatchTyped($event); |
|
151 | + |
|
152 | + $mounts[$mount->getMountPoint()] = $mount; |
|
153 | + foreach ($event->getAdditionalMounts() as $additionalMount) { |
|
154 | + $mounts[$additionalMount->getMountPoint()] = $additionalMount; |
|
155 | + } |
|
156 | + } catch (\Exception $e) { |
|
157 | + $this->logger->logException($e); |
|
158 | + $this->logger->error('Error while trying to create shared mount'); |
|
159 | + } |
|
160 | + } |
|
161 | + |
|
162 | + // array_filter removes the null values from the array |
|
163 | + return array_values(array_filter($mounts)); |
|
164 | + } |
|
165 | + |
|
166 | + /** |
|
167 | + * Groups shares by path (nodeId) and target path |
|
168 | + * |
|
169 | + * @param \OCP\Share\IShare[] $shares |
|
170 | + * @return \OCP\Share\IShare[][] array of grouped shares, each element in the |
|
171 | + * array is a group which itself is an array of shares |
|
172 | + */ |
|
173 | + private function groupShares(array $shares) { |
|
174 | + $tmp = []; |
|
175 | + |
|
176 | + foreach ($shares as $share) { |
|
177 | + if (!isset($tmp[$share->getNodeId()])) { |
|
178 | + $tmp[$share->getNodeId()] = []; |
|
179 | + } |
|
180 | + $tmp[$share->getNodeId()][] = $share; |
|
181 | + } |
|
182 | + |
|
183 | + $result = []; |
|
184 | + // sort by stime, the super share will be based on the least recent share |
|
185 | + foreach ($tmp as &$tmp2) { |
|
186 | + @usort($tmp2, function ($a, $b) { |
|
187 | + $aTime = $a->getShareTime()->getTimestamp(); |
|
188 | + $bTime = $b->getShareTime()->getTimestamp(); |
|
189 | + if ($aTime === $bTime) { |
|
190 | + return $a->getId() < $b->getId() ? -1 : 1; |
|
191 | + } |
|
192 | + return $aTime < $bTime ? -1 : 1; |
|
193 | + }); |
|
194 | + $result[] = $tmp2; |
|
195 | + } |
|
196 | + |
|
197 | + return array_values($result); |
|
198 | + } |
|
199 | + |
|
200 | + /** |
|
201 | + * Build super shares (virtual share) by grouping them by node id and target, |
|
202 | + * then for each group compute the super share and return it along with the matching |
|
203 | + * grouped shares. The most permissive permissions are used based on the permissions |
|
204 | + * of all shares within the group. |
|
205 | + * |
|
206 | + * @param \OCP\Share\IShare[] $allShares |
|
207 | + * @param \OCP\IUser $user user |
|
208 | + * @return array Tuple of [superShare, groupedShares] |
|
209 | + */ |
|
210 | + private function buildSuperShares(array $allShares, \OCP\IUser $user) { |
|
211 | + $result = []; |
|
212 | + |
|
213 | + $groupedShares = $this->groupShares($allShares); |
|
214 | + |
|
215 | + /** @var \OCP\Share\IShare[] $shares */ |
|
216 | + foreach ($groupedShares as $shares) { |
|
217 | + if (count($shares) === 0) { |
|
218 | + continue; |
|
219 | + } |
|
220 | + |
|
221 | + $superShare = $this->shareManager->newShare(); |
|
222 | + |
|
223 | + // compute super share based on first entry of the group |
|
224 | + $superShare->setId($shares[0]->getId()) |
|
225 | + ->setShareOwner($shares[0]->getShareOwner()) |
|
226 | + ->setNodeId($shares[0]->getNodeId()) |
|
227 | + ->setShareType($shares[0]->getShareType()) |
|
228 | + ->setTarget($shares[0]->getTarget()); |
|
229 | + |
|
230 | + // use most permissive permissions |
|
231 | + $permissions = 0; |
|
232 | + $status = IShare::STATUS_PENDING; |
|
233 | + foreach ($shares as $share) { |
|
234 | + $permissions |= $share->getPermissions(); |
|
235 | + $status = max($status, $share->getStatus()); |
|
236 | + |
|
237 | + if ($share->getTarget() !== $superShare->getTarget()) { |
|
238 | + // adjust target, for database consistency |
|
239 | + $share->setTarget($superShare->getTarget()); |
|
240 | + try { |
|
241 | + $this->shareManager->moveShare($share, $user->getUID()); |
|
242 | + } catch (\InvalidArgumentException $e) { |
|
243 | + // ignore as it is not important and we don't want to |
|
244 | + // block FS setup |
|
245 | + |
|
246 | + // the subsequent code anyway only uses the target of the |
|
247 | + // super share |
|
248 | + |
|
249 | + // such issue can usually happen when dealing with |
|
250 | + // null groups which usually appear with group backend |
|
251 | + // caching inconsistencies |
|
252 | + $this->logger->debug( |
|
253 | + 'Could not adjust share target for share ' . $share->getId() . ' to make it consistent: ' . $e->getMessage(), |
|
254 | + ['app' => 'files_sharing'] |
|
255 | + ); |
|
256 | + } |
|
257 | + } |
|
258 | + if (!is_null($share->getNodeCacheEntry())) { |
|
259 | + $superShare->setNodeCacheEntry($share->getNodeCacheEntry()); |
|
260 | + } |
|
261 | + } |
|
262 | + |
|
263 | + $superShare->setPermissions($permissions) |
|
264 | + ->setStatus($status); |
|
265 | + |
|
266 | + $result[] = [$superShare, $shares]; |
|
267 | + } |
|
268 | + |
|
269 | + return $result; |
|
270 | + } |
|
271 | 271 | } |