Skip to content

Commit

Permalink
Remove the prefix after assembling the last chunk.
Browse files Browse the repository at this point in the history
Until now, the reassembled file shared the name with the very first chunk uploaded.
It had the form {prefix}_{filename}. This commit fixes this issue and addresses #21 with it.
  • Loading branch information
sheeep committed Jun 24, 2013
1 parent f6804b9 commit 5364f98
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
23 changes: 20 additions & 3 deletions Tests/Controller/AbstractChunkedUploadTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@

use Symfony\Component\EventDispatcher\Event;
use Oneup\UploaderBundle\Tests\Controller\AbstractUploadTest;
use Oneup\UploaderBundle\UploadEvents;
use Oneup\UploaderBundle\Event\PostChunkUploadEvent;
use Oneup\UploaderBundle\Event\PreUploadEvent;
use Oneup\UploaderBundle\UploadEvents;

abstract class AbstractChunkedUploadTest extends AbstractUploadTest
{
Expand All @@ -17,11 +18,27 @@ abstract protected function getNextFile($i);
public function testChunkedUpload()
{
// assemble a request
$client = $this->client;
$me = $this;
$endpoint = $this->helper->endpoint($this->getConfigKey());
$basename = '';

for ($i = 0; $i < $this->total; $i ++) {
$client->request('POST', $endpoint, $this->getNextRequestParameters($i), array($this->getNextFile($i)));
$file = $this->getNextFile($i);

if ($basename === '') {
$basename = $file->getClientOriginalName();
}

$client = static::createClient();
$dispatcher = $client->getContainer()->get('event_dispatcher');

$dispatcher->addListener(UploadEvents::PRE_UPLOAD, function(PreUploadEvent $event) use (&$me, $basename) {
$file = $event->getFile();

$me->assertEquals($file->getBasename(), $basename);
});

$client->request('POST', $endpoint, $this->getNextRequestParameters($i), array($file));
$response = $client->getResponse();

$this->assertTrue($response->isSuccessful());
Expand Down
7 changes: 6 additions & 1 deletion Uploader/Chunk/ChunkManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Oneup\UploaderBundle\Uploader\Chunk;

use Symfony\Component\HttpFoundation\File\File;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Symfony\Component\Finder\Finder;
use Symfony\Component\Filesystem\Filesystem;
Expand Down Expand Up @@ -65,7 +66,11 @@ public function assembleChunks(\IteratorAggregate $chunks)
$iterator->next();
}

return $base;
// remove the prefix added by self::addChunk
$assembled = new File($base->getRealPath());
$assembled = $assembled->move($base->getPath(), preg_replace('/^(.+?)_/', '', $base->getBasename()));

return $assembled;
}

public function cleanup($path)
Expand Down

0 comments on commit 5364f98

Please sign in to comment.