ObjectUtil.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2023 Robin Appelman <robin@icewind.nl>
  5. *
  6. * @license GNU AGPL version 3 or any later version
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU Affero General Public License as
  10. * published by the Free Software Foundation, either version 3 of the
  11. * License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU Affero General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. *
  21. */
  22. namespace OCA\Files\Command\Object;
  23. use OCP\DB\QueryBuilder\IQueryBuilder;
  24. use OCP\Files\ObjectStore\IObjectStore;
  25. use OCP\IConfig;
  26. use OCP\IDBConnection;
  27. use Symfony\Component\Console\Output\OutputInterface;
  28. class ObjectUtil {
  29. public function __construct(
  30. private IConfig $config,
  31. private IDBConnection $connection,
  32. ) {
  33. }
  34. private function getObjectStoreConfig(): ?array {
  35. $config = $this->config->getSystemValue('objectstore_multibucket');
  36. if (is_array($config)) {
  37. $config['multibucket'] = true;
  38. return $config;
  39. }
  40. $config = $this->config->getSystemValue('objectstore');
  41. if (is_array($config)) {
  42. if (!isset($config['multibucket'])) {
  43. $config['multibucket'] = false;
  44. }
  45. return $config;
  46. }
  47. return null;
  48. }
  49. public function getObjectStore(?string $bucket, OutputInterface $output): ?IObjectStore {
  50. $config = $this->getObjectStoreConfig();
  51. if (!$config) {
  52. $output->writeln("<error>Instance is not using primary object store</error>");
  53. return null;
  54. }
  55. if ($config['multibucket'] && !$bucket) {
  56. $output->writeln("<error>--bucket option required</error> because <info>multi bucket</info> is enabled.");
  57. return null;
  58. }
  59. if (!isset($config['arguments'])) {
  60. throw new \Exception("no arguments configured for object store configuration");
  61. }
  62. if (!isset($config['class'])) {
  63. throw new \Exception("no class configured for object store configuration");
  64. }
  65. if ($bucket) {
  66. // s3, swift
  67. $config['arguments']['bucket'] = $bucket;
  68. // azure
  69. $config['arguments']['container'] = $bucket;
  70. }
  71. $store = new $config['class']($config['arguments']);
  72. if (!$store instanceof IObjectStore) {
  73. throw new \Exception("configured object store class is not an object store implementation");
  74. }
  75. return $store;
  76. }
  77. /**
  78. * Check if an object is referenced in the database
  79. */
  80. public function objectExistsInDb(string $object): int|false {
  81. if (!str_starts_with($object, 'urn:oid:')) {
  82. return false;
  83. }
  84. $fileId = (int)substr($object, strlen('urn:oid:'));
  85. $query = $this->connection->getQueryBuilder();
  86. $query->select('fileid')
  87. ->from('filecache')
  88. ->where($query->expr()->eq('fileid', $query->createNamedParameter($fileId, IQueryBuilder::PARAM_INT)));
  89. $result = $query->executeQuery();
  90. if ($result->fetchOne() === false) {
  91. return false;
  92. }
  93. return $fileId;
  94. }
  95. }