diff options
author | Robin Appelman <robin@icewind.nl> | 2025-03-20 15:22:27 +0100 |
---|---|---|
committer | Andy Scherzinger <info@andy-scherzinger.de> | 2025-04-24 23:22:42 +0200 |
commit | aa830325bd679eb24386d118bdbdf5a78bcc5fd2 (patch) | |
tree | 4a66fa706940365324fce60a53c064d3859ffca0 /apps/files/lib/Command/Object/ObjectUtil.php | |
parent | ca6f59536763d635adb4d9496b734b6e017b489e (diff) | |
download | nextcloud-server-backport/object-store-orphan/27.tar.gz nextcloud-server-backport/object-store-orphan/27.zip |
feat: add command to get object metadatabackport/object-store-orphan/27
feat: add command to list objects
feat: add command to list orphan objects
Signed-off-by: Robin Appelman <robin@icewind.nl>
Diffstat (limited to 'apps/files/lib/Command/Object/ObjectUtil.php')
-rw-r--r-- | apps/files/lib/Command/Object/ObjectUtil.php | 78 |
1 files changed, 77 insertions, 1 deletions
diff --git a/apps/files/lib/Command/Object/ObjectUtil.php b/apps/files/lib/Command/Object/ObjectUtil.php index b7359dfa193..04902c829bf 100644 --- a/apps/files/lib/Command/Object/ObjectUtil.php +++ b/apps/files/lib/Command/Object/ObjectUtil.php @@ -23,13 +23,15 @@ declare(strict_types=1); namespace OCA\Files\Command\Object; +use OC\Core\Command\Base; use OCP\DB\QueryBuilder\IQueryBuilder; use OCP\Files\ObjectStore\IObjectStore; use OCP\IConfig; use OCP\IDBConnection; +use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; -class ObjectUtil { +class ObjectUtil extends Base { private IConfig $config; private IDBConnection $connection; @@ -107,4 +109,78 @@ class ObjectUtil { return false; } } + + public function writeIteratorToOutput(InputInterface $input, OutputInterface $output, \Iterator $objects, int $chunkSize): void { + $outputType = $input->getOption('output'); + $humanOutput = $outputType === Base::OUTPUT_FORMAT_PLAIN; + $first = true; + + if (!$humanOutput) { + $output->writeln('['); + } + + foreach ($this->chunkIterator($objects, $chunkSize) as $chunk) { + if ($outputType === Base::OUTPUT_FORMAT_PLAIN) { + $this->outputChunk($input, $output, $chunk); + } else { + foreach ($chunk as $object) { + if (!$first) { + $output->writeln(','); + } + $row = $this->formatObject($object, $humanOutput); + if ($outputType === Base::OUTPUT_FORMAT_JSON_PRETTY) { + $output->write(json_encode($row, JSON_PRETTY_PRINT)); + } else { + $output->write(json_encode($row)); + } + $first = false; + } + } + } + + if (!$humanOutput) { + $output->writeln("\n]"); + } + } + + private function formatObject(array $object, bool $humanOutput): array { + $row = array_merge([ + 'urn' => $object['urn'], + ], ($object['metadata'] ?? [])); + + if ($humanOutput && isset($row['size'])) { + $row['size'] = \OC_Helper::humanFileSize($row['size']); + } + if (isset($row['mtime'])) { + $row['mtime'] = $row['mtime']->format(\DateTimeImmutable::ATOM); + } + return $row; + } + + private function outputChunk(InputInterface $input, OutputInterface $output, iterable $chunk): void { + $result = []; + $humanOutput = $input->getOption('output') === 'plain'; + + foreach ($chunk as $object) { + $result[] = $this->formatObject($object, $humanOutput); + } + $this->writeTableInOutputFormat($input, $output, $result); + } + + public function chunkIterator(\Iterator $iterator, int $count): \Iterator { + $chunk = []; + + for ($i = 0; $iterator->valid(); $i++) { + $chunk[] = $iterator->current(); + $iterator->next(); + if (count($chunk) == $count) { + yield $chunk; + $chunk = []; + } + } + + if (count($chunk)) { + yield $chunk; + } + } } |