ConfigAdapter   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 135
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 68
dl 0
loc 135
rs 10
c 0
b 0
f 0
wmc 12

4 Methods

Rating   Name   Duplication   Size   Complexity  
A prepareStorageConfig() 0 16 4
B getMountsForUser() 0 66 6
A __construct() 0 6 1
A constructStorage() 0 9 1
1
<?php
2
/**
3
 * @copyright Copyright (c) 2016, ownCloud, Inc.
4
 *
5
 * @author Arthur Schiwon <[email protected]>
6
 * @author Christoph Wurst <[email protected]>
7
 * @author Joas Schilling <[email protected]>
8
 * @author Julius Härtl <[email protected]>
9
 * @author Morris Jobke <[email protected]>
10
 * @author Robin Appelman <[email protected]>
11
 * @author Robin McCorkell <[email protected]>
12
 * @author Roeland Jago Douma <[email protected]>
13
 * @author Vincent Petry <[email protected]>
14
 *
15
 * @license AGPL-3.0
16
 *
17
 * This code is free software: you can redistribute it and/or modify
18
 * it under the terms of the GNU Affero General Public License, version 3,
19
 * as published by the Free Software Foundation.
20
 *
21
 * This program is distributed in the hope that it will be useful,
22
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24
 * GNU Affero General Public License for more details.
25
 *
26
 * You should have received a copy of the GNU Affero General Public License, version 3,
27
 * along with this program. If not, see <http://www.gnu.org/licenses/>
28
 *
29
 */
30
namespace OCA\Files_External\Config;
31
32
use OC\Files\Storage\FailedStorage;
33
use OC\Files\Storage\Wrapper\Availability;
34
use OCA\Files_External\Lib\PersonalMount;
35
use OCA\Files_External\Lib\StorageConfig;
36
use OCA\Files_External\Service\UserGlobalStoragesService;
37
use OCA\Files_External\Service\UserStoragesService;
38
use OCP\Files\Config\IMountProvider;
39
use OCP\Files\Storage;
40
use OCP\Files\Storage\IStorageFactory;
41
use OCP\Files\StorageNotAvailableException;
42
use OCP\IUser;
43
44
/**
45
 * Make the old files_external config work with the new public mount config api
46
 */
47
class ConfigAdapter implements IMountProvider {
48
49
	/** @var UserStoragesService */
50
	private $userStoragesService;
51
52
	/** @var UserGlobalStoragesService */
53
	private $userGlobalStoragesService;
54
55
	/**
56
	 * @param UserStoragesService $userStoragesService
57
	 * @param UserGlobalStoragesService $userGlobalStoragesService
58
	 */
59
	public function __construct(
60
		UserStoragesService $userStoragesService,
61
		UserGlobalStoragesService $userGlobalStoragesService
62
	) {
63
		$this->userStoragesService = $userStoragesService;
64
		$this->userGlobalStoragesService = $userGlobalStoragesService;
65
	}
66
67
	/**
68
	 * Process storage ready for mounting
69
	 *
70
	 * @param StorageConfig $storage
71
	 * @param IUser $user
72
	 * @throws \OCP\AppFramework\QueryException
73
	 */
74
	private function prepareStorageConfig(StorageConfig &$storage, IUser $user) {
75
		foreach ($storage->getBackendOptions() as $option => $value) {
76
			$storage->setBackendOption($option, \OCA\Files_External\MountConfig::substitutePlaceholdersInConfig($value, $user->getUID()));
77
		}
78
79
		$objectStore = $storage->getBackendOption('objectstore');
80
		if ($objectStore) {
81
			$objectClass = $objectStore['class'];
82
			if (!is_subclass_of($objectClass, '\OCP\Files\ObjectStore\IObjectStore')) {
83
				throw new \InvalidArgumentException('Invalid object store');
84
			}
85
			$storage->setBackendOption('objectstore', new $objectClass($objectStore));
86
		}
87
88
		$storage->getAuthMechanism()->manipulateStorageConfig($storage, $user);
89
		$storage->getBackend()->manipulateStorageConfig($storage, $user);
90
	}
91
92
	/**
93
	 * Construct the storage implementation
94
	 *
95
	 * @param StorageConfig $storageConfig
96
	 * @return Storage
97
	 */
98
	private function constructStorage(StorageConfig $storageConfig) {
99
		$class = $storageConfig->getBackend()->getStorageClass();
100
		$storage = new $class($storageConfig->getBackendOptions());
101
102
		// auth mechanism should fire first
103
		$storage = $storageConfig->getBackend()->wrapStorage($storage);
104
		$storage = $storageConfig->getAuthMechanism()->wrapStorage($storage);
105
106
		return $storage;
107
	}
108
109
	/**
110
	 * Get all mountpoints applicable for the user
111
	 *
112
	 * @param \OCP\IUser $user
113
	 * @param \OCP\Files\Storage\IStorageFactory $loader
114
	 * @return \OCP\Files\Mount\IMountPoint[]
115
	 */
116
	public function getMountsForUser(IUser $user, IStorageFactory $loader) {
117
		$this->userStoragesService->setUser($user);
118
		$this->userGlobalStoragesService->setUser($user);
119
120
		$storageConfigs = $this->userGlobalStoragesService->getAllStoragesForUser();
121
122
		$storages = array_map(function (StorageConfig $storageConfig) use ($user) {
123
			try {
124
				$this->prepareStorageConfig($storageConfig, $user);
125
				return $this->constructStorage($storageConfig);
126
			} catch (\Exception $e) {
127
				// propagate exception into filesystem
128
				return new FailedStorage(['exception' => $e]);
129
			}
130
		}, $storageConfigs);
131
132
133
		\OC\Files\Cache\Storage::getGlobalCache()->loadForStorageIds(array_map(function (Storage\IStorage $storage) {
134
			return $storage->getId();
135
		}, $storages));
136
137
		$availableStorages = array_map(function (Storage\IStorage $storage, StorageConfig $storageConfig) {
138
			try {
139
				$availability = $storage->getAvailability();
140
				if (!$availability['available'] && !Availability::shouldRecheck($availability)) {
141
					$storage = new FailedStorage([
142
						'exception' => new StorageNotAvailableException('Storage with mount id ' . $storageConfig->getId() . ' is not available')
143
					]);
144
				}
145
			} catch (\Exception $e) {
146
				// propagate exception into filesystem
147
				$storage = new FailedStorage(['exception' => $e]);
148
			}
149
			return $storage;
150
		}, $storages, $storageConfigs);
151
152
		$mounts = array_map(function (StorageConfig $storageConfig, Storage\IStorage $storage) use ($user, $loader) {
153
			if ($storageConfig->getType() === StorageConfig::MOUNT_TYPE_PERSONAl) {
154
				return new PersonalMount(
155
					$this->userStoragesService,
156
					$storageConfig,
157
					$storageConfig->getId(),
158
					$storage,
159
					'/' . $user->getUID() . '/files' . $storageConfig->getMountPoint(),
160
					null,
161
					$loader,
162
					$storageConfig->getMountOptions(),
163
					$storageConfig->getId()
164
				);
165
			} else {
166
				return new SystemMountPoint(
167
					$storageConfig,
168
					$storage,
169
					'/' . $user->getUID() . '/files' . $storageConfig->getMountPoint(),
170
					null,
171
					$loader,
172
					$storageConfig->getMountOptions(),
173
					$storageConfig->getId()
174
				);
175
			}
176
		}, $storageConfigs, $availableStorages);
177
178
		$this->userStoragesService->resetUser();
179
		$this->userGlobalStoragesService->resetUser();
180
181
		return $mounts;
182
	}
183
}
184