Completed
Push — stable13 ( 9c0a9a...065ec7 )
by Morris
19:50 queued 05:17
created

SharedMount   A

Complexity

Total Complexity 23

Size/Duplication

Total Lines 226
Duplicated Lines 0 %

Coupling/Cohesion

Components 3
Dependencies 17

Importance

Changes 0
Metric Value
dl 0
loc 226
rs 10
c 0
b 0
f 0
wmc 23
lcom 3
cbo 17

11 Methods

Rating   Name   Duplication   Size   Complexity  
A stripUserFilesPath() 0 18 3
A moveMount() 0 19 2
A removeMount() 0 9 1
A getShare() 0 3 1
A getStorageRootId() 0 3 1
A getNumericStorageId() 0 19 3
A getMountType() 0 3 1
A __construct() 0 11 1
A verifyMountPoint() 0 27 4
A updateFileTarget() 0 8 2
A generateUniqueTarget() 0 16 4
1
<?php
2
/**
3
 * @copyright Copyright (c) 2016, ownCloud, Inc.
4
 *
5
 * @author Björn Schießle <[email protected]>
6
 * @author Frédéric Fortier <[email protected]>
7
 * @author Joas Schilling <[email protected]>
8
 * @author Morris Jobke <[email protected]>
9
 * @author Robin Appelman <[email protected]>
10
 * @author Roeland Jago Douma <[email protected]>
11
 * @author Vincent Petry <[email protected]>
12
 *
13
 * @license AGPL-3.0
14
 *
15
 * This code is free software: you can redistribute it and/or modify
16
 * it under the terms of the GNU Affero General Public License, version 3,
17
 * as published by the Free Software Foundation.
18
 *
19
 * This program is distributed in the hope that it will be useful,
20
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22
 * GNU Affero General Public License for more details.
23
 *
24
 * You should have received a copy of the GNU Affero General Public License, version 3,
25
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
26
 *
27
 */
28
29
namespace OCA\Files_Sharing;
30
31
use OC\Cache\CappedMemoryCache;
32
use OC\Files\Filesystem;
33
use OC\Files\Mount\MountPoint;
34
use OC\Files\Mount\MoveableMount;
35
use OC\Files\View;
36
use OCP\Files\Storage\IStorageFactory;
37
38
/**
39
 * Shared mount points can be moved by the user
40
 */
