Passed
Push — master ( 821a0d...8b22a4 )
by Robin
22:41 queued 07:27
created

RootMountProvider::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 3
rs 10
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\Mount;
25
26
use OC;
27
use OC\Files\ObjectStore\ObjectStoreStorage;
28
use OC\Files\Storage\LocalRootStorage;
29
use OC_App;
30
use OCP\Files\Config\IRootMountProvider;
31
use OCP\Files\Storage\IStorageFactory;
32
use OCP\IConfig;
33
use Psr\Log\LoggerInterface;
34
35
class RootMountProvider implements IRootMountProvider {
36
	private IConfig $config;
37
	private LoggerInterface $logger;
38
39
	public function __construct(IConfig $config, LoggerInterface $logger) {
40
		$this->config = $config;
41
		$this->logger = $logger;
42
	}
43
44
	public function getRootMounts(IStorageFactory $loader): array {
45
		$objectStore = $this->config->getSystemValue('objectstore', null);
46
		$objectStoreMultiBucket = $this->config->getSystemValue('objectstore_multibucket', null);
47
48
		if ($objectStoreMultiBucket) {
49
			return [$this->getMultiBucketStoreRootMount($loader, $objectStoreMultiBucket)];
50
		} elseif ($objectStore) {
51
			return [$this->getObjectStoreRootMount($loader, $objectStore)];
52
		} else {
53
			return [$this->getLocalRootMount($loader)];
54
		}
55
	}
56
57
	private function validateObjectStoreConfig(array &$config) {
58
		if (empty($config['class'])) {
59
			$this->logger->error('No class given for objectstore', ['app' => 'files']);
60
		}
61
		if (!isset($config['arguments'])) {
62
			$config['arguments'] = [];
63
		}
64
65
		// instantiate object store implementation
66
		$name = $config['class'];
67
		if (strpos($name, 'OCA\\') === 0 && substr_count($name, '\\') >= 2) {
68
			$segments = explode('\\', $name);
69
			OC_App::loadApp(strtolower($segments[1]));
70
		}
71
	}
72
73
	private function getLocalRootMount(IStorageFactory $loader): MountPoint {
74
		$configDataDirectory = $this->config->getSystemValue("datadirectory", OC::$SERVERROOT . "/data");
75
		return new MountPoint(LocalRootStorage::class, '/', ['datadir' => $configDataDirectory], $loader, null, null, self::class);
76
	}
77
78
	private function getObjectStoreRootMount(IStorageFactory $loader, array $config): MountPoint {
79
		$this->validateObjectStoreConfig($config);
80
81
		$config['arguments']['objectstore'] = new $config['class']($config['arguments']);
82
		// mount with plain / root object store implementation
83
		$config['class'] = ObjectStoreStorage::class;
84
85
		return new MountPoint($config['class'], '/', $config['arguments'], $loader, null, null, self::class);
86
	}
87
88
	private function getMultiBucketStoreRootMount(IStorageFactory $loader, array $config): MountPoint {
89
		$this->validateObjectStoreConfig($config);
90
91
		if (!isset($config['arguments']['bucket'])) {
92
			$config['arguments']['bucket'] = '';
93
		}
94
		// put the root FS always in first bucket for multibucket configuration
95
		$config['arguments']['bucket'] .= '0';
96
97
		$config['arguments']['objectstore'] = new $config['class']($config['arguments']);
98
		// mount with plain / root object store implementation
99
		$config['class'] = ObjectStoreStorage::class;
100
101
		return new MountPoint($config['class'], '/', $config['arguments'], $loader, null, null, self::class);
102
	}
103
}
104