Skip to content

Commit 72d4626

Browse files
committed
Added a pre upload event.
This event will be given an instance of UploadedFile instead of a moved file. Addresses #21.
1 parent d2d4d76 commit 72d4626

File tree

5 files changed

+90
-6
lines changed

5 files changed

+90
-6
lines changed

Controller/AbstractController.php

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Symfony\Component\HttpFoundation\Request;
99

1010
use Oneup\UploaderBundle\UploadEvents;
11+
use Oneup\UploaderBundle\Event\PreUploadEvent;
1112
use Oneup\UploaderBundle\Event\PostPersistEvent;
1213
use Oneup\UploaderBundle\Event\PostUploadEvent;
1314
use Oneup\UploaderBundle\Event\ValidationEvent;
@@ -42,21 +43,38 @@ abstract public function upload();
4243
* @param UploadedFile The file to upload
4344
* @param response A response object.
4445
* @param request The request object.
45-
*
46-
* @return File the actual file
4746
*/
4847
protected function handleUpload(UploadedFile $file, ResponseInterface $response, Request $request)
4948
{
5049
$this->validate($file);
5150

51+
$this->dispatchPreUploadEvent($file, $response, $request);
52+
5253
// no error happend, proceed
5354
$namer = $this->container->get($this->config['namer']);
5455
$name = $namer->name($file);
5556

5657
// perform the real upload
5758
$uploaded = $this->storage->upload($file, $name);
5859

59-
$this->dispatchEvents($uploaded, $response, $request);
60+
$this->dispatchPostEvents($uploaded, $response, $request);
61+
}
62+
63+
/**
64+
* This function is a helper function which dispatches pre upload event
65+
*
66+
* @param uploaded The uploaded file.
67+
* @param response A response object.
68+
* @param request The request object.
69+
*/
70+
protected function dispatchPreUploadEvent(UploadedFile $uploaded, ResponseInterface $response, Request $request)
71+
{
72+
$dispatcher = $this->container->get('event_dispatcher');
73+
74+
// dispatch pre upload event (both the specific and the general)
75+
$postUploadEvent = new PreUploadEvent($uploaded, $response, $request, $this->type, $this->config);
76+
$dispatcher->dispatch(UploadEvents::PRE_UPLOAD, $postUploadEvent);
77+
$dispatcher->dispatch(sprintf('%s.%s', UploadEvents::PRE_UPLOAD, $this->type), $postUploadEvent);
6078
}
6179

6280
/**
@@ -67,7 +85,7 @@ protected function handleUpload(UploadedFile $file, ResponseInterface $response,
6785
* @param response A response object.
6886
* @param request The request object.
6987
*/
70-
protected function dispatchEvents($uploaded, ResponseInterface $response, Request $request)
88+
protected function dispatchPostEvents($uploaded, ResponseInterface $response, Request $request)
7189
{
7290
$dispatcher = $this->container->get('event_dispatcher');
7391

Event/PreUploadEvent.php

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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 PreUploadEvent extends Event
10+
{
11+
protected $file;
12+
protected $request;
13+
protected $type;
14+
protected $response;
15+
protected $config;
16+
17+
public function __construct($file, ResponseInterface $response, Request $request, $type, array $config)
18+
{
19+
$this->file = $file;
20+
$this->request = $request;
21+
$this->response = $response;
22+
$this->type = $type;
23+
$this->config = $config;
24+
}
25+
26+
public function getFile()
27+
{
28+
return $this->file;
29+
}
30+
31+
public function getRequest()
32+
{
33+
return $this->request;
34+
}
35+
36+
public function getType()
37+
{
38+
return $this->type;
39+
}
40+
41+
public function getResponse()
42+
{
43+
return $this->response;
44+
}
45+
46+
public function getConfig()
47+
{
48+
return $this->config;
49+
}
50+
}

Resources/doc/events.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ OneupUploaderBundle Events
33

44
For a list of general Events, you can always have a look at the `UploadEvents.php` file in the root of this bundle.
55

6+
* `oneup_uploader.pre_upload` Will be fired after validation but before naming and moving the uploaded file.
67
* `oneup_uploader.post_upload` Will be dispatched after a file has been uploaded and moved.
78
* `oneup_uploader.post_persist` The same as `oneup_uploader.post_upload` but will only be dispatched if no `Orphanage` is used.
89

@@ -12,8 +13,10 @@ In case you are using chunked uploads on your frontend, you can listen to:
1213

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

16+
* `oneup_uploader.pre_upload.{mapping}`
1517
* `oneup_uploader.post_upload.{mapping}`
1618
* `oneup_uploader.post_persist.{mapping}`
19+
* `oneup_uploader.post_chunk_upload.{mapping}`
1720

1821
The `{mapping}` part is the key of your configured mapping. The examples in this documentation always uses the mapping key `gallery`. So the dispatched event would be called `oneup_uploader.post_upload.gallery`.
1922
Using these generic events can save you some time and coding lines, as you don't have to check for the correct type in the `EventListener`.

Tests/Controller/AbstractUploadTest.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Oneup\UploaderBundle\Tests\Controller\AbstractControllerTest;
66
use Oneup\UploaderBundle\UploadEvents;
7+
use Oneup\UploaderBundle\Event\PreUploadEvent;
78
use Oneup\UploaderBundle\Event\PostUploadEvent;
89

910
abstract class AbstractUploadTest extends AbstractControllerTest
@@ -47,9 +48,19 @@ public function testEvents()
4748
// event data
4849
$me = $this;
4950
$uploadCount = 0;
51+
$preValidation = 1;
52+
53+
$dispatcher->addListener(UploadEvents::PRE_UPLOAD, function(PreUploadEvent $event) use (&$uploadCount, &$me, &$preValidation) {
54+
$preValidation -= 2;
5055

51-
$dispatcher->addListener(UploadEvents::POST_UPLOAD, function(PostUploadEvent $event) use (&$uploadCount, &$me) {
56+
$file = $event->getFile();
57+
58+
$me->assertInstanceOf('Symfony\Component\HttpFoundation\File\UploadedFile', $file);
59+
});
60+
61+
$dispatcher->addListener(UploadEvents::POST_UPLOAD, function(PostUploadEvent $event) use (&$uploadCount, &$me, &$preValidation) {
5262
++ $uploadCount;
63+
$preValidation *= -1;
5364

5465
$file = $event->getFile();
5566

@@ -61,5 +72,6 @@ public function testEvents()
6172

6273
$this->assertCount(1, $this->getUploadedFiles());
6374
$this->assertEquals($uploadCount, count($this->getUploadedFiles()));
75+
$this->assertEquals(1, $preValidation);
6476
}
6577
}

UploadEvents.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@
44

55
final class UploadEvents
66
{
7-
const POST_PERSIST = 'oneup_uploader.post_persist';
7+
const PRE_UPLOAD = 'oneup_uploader.pre_upload';
88
const POST_UPLOAD = 'oneup_uploader.post_upload';
9+
const POST_PERSIST = 'oneup_uploader.post_persist';
910
const POST_CHUNK_UPLOAD = 'oneup_uploader.post_chunk_upload';
1011
const VALIDATION = 'oneup_uploader.validation';
1112
}

0 commit comments

Comments
 (0)