Skip to content

Commit

Permalink
Refactored Event dispatching (BC-break)
Browse files Browse the repository at this point in the history
* Moved dispatching to the upload* functions
* Added a new Event which will be fired after a chunk has been uploaded

This is a huge BC-break. It has to be done to address #20 though.
  • Loading branch information
sheeep committed Jun 20, 2013
1 parent 7cf2a6d commit a408548
Show file tree
Hide file tree
Showing 13 changed files with 154 additions and 38 deletions.
28 changes: 22 additions & 6 deletions Controller/AbstractChunkedController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\File\UploadedFile;

use Oneup\UploaderBundle\UploadEvents;
use Oneup\UploaderBundle\Uploader\Response\ResponseInterface;
use Oneup\UploaderBundle\Controller\AbstractController;
use Oneup\UploaderBundle\Event\PostChunkUploadEvent;

abstract class AbstractChunkedController extends AbstractController
{
Expand Down Expand Up @@ -35,7 +39,7 @@ abstract protected function parseChunkedRequest(Request $request);
*
* @param file The uploaded chunk.
*/
protected function handleChunkedUpload(UploadedFile $file)
protected function handleChunkedUpload(UploadedFile $file, ResponseInterface $response, Request $request)
{
// get basic container stuff
$request = $this->container->get('request');
Expand All @@ -47,12 +51,13 @@ protected function handleChunkedUpload(UploadedFile $file)
// get information about this chunked request
list($last, $uuid, $index, $orig) = $this->parseChunkedRequest($request);

$uploaded = $chunkManager->addChunk($uuid, $index, $file, $orig);
$chunk = $chunkManager->addChunk($uuid, $index, $file, $orig);

$this->dispatchChunkEvents($chunk, $response, $request, $last);

// if all chunks collected and stored, proceed
// with reassembling the parts
if($last)
{
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);
Expand All @@ -66,11 +71,22 @@ protected function handleChunkedUpload(UploadedFile $file)

// validate this entity and upload on success
$this->validate($uploadedFile);
$uploaded = $this->handleUpload($uploadedFile);
$uploaded = $this->handleUpload($uploadedFile, $response, $request);

$chunkManager->cleanup($path);
}
}

/**
* This function is a helper function which dispatches post chunk upload event.
*/
protected function dispatchChunkEvents($uploaded, ResponseInterface $response, Request $request, $isLast)
{
$dispatcher = $this->container->get('event_dispatcher');

return $uploaded;
// dispatch post upload event (both the specific and the general)
$postUploadEvent = new PostChunkUploadEvent($uploaded, $response, $request, $isLast, $this->type, $this->config);
$dispatcher->dispatch(UploadEvents::POST_CHUNK_UPLOAD, $postUploadEvent);
$dispatcher->dispatch(sprintf('%s.%s', UploadEvents::POST_CHUNK_UPLOAD, $this->type), $postUploadEvent);
}
}
7 changes: 3 additions & 4 deletions Controller/AbstractController.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ abstract public function upload();
* @param UploadedFile The file to upload
* @return File the actual file
*/
protected function handleUpload(UploadedFile $file)
protected function handleUpload(UploadedFile $file, ResponseInterface $response, Request $request)
{
$this->validate($file);

Expand All @@ -55,7 +55,7 @@ protected function handleUpload(UploadedFile $file)
// perform the real upload
$uploaded = $this->storage->upload($file, $name);

return $uploaded;
$this->dispatchEvents($uploaded, $response, $request);
}

