Skip to content

Commit a408548

Browse files
committed
Refactored Event dispatching (BC-break)
* 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.
1 parent 7cf2a6d commit a408548

13 files changed

+154
-38
lines changed

Controller/AbstractChunkedController.php

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,11 @@
44

55
use Symfony\Component\HttpFoundation\Request;
66
use Symfony\Component\HttpFoundation\File\UploadedFile;
7+
8+
use Oneup\UploaderBundle\UploadEvents;
9+
use Oneup\UploaderBundle\Uploader\Response\ResponseInterface;
710
use Oneup\UploaderBundle\Controller\AbstractController;
11+
use Oneup\UploaderBundle\Event\PostChunkUploadEvent;
812

913
abstract class AbstractChunkedController extends AbstractController
1014
{
@@ -35,7 +39,7 @@ abstract protected function parseChunkedRequest(Request $request);
3539
*
3640
* @param file The uploaded chunk.
3741
*/
38-
protected function handleChunkedUpload(UploadedFile $file)
42+
protected function handleChunkedUpload(UploadedFile $file, ResponseInterface $response, Request $request)
3943
{
4044
// get basic container stuff
4145
$request = $this->container->get('request');
@@ -47,12 +51,13 @@ protected function handleChunkedUpload(UploadedFile $file)
4751
// get information about this chunked request
4852
list($last, $uuid, $index, $orig) = $this->parseChunkedRequest($request);
4953

50-
$uploaded = $chunkManager->addChunk($uuid, $index, $file, $orig);
54+
$chunk = $chunkManager->addChunk($uuid, $index, $file, $orig);
55+
56+
$this->dispatchChunkEvents($chunk, $response, $request, $last);
5157

5258
// if all chunks collected and stored, proceed
5359
// with reassembling the parts
54-
if($last)
55-
{
60+
if ($last) {
5661
// we'll take the first chunk and append the others to it
5762
// this way we don't need another file in temporary space for assembling
5863
$chunks = $chunkManager->getChunks($uuid);
@@ -66,11 +71,22 @@ protected function handleChunkedUpload(UploadedFile $file)
6671

6772
// validate this entity and upload on success
6873
$this->validate($uploadedFile);
69-
$uploaded = $this->handleUpload($uploadedFile);
74+
$uploaded = $this->handleUpload($uploadedFile, $response, $request);
7075

7176
$chunkManager->cleanup($path);
7277
}
78+
}
79+
80+
/**
81+
* This function is a helper function which dispatches post chunk upload event.
82+
*/
83+
protected function dispatchChunkEvents($uploaded, ResponseInterface $response, Request $request, $isLast)
84+
{
85+
$dispatcher = $this->container->get('event_dispatcher');
7386

74-
return $uploaded;
87+
// dispatch post upload event (both the specific and the general)
88+
$postUploadEvent = new PostChunkUploadEvent($uploaded, $response, $request, $isLast, $this->type, $this->config);
89+
$dispatcher->dispatch(UploadEvents::POST_CHUNK_UPLOAD, $postUploadEvent);
90+
$dispatcher->dispatch(sprintf('%s.%s', UploadEvents::POST_CHUNK_UPLOAD, $this->type), $postUploadEvent);
7591
}
7692
}

Controller/AbstractController.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ abstract public function upload();
4444
* @param UploadedFile The file to upload
4545
* @return File the actual file
4646
*/
47-
protected function handleUpload(UploadedFile $file)
47+
protected function handleUpload(UploadedFile $file, ResponseInterface $response, Request $request)
4848
{
4949
$this->validate($file);
5050

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

58-
return $uploaded;
58+
$this->dispatchEvents($uploaded, $response, $request);
5959
}
6060

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

74-
if(!$this->config['use_orphanage'])
75-
{
74+
if (!$this->config['use_orphanage']) {
7675
// dispatch post persist event (both the specific and the general)
7776
$postPersistEvent = new PostPersistEvent($uploaded, $response, $request, $this->type, $this->config);
7877
$dispatcher->dispatch(UploadEvents::POST_PERSIST, $postPersistEvent);

Controller/BlueimpController.php

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,12 @@ public function upload()
2323
{
2424
$file = $file[0];
2525

26-
try
27-
{
28-
$uploaded = $chunked ? $this->handleChunkedUpload($file) : $this->handleUpload($file);
29-
30-
// dispatch POST_PERSIST AND POST_UPLOAD events
31-
$this->dispatchEvents($uploaded, $response, $request);
32-
}
33-
catch(UploadException $e)
34-
{
26+
try {
27+
$chunked ?
28+
$this->handleChunkedUpload($file, $response, $request) :
29+
$this->handleUpload($file, $response, $request)
30+
;
31+
} catch(UploadException $e) {
3532
// return nothing
3633
return new JsonResponse(array());
3734
}

Controller/FancyUploadController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public function upload()
2020
{
2121
try
2222
{
23-
$uploaded = $this->handleUpload($file);
23+
$uploaded = $this->handleUpload($file, $response, $request);
2424

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

Controller/FineUploaderController.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,10 @@ public function upload()
2525
{
2626
try
2727
{
28-
$uploaded = $chunked ? $this->handleChunkedUpload($file) : $this->handleUpload($file);
29-
30-
// dispatch POST_PERSIST AND POST_UPLOAD events
31-
$this->dispatchEvents($uploaded, $response, $request);
28+
$chunked ?
29+
$this->handleChunkedUpload($file, $response, $request) :
30+
$this->handleUpload($file, $response, $request)
31+
;
3232
}
3333
catch(UploadException $e)
3434
{

Controller/MooUploadController.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,18 +33,18 @@ public function upload()
3333

3434
try
3535
{
36-
$uploaded = $chunked ? $this->handleChunkedUpload($file) : $this->handleUpload($file);
37-
3836
// fill response object
3937
$response = $this->response;
4038

4139
$response->setId($headers->get('x-file-id'));
4240
$response->setSize($headers->get('content-length'));
4341
$response->setName($headers->get('x-file-name'));
4442
$response->setUploadedName($uploadFileName);
45-
46-
// dispatch POST_PERSIST AND POST_UPLOAD events
47-
$this->dispatchEvents($uploaded, $response, $request);
43+
44+
$chunked ?
45+
$this->handleChunkedUpload($file, $response, $request) :
46+
$this->handleUpload($file, $response, $request)
47+
;
4848
}
4949
catch(UploadException $e)
5050
{

Controller/PluploadController.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,10 @@ public function upload()
2323
{
2424
try
2525
{
26-
$uploaded = $chunked ? $this->handleChunkedUpload($file) : $this->handleUpload($file);
27-
28-
// dispatch POST_PERSIST AND POST_UPLOAD events
29-
$this->dispatchEvents($uploaded, $response, $request);
26+
$chunked ?
27+
$this->handleChunkedUpload($file, $response, $request) :
28+
$this->handleUpload($file, $response, $request)
29+
;
3030
}
3131
catch(UploadException $e)
3232
{

Controller/UploadifyController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public function upload()
2020
{
2121
try
2222
{
23-
$uploaded = $this->handleUpload($file);
23+
$uploaded = $this->handleUpload($file, $response, $request);
2424

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

Controller/YUI3Controller.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public function upload()
2020
{
2121
try
2222
{
23-
$uploaded = $this->handleUpload($file);
23+
$uploaded = $this->handleUpload($file, $response, $request);
2424

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

Event/PostChunkUploadEvent.php

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?php
2+
3+
namespace Oneup\UploaderBundle\Event;
4+
5+
use Symfony\Component\EventDispatcher\Event;
6+
use Symfony\Component\HttpFoundation\Request;
7+
use Oneup\UploaderBundle\Uploader\Response\ResponseInterface;
8+
9+
class PostChunkUploadEvent extends Event
10+
{
11+
protected $file;
12+
protected $request;
13+
protected $type;
14+
protected $response;
15+
protected $config;
16+
protected $isLast;
17+
18+
public function __construct($chunk, ResponseInterface $response, Request $request, $isLast, $type, array $config)
19+
{
20+
$this->chunk = $chunk;
21+
$this->request = $request;
22+
$this->response = $response;
23+
$this->isLast = $isLast;
24+
$this->type = $type;
25+
$this->config = $config;
26+
}
27+
28+
public function getChunk()
29+
{
30+
return $this->chunk;
31+
}
32+
33+
public function getRequest()
34+
{
35+
return $this->request;
36+
}
37+
38+
public function getType()
39+
{
40+
return $this->type;
41+
}
42+
43+
public function getResponse()
44+
{
45+
return $this->response;
46+
}
47+
48+
public function getConfig()
49+
{
50+
return $this->config;
51+
}
52+
53+
public function getIsLast()
54+
{
55+
return $this->isLast;
56+
}
57+
58+
public function isLast()
59+
{
60+
return $this->isLast;
61+
}
62+
}

Resources/doc/events.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ For a list of general Events, you can always have a look at the `UploadEvents.ph
66
* `oneup_uploader.post_upload` Will be dispatched after a file has been uploaded and moved.
77
* `oneup_uploader.post_persist` The same as `oneup_uploader.post_upload` but will only be dispatched if no `Orphanage` is used.
88

9+
In case you are using chunked uploads on your frontend, you can listen to:
10+
11+
* `oneup_uploader.post_chunk_upload` Will be dispatched after a chunk has been uploaded (including the last and assembled one)
12+
913
Moreover this bundles also dispatches some special kind of generic events you can listen to.
1014

1115
* `oneup_uploader.post_upload.{mapping}`

Tests/Controller/AbstractChunkedUploadTest.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@
22

33
namespace Oneup\UploaderBundle\Tests\Controller;
44

5+
use Symfony\Component\EventDispatcher\Event;
56
use Oneup\UploaderBundle\Tests\Controller\AbstractUploadTest;
7+
use Oneup\UploaderBundle\UploadEvents;
8+
use Oneup\UploaderBundle\Event\PostChunkUploadEvent;
69

710
abstract class AbstractChunkedUploadTest extends AbstractUploadTest
811
{
@@ -31,4 +34,38 @@ public function testChunkedUpload()
3134
$this->assertEquals(120, $file->getSize());
3235
}
3336
}
37+
38+
public function testEvents()
39+
{
40+
$endpoint = $this->helper->endpoint($this->getConfigKey());
41+
42+
// prepare listener data
43+
$me = $this;
44+
$chunkCount = 0;
45+
$uploadCount = 0;
46+
$chunkSize = $this->getNextFile(0)->getSize();
47+
48+
for($i = 0; $i < $this->total; $i ++) {
49+
// each time create a new client otherwise the events won't get dispatched
50+
$client = static::createClient();
51+
$dispatcher = $client->getContainer()->get('event_dispatcher');
52+
53+
$dispatcher->addListener(UploadEvents::POST_CHUNK_UPLOAD, function(PostChunkUploadEvent $event) use (&$chunkCount, $chunkSize, &$me) {
54+
++ $chunkCount;
55+
56+
$chunk = $event->getChunk();
57+
58+
$me->assertEquals($chunkSize, $chunk->getSize());
59+
});
60+
61+
$dispatcher->addListener(UploadEvents::POST_UPLOAD, function(Event $event) use (&$uploadCount) {
62+
++ $uploadCount;
63+
});
64+
65+
$client->request('POST', $endpoint, $this->getNextRequestParameters($i), array($this->getNextFile($i)));
66+
}
67+
68+
$this->assertEquals($this->total, $chunkCount);
69+
$this->assertEquals(1, $uploadCount);
70+
}
3471
}

UploadEvents.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44

55
final class UploadEvents
66
{
7-
const POST_PERSIST = 'oneup_uploader.post_persist';
8-
const POST_UPLOAD = 'oneup_uploader.post_upload';
9-
const VALIDATION = 'oneup_uploader.validation';
7+
const POST_PERSIST = 'oneup_uploader.post_persist';
8+
const POST_UPLOAD = 'oneup_uploader.post_upload';
9+
const POST_CHUNK_UPLOAD = 'oneup_uploader.post_chunk_upload';
10+
const VALIDATION = 'oneup_uploader.validation';
1011
}

0 commit comments

Comments
 (0)