Skip to content

Commit

Permalink
Distribute the load of assembling chunks to every upload request.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
sheeep committed Jul 14, 2013
1 parent 60e1768 commit 9dbd905
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 7 deletions.
16 changes: 10 additions & 6 deletions Controller/AbstractChunkedController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
12 changes: 11 additions & 1 deletion Uploader/Chunk/ChunkManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -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();
}

Expand Down Expand Up @@ -97,4 +102,9 @@ public function getChunks($uuid)

return $finder;
}

public function getLoadDistribution()
{
return $this->configuration['load_distribution'];
}
}

0 comments on commit 9dbd905

Please sign in to comment.