Completed
Push — master ( d78a4d...d0c46a )
by Morris
29:49 queued 11:15
created

ApiController   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 72
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
dl 0
loc 72
rs 10
c 0
b 0
f 0
wmc 7
lcom 1
cbo 4

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
B formatMount() 0 31 4
A getUserMounts() 0 11 2
1
<?php
2
declare(strict_types=1);
3
/**
4
 * @copyright Copyright (c) 2016, ownCloud, Inc.
5
 *
6
 * @author Jesús Macias <[email protected]>
7
 * @author Joas Schilling <[email protected]>
8
 * @author Morris Jobke <[email protected]>
9
 * @author Robin Appelman <[email protected]>
10
 * @author Robin McCorkell <[email protected]>
11
 * @author Roeland Jago Douma <[email protected]>
12
 * @author Vincent Petry <[email protected]>
13
 *
14
 * @license AGPL-3.0
15
 *
16
 * This code is free software: you can redistribute it and/or modify
17
 * it under the terms of the GNU Affero General Public License, version 3,
18
 * as published by the Free Software Foundation.
19
 *
20
 * This program is distributed in the hope that it will be useful,
21
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23
 * GNU Affero General Public License for more details.
24
 *
25
 * You should have received a copy of the GNU Affero General Public License, version 3,
26
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
27
 *
28
 */
29
30
namespace OCA\Files_External\Controller;
31
32
use OCP\AppFramework\Http\DataResponse;
33
use OCP\AppFramework\OCSController;
34
use OCP\IRequest;
35
use OCP\IUserSession;
36
37
class ApiController extends OCSController {
38
39
	/** @var IUserSession */
40
	private $userSession;
41
42
	public function __construct(string $appName,
43
								IRequest $request,
44
								IUserSession $userSession) {
45
		parent::__construct($appName, $request);
46
47
		$this->userSession = $userSession;
48
	}
49
50
	/**
51
	 * Formats the given mount config to a mount entry.
52
	 *
53
	 * @param string $mountPoint mount point name, relative to the data dir
54
	 * @param array $mountConfig mount config to format
55
	 *
56
	 * @return array entry
57
	 */
58
	private function formatMount(string $mountPoint, array $mountConfig): array {
59
		// strip "/$user/files" from mount point
60
		$mountPoint = explode('/', trim($mountPoint, '/'), 3);
61
		$mountPoint = $mountPoint[2] ?? '';
62
63
		// split path from mount point
64
		$path = \dirname($mountPoint);
65
		if ($path === '.') {
66
			$path = '';
67
		}
68
69
		$isSystemMount = !$mountConfig['personal'];
70
71
		$permissions = \OCP\Constants::PERMISSION_READ;
72
		// personal mounts can be deleted
73
		if (!$isSystemMount) {
74
			$permissions |= \OCP\Constants::PERMISSION_DELETE;
75
		}
76
77
		$entry = array(
78
			'name' => basename($mountPoint),
79
			'path' => $path,
80
			'type' => 'dir',
81
			'backend' => $mountConfig['backend'],
82
			'scope' => $isSystemMount ? 'system' : 'personal',
83
			'permissions' => $permissions,
84
			'id' => $mountConfig['id'],
85
			'class' => $mountConfig['class']
86
		);
87
		return $entry;
88
	}
89
90
	/**
91
	 * @NoAdminRequired
92
	 *
93
	 * Returns the mount points visible for this user.
94
	 *
95
	 * @return DataResponse share information
96
	 */
97
	public function getUserMounts(): DataResponse {
98
		$entries = [];
99
		$user = $this->userSession->getUser()->getUID();
100
101
		$mounts = \OC_Mount_Config::getAbsoluteMountPoints($user);
102
		foreach($mounts as $mountPoint => $mount) {
103
			$entries[] = $this->formatMount($mountPoint, $mount);
104
		}
105
106
		return new DataResponse($entries);
107
	}
108
}
109