|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | +/** |
| 5 | + * SPDX-FileCopyrightText: 2023 Nextcloud GmbH and Nextcloud contributors |
| 6 | + * SPDX-License-Identifier: AGPL-3.0-or-later |
| 7 | + */ |
| 8 | + |
| 9 | +namespace OCA\Files\Command\Object; |
| 10 | + |
| 11 | +use OC\Core\Command\Base; |
| 12 | +use OCP\DB\QueryBuilder\IQueryBuilder; |
| 13 | +use OCP\Files\ObjectStore\IObjectStoreMetaData; |
| 14 | +use OCP\IDBConnection; |
| 15 | +use Symfony\Component\Console\Input\InputInterface; |
| 16 | +use Symfony\Component\Console\Input\InputOption; |
| 17 | +use Symfony\Component\Console\Output\OutputInterface; |
| 18 | + |
| 19 | +class Orphans extends Base { |
| 20 | + private const CHUNK_SIZE = 100; |
| 21 | + |
| 22 | + private ?IQueryBuilder $query = null; |
| 23 | + |
| 24 | + public function __construct( |
| 25 | + private readonly ObjectUtil $objectUtils, |
| 26 | + private readonly IDBConnection $connection, |
| 27 | + ) { |
| 28 | + parent::__construct(); |
| 29 | + } |
| 30 | + |
| 31 | + private function getQuery(): IQueryBuilder { |
| 32 | + if (!$this->query) { |
| 33 | + $this->query = $this->connection->getQueryBuilder(); |
| 34 | + $this->query->select('fileid') |
| 35 | + ->from('filecache') |
| 36 | + ->where($this->query->expr()->eq('fileid', $this->query->createParameter('file_id'))); |
| 37 | + } |
| 38 | + return $this->query; |
| 39 | + } |
| 40 | + |
| 41 | + protected function configure(): void { |
| 42 | + parent::configure(); |
| 43 | + $this |
| 44 | + ->setName('files:object:orphans') |
| 45 | + ->setDescription('List all objects in the object store that don\'t have a matching entry in the database') |
| 46 | + ->addOption('bucket', 'b', InputOption::VALUE_REQUIRED, "Bucket to list the objects from, only required in cases where it can't be determined from the config"); |
| 47 | + } |
| 48 | + |
| 49 | + public function execute(InputInterface $input, OutputInterface $output): int { |
| 50 | + $objectStore = $this->objectUtils->getObjectStore($input->getOption('bucket'), $output); |
| 51 | + if (!$objectStore) { |
| 52 | + return self::FAILURE; |
| 53 | + } |
| 54 | + |
| 55 | + if (!$objectStore instanceof IObjectStoreMetaData) { |
| 56 | + $output->writeln('<error>Configured object store does currently not support listing objects</error>'); |
| 57 | + return self::FAILURE; |
| 58 | + } |
| 59 | + $prefixLength = strlen('urn:oid:'); |
| 60 | + |
| 61 | + $objects = $objectStore->listObjects('urn:oid:'); |
| 62 | + $orphans = new \CallbackFilterIterator($objects, function (array $object) use ($prefixLength) { |
| 63 | + $fileId = (int)substr($object['urn'], $prefixLength); |
| 64 | + return !$this->fileIdInDb($fileId); |
| 65 | + }); |
| 66 | + |
| 67 | + $orphans = $this->objectUtils->formatObjects($orphans, $input->getOption('output') === self::OUTPUT_FORMAT_PLAIN); |
| 68 | + $this->writeStreamingTableInOutputFormat($input, $output, $orphans, self::CHUNK_SIZE); |
| 69 | + |
| 70 | + return self::SUCCESS; |
| 71 | + } |
| 72 | + |
| 73 | + private function fileIdInDb(int $fileId): bool { |
| 74 | + $query = $this->getQuery(); |
| 75 | + $query->setParameter('file_id', $fileId, IQueryBuilder::PARAM_INT); |
| 76 | + $result = $query->executeQuery(); |
| 77 | + return $result->fetchOne() !== false; |
| 78 | + } |
| 79 | +} |
0 commit comments