41
class SharedMount extends MountPoint implements MoveableMount {
42
	/**
43
	 * @var \OCA\Files_Sharing\SharedStorage $storage
44
	 */
45
	protected $storage = null;
46
47
	/**
48
	 * @var \OC\Files\View
49
	 */
50
	private $recipientView;
51
52
	/**
53
	 * @var string
54
	 */
55
	private $user;
56
57
	/** @var \OCP\Share\IShare */
58
	private $superShare;
59
60
	/** @var \OCP\Share\IShare[] */
61
	private $groupedShares;
62
63
	/**
64
	 * @param string $storage
65
	 * @param SharedMount[] $mountpoints
66
	 * @param array $arguments
67
	 * @param IStorageFactory $loader
68
	 * @param View $recipientView
69
	 */
70
	public function __construct($storage, array $mountpoints, $arguments, IStorageFactory $loader, View $recipientView, CappedMemoryCache $folderExistCache) {
71
		$this->user = $arguments['user'];
72
		$this->recipientView = $recipientView;
73
74
		$this->superShare = $arguments['superShare'];
75
		$this->groupedShares = $arguments['groupedShares'];
76
77
		$newMountPoint = $this->verifyMountPoint($this->superShare, $mountpoints, $folderExistCache);
78
		$absMountPoint = '/' . $this->user . '/files' . $newMountPoint;
79
		parent::__construct($storage, $absMountPoint, $arguments, $loader);
80
	}
81
82
	/**
83
	 * check if the parent folder exists otherwise move the mount point up
84
	 *
85
	 * @param \OCP\Share\IShare $share
86
	 * @param SharedMount[] $mountpoints
87
	 * @return string
88
	 */
89
	private function verifyMountPoint(\OCP\Share\IShare $share, array $mountpoints, CappedMemoryCache $folderExistCache) {
90
91
		$mountPoint = basename($share->getTarget());
92
		$parent = dirname($share->getTarget());
93
94
		if ($folderExistCache->hasKey($parent)) {
95
			$parentExists = $folderExistCache->get($parent);
96
		} else {
97
			$parentExists = $this->recipientView->is_dir($parent);
98
			$folderExistCache->set($parent, $parentExists);
99
		}
100
		if (!$parentExists) {
101
			$parent = Helper::getShareFolder($this->recipientView);
102
		}
103
104
		$newMountPoint = $this->generateUniqueTarget(
105
			\OC\Files\Filesystem::normalizePath($parent . '/' . $mountPoint),
106
			$this->recipientView,
107
			$mountpoints
108
		);
109
110
		if ($newMountPoint !== $share->getTarget()) {
111
			$this->updateFileTarget($newMountPoint, $share);
112
		}
113
114
		return $newMountPoint;
115
	}
116
117
	/**
118
	 * update fileTarget in the database if the mount point changed
119
	 *
120
	 * @param string $newPath
121
	 * @param \OCP\Share\IShare $share
122
	 * @return bool
123
	 */
124
	private function updateFileTarget($newPath, &$share) {
125
		$share->setTarget($newPath);
126
127
		foreach ($this->groupedShares as $tmpShare) {
128
			$tmpShare->setTarget($newPath);
129
			\OC::$server->getShareManager()->moveShare($tmpShare, $this->user);
130
		}
131
	}
132
133
134
	/**
135
	 * @param string $path
136
	 * @param View $view
137
	 * @param SharedMount[] $mountpoints
138
	 * @return mixed
139
	 */
140
	private function generateUniqueTarget($path, $view, array $mountpoints) {
141
		$pathinfo = pathinfo($path);
142
		$ext = (isset($pathinfo['extension'])) ? '.' . $pathinfo['extension'] : '';
143
		$name = $pathinfo['filename'];
144
		$dir = $pathinfo['dirname'];
145
146
		$i = 2;
147
		$absolutePath = $this->recipientView->getAbsolutePath($path) . '/';
148
		while ($view->file_exists($path) || isset($mountpoints[$absolutePath])) {
149
			$path = Filesystem::normalizePath($dir . '/' . $name . ' (' . $i . ')' . $ext);
150
			$absolutePath = $this->recipientView->getAbsolutePath($path) . '/';
151
			$i++;
152
		}
153
154
		return $path;
155
	}
156
157
	/**
158
	 * Format a path to be relative to the /user/files/ directory
159
	 *
160
	 * @param string $path the absolute path
161
	 * @return string e.g. turns '/admin/files/test.txt' into '/test.txt'
162
	 * @throws \OCA\Files_Sharing\Exceptions\BrokenPath
163
	 */
164
	protected function stripUserFilesPath($path) {
165
		$trimmed = ltrim($path, '/');
166
		$split = explode('/', $trimmed);
167
168
		// it is not a file relative to data/user/files
169
		if (count($split) < 3 || $split[1] !== 'files') {
170
			\OCP\Util::writeLog('file sharing',
171
				'Can not strip userid and "files/" from path: ' . $path,
172
				\OCP\Util::ERROR);
173
			throw new \OCA\Files_Sharing\Exceptions\BrokenPath('Path does not start with /user/files', 10);
174
		}
175
176
		// skip 'user' and 'files'
177
		$sliced = array_slice($split, 2);
178
		$relPath = implode('/', $sliced);
179
180
		return '/' . $relPath;
181
	}
182
183
	/**
184
	 * Move the mount point to $target
185
	 *
186
	 * @param string $target the target mount point
187
	 * @return bool
188
	 */
189
	public function moveMount($target) {
190
191
		$relTargetPath = $this->stripUserFilesPath($target);
192
		$share = $this->storage->getShare();
193
194
		$result = true;
195
196
		try {
197
			$this->updateFileTarget($relTargetPath, $share);
198
			$this->setMountPoint($target);
199
			$this->storage->setMountPoint($relTargetPath);
200
		} catch (\Exception $e) {
201
			\OCP\Util::writeLog('file sharing',
202
				'Could not rename mount point for shared folder "' . $this->getMountPoint() . '" to "' . $target . '"',
203
				\OCP\Util::ERROR);
204
		}
205
206
		return $result;
207
	}
208
209
	/**
210
	 * Remove the mount points
211
	 *
212
	 * @return bool
213
	 */
214
	public function removeMount() {
215
		$mountManager = \OC\Files\Filesystem::getMountManager();
216
		/** @var $storage \OCA\Files_Sharing\SharedStorage */
217
		$storage = $this->getStorage();
218
		$result = $storage->unshareStorage();
219
		$mountManager->removeMount($this->mountPoint);
220
221
		return $result;
222
	}
223
224
	/**
225
	 * @return \OCP\Share\IShare
226
	 */
227
	public function getShare() {
228
		return $this->superShare;
229
	}
230
231
	/**
232
	 * Get the file id of the root of the storage
233
	 *
234
	 * @return int
235
	 */
236
	public function getStorageRootId() {
237
		return $this->getShare()->getNodeId();
238
	}
239
240
	/**
241
	 * @return int
242
	 */
243
	public function getNumericStorageId() {
244
		if (!is_null($this->getShare()->getNodeCacheEntry())) {
245
			return $this->getShare()->getNodeCacheEntry()->getStorageId();
246
		} else {
247
			$builder = \OC::$server->getDatabaseConnection()->getQueryBuilder();
248
249
			$query = $builder->select('storage')
250
				->from('filecache')
251
				->where($builder->expr()->eq('fileid', $builder->createNamedParameter($this->getStorageRootId())));
252
253
			$result = $query->execute();
254
			$row = $result->fetch();
255
			$result->closeCursor();
256
			if ($row) {
257
				return (int)$row['storage'];
258
			}
259
			return -1;
260
		}
261
	}
262
263
	public function getMountType() {
264
		return 'shared';
265
	}
266
}
267