|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
/** |
|
5
|
|
|
* @copyright Copyright (c) 2023 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 OCA\Files\Command\Object; |
|
25
|
|
|
|
|
26
|
|
|
use OCP\DB\QueryBuilder\IQueryBuilder; |
|
27
|
|
|
use OCP\Files\ObjectStore\IObjectStore; |
|
28
|
|
|
use OCP\IConfig; |
|
29
|
|
|
use OCP\IDBConnection; |
|
30
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
31
|
|
|
|
|
32
|
|
|
class ObjectUtil { |
|
33
|
|
|
private IConfig $config; |
|
34
|
|
|
private IDBConnection $connection; |
|
35
|
|
|
|
|
36
|
|
|
public function __construct(IConfig $config, IDBConnection $connection) { |
|
37
|
|
|
$this->config = $config; |
|
38
|
|
|
$this->connection = $connection; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
private function getObjectStoreConfig(): ?array { |
|
42
|
|
|
$config = $this->config->getSystemValue('objectstore_multibucket'); |
|
43
|
|
|
if (is_array($config)) { |
|
44
|
|
|
$config['multibucket'] = true; |
|
45
|
|
|
return $config; |
|
46
|
|
|
} |
|
47
|
|
|
$config = $this->config->getSystemValue('objectstore'); |
|
48
|
|
|
if (is_array($config)) { |
|
49
|
|
|
if (!isset($config['multibucket'])) { |
|
50
|
|
|
$config['multibucket'] = false; |
|
51
|
|
|
} |
|
52
|
|
|
return $config; |
|
53
|
|
|
} else { |
|
54
|
|
|
return null; |
|
55
|
|
|
} |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
public function getObjectStore(?string $bucket, OutputInterface $output): ?IObjectStore { |
|
59
|
|
|
$config = $this->getObjectStoreConfig(); |
|
60
|
|
|
if (!$config) { |
|
61
|
|
|
$output->writeln("<error>Instance is not using primary object store</error>"); |
|
62
|
|
|
return null; |
|
63
|
|
|
} |
|
64
|
|
|
if ($config['multibucket'] && !$bucket) { |
|
65
|
|
|
$output->writeln("<error>--bucket option required</error> because <info>multi bucket</info> is enabled."); |
|
66
|
|
|
return null; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
if (!isset($config['arguments'])) { |
|
70
|
|
|
throw new \Exception("no arguments configured for object store configuration"); |
|
71
|
|
|
} |
|
72
|
|
|
if (!isset($config['class'])) { |
|
73
|
|
|
throw new \Exception("no class configured for object store configuration"); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
if ($bucket) { |
|
77
|
|
|
// s3, swift |
|
78
|
|
|
$config['arguments']['bucket'] = $bucket; |
|
79
|
|
|
// azure |
|
80
|
|
|
$config['arguments']['container'] = $bucket; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
$store = new $config['class']($config['arguments']); |
|
84
|
|
|
if (!$store instanceof IObjectStore) { |
|
85
|
|
|
throw new \Exception("configured object store class is not an object store implementation"); |
|
86
|
|
|
} |
|
87
|
|
|
return $store; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* Check if an object is referenced in the database |
|
92
|
|
|
*/ |
|
93
|
|
|
public function objectExistsInDb(string $object): int|false { |
|
94
|
|
|
if (str_starts_with($object, 'urn:oid:')) { |
|
95
|
|
|
$fileId = (int)substr($object, strlen('urn:oid:')); |
|
96
|
|
|
$query = $this->connection->getQueryBuilder(); |
|
97
|
|
|
$query->select('fileid') |
|
98
|
|
|
->from('filecache') |
|
99
|
|
|
->where($query->expr()->eq('fileid', $query->createNamedParameter($fileId, IQueryBuilder::PARAM_INT))); |
|
100
|
|
|
$result = $query->executeQuery(); |
|
101
|
|
|
if ($result->fetchOne() !== false) { |
|
102
|
|
|
return $fileId; |
|
103
|
|
|
} else { |
|
104
|
|
|
return false; |
|
105
|
|
|
} |
|
106
|
|
|
} else { |
|
107
|
|
|
return false; |
|
108
|
|
|
} |
|
109
|
|
|
} |
|
110
|
|
|
} |
|
111
|
|
|
|