From cab0860a44d257dcc6a4a2b1f94d1237fe1765b6 Mon Sep 17 00:00:00 2001 From: Jim Schmid Date: Mon, 24 Jun 2013 14:08:30 +0200 Subject: [PATCH] Refactored the way Chunks are reassembled. Removed a rather hacky way of determine which of the chunks to use as a base, and replaced it with an Iterator as mentioned in #21. --- Uploader/Chunk/ChunkManager.php | 27 ++++++++++++--------------- 1 file changed, 12 insertions(+), 15 deletions(-) diff --git a/Uploader/Chunk/ChunkManager.php b/Uploader/Chunk/ChunkManager.php index b1dcf236..f5bc733d 100644 --- a/Uploader/Chunk/ChunkManager.php +++ b/Uploader/Chunk/ChunkManager.php @@ -49,23 +49,20 @@ public function addChunk($uuid, $index, UploadedFile $chunk, $original) public function assembleChunks(\Traversable $chunks) { - // I don't really get it why getIterator()->current() always - // gives me a null-value, due to that I've to implement this - // in a rather unorthodox way. - $i = 0; - $base = null; - - foreach ($chunks as $file) { - if ($i++ == 0) { - $base = $file; - - // proceed with next files, as we have our - // base data-container - continue; - } + $iterator = $chunks->getIterator()->getInnerIterator(); + + $base = $iterator->current(); + $iterator->next(); + + while ($iterator->valid()) { - if(false === file_put_contents($base->getPathname(), file_get_contents($file->getPathname()), \FILE_APPEND | \LOCK_EX)) + $file = $iterator->current(); + + if (false === file_put_contents($base->getPathname(), file_get_contents($file->getPathname()), \FILE_APPEND | \LOCK_EX)) { throw new \RuntimeException('Reassembling chunks failed.'); + } + + $iterator->next(); } return $base;