ObjectUtil.php 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2023 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OCA\Files\Command\Object;
  8. use OCP\DB\QueryBuilder\IQueryBuilder;
  9. use OCP\Files\ObjectStore\IObjectStore;
  10. use OCP\IConfig;
  11. use OCP\IDBConnection;
  12. use Symfony\Component\Console\Output\OutputInterface;
  13. class ObjectUtil {
  14. public function __construct(
  15. private IConfig $config,
  16. private IDBConnection $connection,
  17. ) {
  18. }
  19. private function getObjectStoreConfig(): ?array {
  20. $config = $this->config->getSystemValue('objectstore_multibucket');
  21. if (is_array($config)) {
  22. $config['multibucket'] = true;
  23. return $config;
  24. }
  25. $config = $this->config->getSystemValue('objectstore');
  26. if (is_array($config)) {
  27. if (!isset($config['multibucket'])) {
  28. $config['multibucket'] = false;
  29. }
  30. return $config;
  31. }
  32. return null;
  33. }
  34. public function getObjectStore(?string $bucket, OutputInterface $output): ?IObjectStore {
  35. $config = $this->getObjectStoreConfig();
  36. if (!$config) {
  37. $output->writeln('<error>Instance is not using primary object store</error>');
  38. return null;
  39. }
  40. if ($config['multibucket'] && !$bucket) {
  41. $output->writeln('<error>--bucket option required</error> because <info>multi bucket</info> is enabled.');
  42. return null;
  43. }
  44. if (!isset($config['arguments'])) {
  45. throw new \Exception('no arguments configured for object store configuration');
  46. }
  47. if (!isset($config['class'])) {
  48. throw new \Exception('no class configured for object store configuration');
  49. }
  50. if ($bucket) {
  51. // s3, swift
  52. $config['arguments']['bucket'] = $bucket;
  53. // azure
  54. $config['arguments']['container'] = $bucket;
  55. }
  56. $store = new $config['class']($config['arguments']);
  57. if (!$store instanceof IObjectStore) {
  58. throw new \Exception('configured object store class is not an object store implementation');
  59. }
  60. return $store;
  61. }
  62. /**
  63. * Check if an object is referenced in the database
  64. */
  65. public function objectExistsInDb(string $object): int|false {
  66. if (!str_starts_with($object, 'urn:oid:')) {
  67. return false;
  68. }
  69. $fileId = (int)substr($object, strlen('urn:oid:'));
  70. $query = $this->connection->getQueryBuilder();
  71. $query->select('fileid')
  72. ->from('filecache')
  73. ->where($query->expr()->eq('fileid', $query->createNamedParameter($fileId, IQueryBuilder::PARAM_INT)));
  74. $result = $query->executeQuery();
  75. if ($result->fetchOne() === false) {
  76. return false;
  77. }
  78. return $fileId;
  79. }
  80. }