Completed
Push — master ( b4cf55...c1feae )
by Lukas
32:26 queued 23:46
created

getSingleBucketObjectStoreConfig()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 19
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 11
nc 5
nop 1
dl 0
loc 19
rs 9.2
c 0
b 0
f 0
1
<?php
2
/**
3
 * @copyright Copyright (c) 2016, ownCloud, Inc.
4
 *
5
 * @author Robin Appelman <[email protected]>
6
 * @author Roeland Jago Douma <[email protected]>
7
 *
8
 * @license AGPL-3.0
9
 *
10
 * This code is free software: you can redistribute it and/or modify
11
 * it under the terms of the GNU Affero General Public License, version 3,
12
 * as published by the Free Software Foundation.
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, version 3,
20
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
21
 *
22
 */
23
24
namespace OC\Files\Mount;
25
26
use OCP\Files\Config\IHomeMountProvider;
27
use OCP\Files\Storage\IStorageFactory;
28
use OCP\IConfig;
29
use OCP\IUser;
30
31
/**
32
 * Mount provider for object store home storages
33
 */
34
class ObjectHomeMountProvider implements IHomeMountProvider {
35
	/**
36
	 * @var IConfig
37
	 */
38
	private $config;
39
40
	/**
41
	 * ObjectStoreHomeMountProvider constructor.
42
	 *
43
	 * @param IConfig $config
44
	 */
45
	public function __construct(IConfig $config) {
46
		$this->config = $config;
47
	}
48
49
	/**
50
	 * Get the cache mount for a user
51
	 *
52
	 * @param IUser $user
53
	 * @param IStorageFactory $loader
54
	 * @return \OCP\Files\Mount\IMountPoint[]
55
	 */
56
	public function getHomeMountForUser(IUser $user, IStorageFactory $loader) {
57
58
		$config = $this->getMultiBucketObjectStoreConfig($user);
59
		if ($config === null) {
60
			$config = $this->getSingleBucketObjectStoreConfig($user);
61
		}
62
63
		if ($config === null) {
64
			return null;
65
		}
66
67
		return new MountPoint('\OC\Files\ObjectStore\HomeObjectStoreStorage', '/' . $user->getUID(), $config['arguments'], $loader);
68
	}
69
70
	/**
71
	 * @param IUser $user
72
	 * @return array|null
73
	 */
74
	private function getSingleBucketObjectStoreConfig(IUser $user) {
75
		$config = $this->config->getSystemValue('objectstore');
76
		if (!is_array($config)) {
77
			return null;
78
		}
79
80
		// sanity checks
81
		if (empty($config['class'])) {
82
			\OCP\Util::writeLog('files', 'No class given for objectstore', \OCP\Util::ERROR);
83
		}
84
		if (!isset($config['arguments'])) {
85
			$config['arguments'] = [];
86
		}
87
		$config['arguments']['user'] = $user;
88
		// instantiate object store implementation
89
		$config['arguments']['objectstore'] = new $config['class']($config['arguments']);
90
91
		return $config;
92
	}
93
94
	/**
95
	 * @param IUser $user
96
	 * @return array|null
97
	 */
98
	private function getMultiBucketObjectStoreConfig(IUser $user) {
99
		$config = $this->config->getSystemValue('objectstore_multibucket');
100
		if (!is_array($config)) {
101
			return null;
102
		}
103
104
		// sanity checks
105
		if (empty($config['class'])) {
106
			\OCP\Util::writeLog('files', 'No class given for objectstore', \OCP\Util::ERROR);
107
		}
108
		if (!isset($config['arguments'])) {
109
			$config['arguments'] = [];
110
		}
111
		$config['arguments']['user'] = $user;
112
113
		$bucket = $this->config->getUserValue($user->getUID(), 'homeobjectstore', 'bucket', null);
114
115
		if ($bucket === null) {
116
			/*
117
			 * Use any provided bucket argument as prefix
118
			 * and add the mapping from username => bucket
119
			 */
120
			if (!isset($config['arguments']['bucket'])) {
121
				$config['arguments']['bucket'] = '';
122
			}
123
			$mapper = new \OC\Files\ObjectStore\Mapper($user);
124
			$numBuckets = isset($config['arguments']['num_buckets']) ? $config['arguments']['num_buckets'] : 64;
125
			$config['arguments']['bucket'] .= $mapper->getBucket($numBuckets);
126
127
			$this->config->setUserValue($user->getUID(), 'homeobjectstore', 'bucket', $config['arguments']['bucket']);
128
		} else {
129
			$config['arguments']['bucket'] = $bucket;
130
		}
131
132
		// instantiate object store implementation
133
		$config['arguments']['objectstore'] = new $config['class']($config['arguments']);
134
135
		return $config;
136
	}
137
}
138