/**
Expand All @@ -71,8 +71,7 @@ protected function dispatchEvents($uploaded, ResponseInterface $response, Reques
$dispatcher->dispatch(UploadEvents::POST_UPLOAD, $postUploadEvent);
$dispatcher->dispatch(sprintf('%s.%s', UploadEvents::POST_UPLOAD, $this->type), $postUploadEvent);

if(!$this->config['use_orphanage'])
{
if (!$this->config['use_orphanage']) {
// dispatch post persist event (both the specific and the general)
$postPersistEvent = new PostPersistEvent($uploaded, $response, $request, $this->type, $this->config);
$dispatcher->dispatch(UploadEvents::POST_PERSIST, $postPersistEvent);
Expand Down
15 changes: 6 additions & 9 deletions Controller/BlueimpController.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,12 @@ public function upload()
{
$file = $file[0];

try
{
$uploaded = $chunked ? $this->handleChunkedUpload($file) : $this->handleUpload($file);

// dispatch POST_PERSIST AND POST_UPLOAD events
$this->dispatchEvents($uploaded, $response, $request);
}
catch(UploadException $e)
{
try {
$chunked ?
$this->handleChunkedUpload($file, $response, $request) :
$this->handleUpload($file, $response, $request)
;
} catch(UploadException $e) {
// return nothing
return new JsonResponse(array());
}
Expand Down
2 changes: 1 addition & 1 deletion Controller/FancyUploadController.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public function upload()
{
try
{
$uploaded = $this->handleUpload($file);
$uploaded = $this->handleUpload($file, $response, $request);

// dispatch POST_PERSIST AND POST_UPLOAD events
$this->dispatchEvents($uploaded, $response, $request);
Expand Down
8 changes: 4 additions & 4 deletions Controller/FineUploaderController.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ public function upload()
{
try
{
$uploaded = $chunked ? $this->handleChunkedUpload($file) : $this->handleUpload($file);

// dispatch POST_PERSIST AND POST_UPLOAD events
$this->dispatchEvents($uploaded, $response, $request);
$chunked ?
$this->handleChunkedUpload($file, $response, $request) :
$this->handleUpload($file, $response, $request)
;
}
catch(UploadException $e)
{
Expand Down
10 changes: 5 additions & 5 deletions Controller/MooUploadController.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,18 @@ public function upload()

try
{
$uploaded = $chunked ? $this->handleChunkedUpload($file) : $this->handleUpload($file);

// fill response object
$response = $this->response;

$response->setId($headers->get('x-file-id'));
$response->setSize($headers->get('content-length'));
$response->setName($headers->get('x-file-name'));
$response->setUploadedName($uploadFileName);

// dispatch POST_PERSIST AND POST_UPLOAD events
$this->dispatchEvents($uploaded, $response, $request);

$chunked ?
$this->handleChunkedUpload($file, $response, $request) :
$this->handleUpload($file, $response, $request)
;
}
catch(UploadException $e)
{
Expand Down
8 changes: 4 additions & 4 deletions Controller/PluploadController.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ public function upload()
{
try
{
$uploaded = $chunked ? $this->handleChunkedUpload($file) : $this->handleUpload($file);

// dispatch POST_PERSIST AND POST_UPLOAD events
$this->dispatchEvents($uploaded, $response, $request);
$chunked ?
$this->handleChunkedUpload($file, $response, $request) :
$this->handleUpload($file, $response, $request)
;
}
catch(UploadException $e)
{
Expand Down
2 changes: 1 addition & 1 deletion Controller/UploadifyController.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public function upload()
{
try
{
$uploaded = $this->handleUpload($file);
$uploaded = $this->handleUpload($file, $response, $request);

// dispatch POST_PERSIST AND POST_UPLOAD events
$this->dispatchEvents($uploaded, $response, $request);
Expand Down
2 changes: 1 addition & 1 deletion Controller/YUI3Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public function upload()
{
try
{
$uploaded = $this->handleUpload($file);
$uploaded = $this->handleUpload($file, $response, $request);

// dispatch POST_PERSIST AND POST_UPLOAD events
$this->dispatchEvents($uploaded, $response, $request);
Expand Down
62 changes: 62 additions & 0 deletions Event/PostChunkUploadEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

namespace Oneup\UploaderBundle\Event;

use Symfony\Component\EventDispatcher\Event;
use Symfony\Component\HttpFoundation\Request;
use Oneup\UploaderBundle\Uploader\Response\ResponseInterface;

class PostChunkUploadEvent extends Event
{
protected $file;
protected $request;
protected $type;
protected $response;
protected $config;
protected $isLast;

public function __construct($chunk, ResponseInterface $response, Request $request, $isLast, $type, array $config)
{
$this->chunk = $chunk;
$this->request = $request;
$this->response = $response;
$this->isLast = $isLast;
$this->type = $type;
$this->config = $config;
}

public function getChunk()
{
return $this->chunk;
}

public function getRequest()
{
return $this->request;
}

public function getType()
{
return $this->type;
}

public function getResponse()
{
return $this->response;
}

public function getConfig()
{
return $this->config;
}

public function getIsLast()
{
return $this->isLast;
}

public function isLast()
{
return $this->isLast;
}
}
4 changes: 4 additions & 0 deletions Resources/doc/events.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ For a list of general Events, you can always have a look at the `UploadEvents.ph
* `oneup_uploader.post_upload` Will be dispatched after a file has been uploaded and moved.
* `oneup_uploader.post_persist` The same as `oneup_uploader.post_upload` but will only be dispatched if no `Orphanage` is used.

In case you are using chunked uploads on your frontend, you can listen to:

* `oneup_uploader.post_chunk_upload` Will be dispatched after a chunk has been uploaded (including the last and assembled one)

Moreover this bundles also dispatches some special kind of generic events you can listen to.

* `oneup_uploader.post_upload.{mapping}`
Expand Down
37 changes: 37 additions & 0 deletions Tests/Controller/AbstractChunkedUploadTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@

namespace Oneup\UploaderBundle\Tests\Controller;

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

abstract class AbstractChunkedUploadTest extends AbstractUploadTest
{
Expand Down Expand Up @@ -31,4 +34,38 @@ public function testChunkedUpload()
$this->assertEquals(120, $file->getSize());
}
}

public function testEvents()
{
$endpoint = $this->helper->endpoint($this->getConfigKey());

// prepare listener data
$me = $this;
$chunkCount = 0;
$uploadCount = 0;
$chunkSize = $this->getNextFile(0)->getSize();

for($i = 0; $i < $this->total; $i ++) {
// each time create a new client otherwise the events won't get dispatched
$client = static::createClient();
$dispatcher = $client->getContainer()->get('event_dispatcher');

$dispatcher->addListener(UploadEvents::POST_CHUNK_UPLOAD, function(PostChunkUploadEvent $event) use (&$chunkCount, $chunkSize, &$me) {
++ $chunkCount;

$chunk = $event->getChunk();

$me->assertEquals($chunkSize, $chunk->getSize());
});

$dispatcher->addListener(UploadEvents::POST_UPLOAD, function(Event $event) use (&$uploadCount) {
++ $uploadCount;
});

$client->request('POST', $endpoint, $this->getNextRequestParameters($i), array($this->getNextFile($i)));
}

$this->assertEquals($this->total, $chunkCount);
$this->assertEquals(1, $uploadCount);
}
}
7 changes: 4 additions & 3 deletions UploadEvents.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@

final class UploadEvents
{
const POST_PERSIST = 'oneup_uploader.post_persist';
const POST_UPLOAD = 'oneup_uploader.post_upload';
const VALIDATION = 'oneup_uploader.validation';
const POST_PERSIST = 'oneup_uploader.post_persist';
const POST_UPLOAD = 'oneup_uploader.post_upload';
const POST_CHUNK_UPLOAD = 'oneup_uploader.post_chunk_upload';
const VALIDATION = 'oneup_uploader.validation';
}

0 comments on commit a408548

Please sign in to comment.