Passed
Push — master ( d7aa58...61aa09 )
by Blizzz
15:49 queued 11s
created

LazyUserFolder::getMountPoint()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 6
nc 3
nop 0
dl 0
loc 9
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
/**
5
 * @copyright Copyright (c) 2022 Robin Appelman <[email protected]>
6
 *
7
 * @license GNU AGPL version 3 or any later version
8
 *
9
 * This program is free software: you can redistribute it and/or modify
10
 * it under the terms of the GNU Affero General Public License as
11
 * published by the Free Software Foundation, either version 3 of the
12
 * License, or (at your option) any later version.
13
 *
14
 * This program is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 * GNU Affero General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU Affero General Public License
20
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21
 *
22
 */
23
24
namespace OC\Files\Node;
25
26
use OCP\Files\FileInfo;
27
use OCP\Constants;
28
use OCP\Files\IRootFolder;
29
use OCP\Files\Mount\IMountManager;
30
use OCP\Files\NotFoundException;
31
use OCP\IUser;
32
33
class LazyUserFolder extends LazyFolder {
34
	private IRootFolder $root;
35
	private IUser $user;
36
	private string $path;
37
	private IMountManager $mountManager;
38
39
	public function __construct(IRootFolder $rootFolder, IUser $user, IMountManager $mountManager) {
40
		$this->root = $rootFolder;
41
		$this->user = $user;
42
		$this->mountManager = $mountManager;
43
		$this->path = '/' . $user->getUID() . '/files';
44
		parent::__construct(function () use ($user) {
45
			try {
46
				return $this->root->get('/' . $user->getUID() . '/files');
47
			} catch (NotFoundException $e) {
48
				if (!$this->root->nodeExists('/' . $user->getUID())) {
49
					$this->root->newFolder('/' . $user->getUID());
50
				}
51
				return $this->root->newFolder('/' . $user->getUID() . '/files');
52
			}
53
		}, [
54
			'path' => $this->path,
55
			'permissions' => Constants::PERMISSION_ALL,
56
			'type' => FileInfo::TYPE_FOLDER,
57
			'mimetype' => FileInfo::MIMETYPE_FOLDER,
58
		]);
59
	}
60
61
	public function get($path) {
62
		return $this->root->get('/' . $this->user->getUID() . '/files/' . ltrim($path, '/'));
63
	}
64
65
	/**
66
	 * @param int $id
67
	 * @return \OCP\Files\Node[]
68
	 */
69
	public function getById($id) {
70
		return $this->root->getByIdInPath((int)$id, $this->getPath());
71
	}
72
73
	public function getMountPoint() {
74
		if ($this->folder !== null) {
75
			return $this->folder->getMountPoint();
76
		}
77
		$mountPoint = $this->mountManager->find('/' . $this->user->getUID());
78
		if (is_null($mountPoint)) {
79
			throw new \Exception("No mountpoint for user folder");
80
		}
81
		return $mountPoint;
82
	}
83
}
84