|
8 | 8 |
|
9 | 9 | namespace OCA\Files\Command\Object;
|
10 | 10 |
|
| 11 | +use OC\Core\Command\Base; |
11 | 12 | use OCP\DB\QueryBuilder\IQueryBuilder;
|
12 | 13 | use OCP\Files\ObjectStore\IObjectStore;
|
13 | 14 | use OCP\IConfig;
|
14 | 15 | use OCP\IDBConnection;
|
| 16 | +use Symfony\Component\Console\Input\InputInterface; |
15 | 17 | use Symfony\Component\Console\Output\OutputInterface;
|
16 | 18 |
|
17 |
| -class ObjectUtil { |
| 19 | +class ObjectUtil extends Base { |
18 | 20 | public function __construct(
|
19 | 21 | private IConfig $config,
|
20 | 22 | private IDBConnection $connection,
|
@@ -91,4 +93,78 @@ public function objectExistsInDb(string $object): int|false {
|
91 | 93 |
|
92 | 94 | return $fileId;
|
93 | 95 | }
|
| 96 | + |
| 97 | + public function writeIteratorToOutput(InputInterface $input, OutputInterface $output, \Iterator $objects, int $chunkSize): void { |
| 98 | + $outputType = $input->getOption('output'); |
| 99 | + $humanOutput = $outputType === Base::OUTPUT_FORMAT_PLAIN; |
| 100 | + $first = true; |
| 101 | + |
| 102 | + if (!$humanOutput) { |
| 103 | + $output->writeln('['); |
| 104 | + } |
| 105 | + |
| 106 | + foreach ($this->chunkIterator($objects, $chunkSize) as $chunk) { |
| 107 | + if ($outputType === Base::OUTPUT_FORMAT_PLAIN) { |
| 108 | + $this->outputChunk($input, $output, $chunk); |
| 109 | + } else { |
| 110 | + foreach ($chunk as $object) { |
| 111 | + if (!$first) { |
| 112 | + $output->writeln(','); |
| 113 | + } |
| 114 | + $row = $this->formatObject($object, $humanOutput); |
| 115 | + if ($outputType === Base::OUTPUT_FORMAT_JSON_PRETTY) { |
| 116 | + $output->write(json_encode($row, JSON_PRETTY_PRINT)); |
| 117 | + } else { |
| 118 | + $output->write(json_encode($row)); |
| 119 | + } |
| 120 | + $first = false; |
| 121 | + } |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + if (!$humanOutput) { |
| 126 | + $output->writeln("\n]"); |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + private function formatObject(array $object, bool $humanOutput): array { |
| 131 | + $row = array_merge([ |
| 132 | + 'urn' => $object['urn'], |
| 133 | + ], ($object['metadata'] ?? [])); |
| 134 | + |
| 135 | + if ($humanOutput && isset($row['size'])) { |
| 136 | + $row['size'] = \OC_Helper::humanFileSize($row['size']); |
| 137 | + } |
| 138 | + if (isset($row['mtime'])) { |
| 139 | + $row['mtime'] = $row['mtime']->format(\DateTimeImmutable::ATOM); |
| 140 | + } |
| 141 | + return $row; |
| 142 | + } |
| 143 | + |
| 144 | + private function outputChunk(InputInterface $input, OutputInterface $output, iterable $chunk): void { |
| 145 | + $result = []; |
| 146 | + $humanOutput = $input->getOption('output') === 'plain'; |
| 147 | + |
| 148 | + foreach ($chunk as $object) { |
| 149 | + $result[] = $this->formatObject($object, $humanOutput); |
| 150 | + } |
| 151 | + $this->writeTableInOutputFormat($input, $output, $result); |
| 152 | + } |
| 153 | + |
| 154 | + public function chunkIterator(\Iterator $iterator, int $count): \Iterator { |
| 155 | + $chunk = []; |
| 156 | + |
| 157 | + for ($i = 0; $iterator->valid(); $i++) { |
| 158 | + $chunk[] = $iterator->current(); |
| 159 | + $iterator->next(); |
| 160 | + if (count($chunk) == $count) { |
| 161 | + yield $chunk; |
| 162 | + $chunk = []; |
| 163 | + } |
| 164 | + } |
| 165 | + |
| 166 | + if (count($chunk)) { |
| 167 | + yield $chunk; |
| 168 | + } |
| 169 | + } |
94 | 170 | }
|
0 commit comments