From 9dbd9056dfe403ce6f1273d2d75fe814d517731a Mon Sep 17 00:00:00 2001 From: Jim Schmid Date: Mon, 15 Jul 2013 01:24:35 +0200 Subject: [PATCH] Distribute the load of assembling chunks to every upload request. This is a configurable option, as it could be the case that chunks will be uploaded asynchronous and therefore not in the correct order. Fixes #24. --- Controller/AbstractChunkedController.php | 16 ++++++++++------ DependencyInjection/Configuration.php | 1 + Uploader/Chunk/ChunkManager.php | 12 +++++++++++- 3 files changed, 22 insertions(+), 7 deletions(-) diff --git a/Controller/AbstractChunkedController.php b/Controller/AbstractChunkedController.php index 4d797d16..f0537036 100644 --- a/Controller/AbstractChunkedController.php +++ b/Controller/AbstractChunkedController.php @@ -57,16 +57,20 @@ protected function handleChunkedUpload(UploadedFile $file, ResponseInterface $re $chunk = $chunkManager->addChunk($uuid, $index, $file, $orig); $this->dispatchChunkEvents($chunk, $response, $request, $last); + + if ($chunkManager->getLoadDistribution()) { + $chunks = $chunkManager->getChunks($uuid); + $assembled = $chunkManager->assembleChunks($chunks); + } // if all chunks collected and stored, proceed // with reassembling the parts if ($last) { - // we'll take the first chunk and append the others to it - // this way we don't need another file in temporary space for assembling - $chunks = $chunkManager->getChunks($uuid); - - // assemble parts - $assembled = $chunkManager->assembleChunks($chunks); + if (!$chunkManager->getLoadDistribution()) { + $chunks = $chunkManager->getChunks($uuid); + $assembled = $chunkManager->assembleChunks($chunks); + } + $path = $assembled->getPath(); // create a temporary uploaded file to meet the interface restrictions diff --git a/DependencyInjection/Configuration.php b/DependencyInjection/Configuration.php index 9b4c457e..02085e02 100644 --- a/DependencyInjection/Configuration.php +++ b/DependencyInjection/Configuration.php @@ -19,6 +19,7 @@ public function getConfigTreeBuilder() ->children() ->scalarNode('maxage')->defaultValue(604800)->end() ->scalarNode('directory')->defaultNull()->end() + ->booleanNode('load_distribution')->defaultTrue()->end() ->end() ->end() ->arrayNode('orphanage') diff --git a/Uploader/Chunk/ChunkManager.php b/Uploader/Chunk/ChunkManager.php index 5bfa4b54..310cd0b3 100644 --- a/Uploader/Chunk/ChunkManager.php +++ b/Uploader/Chunk/ChunkManager.php @@ -48,7 +48,7 @@ public function addChunk($uuid, $index, UploadedFile $chunk, $original) return $chunk->move($path, $name); } - public function assembleChunks(\IteratorAggregate $chunks) + public function assembleChunks(\IteratorAggregate $chunks, $removeChunk = true) { $iterator = $chunks->getIterator()->getInnerIterator(); @@ -63,6 +63,11 @@ public function assembleChunks(\IteratorAggregate $chunks) throw new \RuntimeException('Reassembling chunks failed.'); } + if ($removeChunk) { + $filesystem = new Filesystem(); + $filesystem->remove($file->getPathname()); + } + $iterator->next(); } @@ -97,4 +102,9 @@ public function getChunks($uuid) return $finder; } + + public function getLoadDistribution() + { + return $this->configuration['load_distribution']; + } }