1313use OCP \Files \ObjectStore \IObjectStore ;
1414use OCP \IConfig ;
1515use OCP \IDBConnection ;
16+ use OCP \Util ;
1617use Symfony \Component \Console \Input \InputInterface ;
1718use Symfony \Component \Console \Output \OutputInterface ;
1819
@@ -97,32 +98,28 @@ public function objectExistsInDb(string $object): int|false {
9798 public function writeIteratorToOutput (InputInterface $ input , OutputInterface $ output , \Iterator $ objects , int $ chunkSize ): void {
9899 $ outputType = $ input ->getOption ('output ' );
99100 $ humanOutput = $ outputType === Base::OUTPUT_FORMAT_PLAIN ;
100- $ first = true ;
101101
102- if (!$ humanOutput ) {
103- $ output ->writeln ('[ ' );
104- }
102+ if ($ humanOutput ) {
103+ // we can't write tables in a streaming way, so we print them in chunks instead
104+ foreach ($ this ->chunkIterator ($ objects , $ chunkSize ) as $ chunk ) {
105+ $ this ->outputChunkHuman ($ input , $ output , $ chunk );
106+ }
107+ } else {
108+ $ first = true ;
105109
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 ;
110+ $ output ->writeln ('[ ' );
111+ foreach ($ objects as $ object ) {
112+ if (!$ first ) {
113+ $ output ->writeln (', ' );
121114 }
115+ $ row = $ this ->formatObject ($ object , false );
116+ if ($ outputType === self ::OUTPUT_FORMAT_JSON_PRETTY ) {
117+ $ output ->write (json_encode ($ row , JSON_PRETTY_PRINT ));
118+ } else {
119+ $ output ->write (json_encode ($ row ));
120+ }
121+ $ first = false ;
122122 }
123- }
124-
125- if (!$ humanOutput ) {
126123 $ output ->writeln ("\n] " );
127124 }
128125 }
@@ -133,20 +130,18 @@ private function formatObject(array $object, bool $humanOutput): array {
133130 ], ($ object ['metadata ' ] ?? []));
134131
135132 if ($ humanOutput && isset ($ row ['size ' ])) {
136- $ row ['size ' ] = \OC_Helper ::humanFileSize ($ row ['size ' ]);
133+ $ row ['size ' ] = Util ::humanFileSize ($ row ['size ' ]);
137134 }
138135 if (isset ($ row ['mtime ' ])) {
139136 $ row ['mtime ' ] = $ row ['mtime ' ]->format (\DateTimeImmutable::ATOM );
140137 }
141138 return $ row ;
142139 }
143140
144- private function outputChunk (InputInterface $ input , OutputInterface $ output , iterable $ chunk ): void {
141+ private function outputChunkHuman (InputInterface $ input , OutputInterface $ output , iterable $ chunk ): void {
145142 $ result = [];
146- $ humanOutput = $ input ->getOption ('output ' ) === 'plain ' ;
147-
148143 foreach ($ chunk as $ object ) {
149- $ result [] = $ this ->formatObject ($ object , $ humanOutput );
144+ $ result [] = $ this ->formatObject ($ object , true );
150145 }
151146 $ this ->writeTableInOutputFormat ($ input , $ output , $ result );
152147 }
@@ -158,12 +153,14 @@ public function chunkIterator(\Iterator $iterator, int $count): \Iterator {
158153 $ chunk [] = $ iterator ->current ();
159154 $ iterator ->next ();
160155 if (count ($ chunk ) == $ count ) {
156+ // Got a full chunk, yield and start a new one
161157 yield $ chunk ;
162158 $ chunk = [];
163159 }
164160 }
165161
166162 if (count ($ chunk )) {
163+ // Yield the last chunk even if incomplete
167164 yield $ chunk ;
168165 }
169166 }
0 commit comments