@@ -61,9 +61,21 @@ public function initialize(Server $server): void {
6161 $ this ->server ->on ('afterMethod:GET ' , $ this ->afterDownload (...), 999 );
6262 }
6363
64+ /**
65+ * Recursively iterate over all nodes in a folder.
66+ */
67+ protected function iterateNodes (NcNode $ node ): iterable {
68+ if ($ node instanceof NcFile) {
69+ yield $ node ;
70+ } elseif ($ node instanceof NcFolder) {
71+ foreach ($ node ->getDirectoryListing () as $ childNode ) {
72+ yield from $ this ->iterateNodes ($ childNode );
73+ }
74+ }
75+ }
76+
6477 /**
6578 * Adding a node to the archive streamer.
66- * This will recursively add new nodes to the stream if the node is a directory.
6779 */
6880 protected function streamNode (Streamer $ streamer , NcNode $ node , string $ rootPath ): void {
6981 // Remove the root path from the filename to make it relative to the requested folder
@@ -79,10 +91,6 @@ protected function streamNode(Streamer $streamer, NcNode $node, string $rootPath
7991 $ streamer ->addFileFromStream ($ resource , $ filename , $ node ->getSize (), $ mtime );
8092 } elseif ($ node instanceof NcFolder) {
8193 $ streamer ->addEmptyDir ($ filename , $ mtime );
82- $ content = $ node ->getDirectoryListing ();
83- foreach ($ content as $ subNode ) {
84- $ this ->streamNode ($ streamer , $ subNode , $ rootPath );
85- }
8694 }
8795 }
8896
@@ -137,14 +145,20 @@ public function handleDownload(Request $request, Response $response): ?bool {
137145 }
138146
139147 $ folder = $ node ->getNode ();
140- $ nodes = empty ($ files ) ? $ folder ->getDirectoryListing () : [];
148+ $ rootNodes = empty ($ files ) ? $ folder ->getDirectoryListing () : [];
141149 foreach ($ files as $ path ) {
142150 $ child = $ node ->getChild ($ path );
143151 assert ($ child instanceof Node);
144- $ nodes [] = $ child ->getNode ();
152+ $ rootNodes [] = $ child ->getNode ();
153+ }
154+ $ allNodes = [];
155+ foreach ($ rootNodes as $ rootNode ) {
156+ foreach ($ this ->iterateNodes ($ rootNode ) as $ node ) {
157+ $ allNodes [] = $ node ;
158+ }
145159 }
146160
147- $ event = new BeforeZipCreatedEvent ($ folder , $ files , $ nodes );
161+ $ event = new BeforeZipCreatedEvent ($ folder , $ files , $ allNodes );
148162 $ this ->eventDispatcher ->dispatchTyped ($ event );
149163 if ((!$ event ->isSuccessful ()) || $ event ->getErrorMessage () !== null ) {
150164 $ errorMessage = $ event ->getErrorMessage ();
@@ -156,8 +170,7 @@ public function handleDownload(Request $request, Response $response): ?bool {
156170 // Downloading was denied by an app
157171 throw new Forbidden ($ errorMessage );
158172 }
159-
160- $ nodes = $ event ->getNodes ();
173+ $ allNodes = $ event ->getNodes ();
161174
162175 $ archiveName = $ folder ->getName ();
163176 if (count (explode ('/ ' , trim ($ folder ->getPath (), '/ ' ), 3 )) === 2 ) {
@@ -171,13 +184,13 @@ public function handleDownload(Request $request, Response $response): ?bool {
171184 $ rootPath = dirname ($ folder ->getPath ());
172185 }
173186
174- $ streamer = new Streamer ($ tarRequest , -1 , count ($ nodes ), $ this ->timezoneFactory );
187+ $ streamer = new Streamer ($ tarRequest , -1 , count ($ rootNodes ), $ this ->timezoneFactory );
175188 $ streamer ->sendHeaders ($ archiveName );
176189 // For full folder downloads we also add the folder itself to the archive
177190 if (empty ($ files )) {
178191 $ streamer ->addEmptyDir ($ archiveName );
179192 }
180- foreach ($ nodes as $ node ) {
193+ foreach ($ allNodes as $ node ) {
181194 $ this ->streamNode ($ streamer , $ node , $ rootPath );
182195 }
183196 $ streamer ->finalize ();
0 commit comments