Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dispatch event on preview request #2076

Merged
merged 1 commit into from
Nov 18, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion lib/private/Preview/Generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
use OCP\IImage;
use OCP\IPreview;
use OCP\Preview\IProvider;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\EventDispatcher\GenericEvent;

class Generator {

Expand All @@ -43,23 +45,28 @@ class Generator {
private $appData;
/** @var GeneratorHelper */
private $helper;
/** @var EventDispatcherInterface */
private $eventDispatcher;

/**
* @param IConfig $config
* @param IPreview $previewManager
* @param IAppData $appData
* @param GeneratorHelper $helper
* @param EventDispatcherInterface $eventDispatcher
*/
public function __construct(
IConfig $config,
IPreview $previewManager,
IAppData $appData,
GeneratorHelper $helper
GeneratorHelper $helper,
EventDispatcherInterface $eventDispatcher
) {
$this->config = $config;
$this->previewManager = $previewManager;
$this->appData = $appData;
$this->helper = $helper;
$this->eventDispatcher = $eventDispatcher;
}

/**
Expand All @@ -78,6 +85,16 @@ public function __construct(
* @throws NotFoundException
*/
public function getPreview(File $file, $width = -1, $height = -1, $crop = false, $mode = IPreview::MODE_FILL, $mimeType = null) {
$this->eventDispatcher->dispatch(
IPreview::EVENT,
new GenericEvent($file,[
'width' => $width,
'height' => $height,
'crop' => $crop,
'mode' => $mode
])
);

if ($mimeType === null) {
$mimeType = $file->getMimeType();
}
Expand Down
18 changes: 14 additions & 4 deletions lib/private/PreviewManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
use OCP\IConfig;
use OCP\IPreview;
use OCP\Preview\IProvider;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;

class PreviewManager implements IPreview {
/** @var IConfig */
Expand All @@ -46,6 +47,9 @@ class PreviewManager implements IPreview {
/** @var IAppData */
protected $appData;

/** @var EventDispatcherInterface */
protected $eventDispatcher;

/** @var Generator */
private $generator;

Expand All @@ -65,16 +69,21 @@ class PreviewManager implements IPreview {
protected $defaultProviders;

/**
* Constructor
* PreviewManager constructor.
*
* @param \OCP\IConfig $config
* @param IConfig $config
* @param IRootFolder $rootFolder
* @param IAppData $appData
* @param EventDispatcherInterface $eventDispatcher
*/
public function __construct(IConfig $config,
IRootFolder $rootFolder,
IAppData $appData) {
IAppData $appData,
EventDispatcherInterface $eventDispatcher) {
$this->config = $config;
$this->rootFolder = $rootFolder;
$this->appData = $appData;
$this->eventDispatcher = $eventDispatcher;
}

/**
Expand Down Expand Up @@ -165,7 +174,8 @@ public function getPreview(File $file, $width = -1, $height = -1, $crop = false,
$this->appData,
new GeneratorHelper(
$this->rootFolder
)
),
$this->eventDispatcher
);
}

Expand Down
3 changes: 2 additions & 1 deletion lib/private/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,8 @@ public function __construct($webRoot, \OC\Config $config) {
return new PreviewManager(
$c->getConfig(),
$c->getRootFolder(),
$c->getAppDataDir('preview')
$c->getAppDataDir('preview'),
$c->getEventDispatcher()
);
});

Expand Down
5 changes: 5 additions & 0 deletions lib/public/IPreview.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@
*/
interface IPreview {

/**
* @since 9.2.0
*/
const EVENT = self::class . ':' . 'PreviewRequested';

const MODE_FILL = 'fill';
const MODE_COVER = 'cover';

Expand Down
68 changes: 67 additions & 1 deletion tests/lib/Preview/GeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
use OCP\IImage;
use OCP\IPreview;
use OCP\Preview\IProvider;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\EventDispatcher\GenericEvent;

class GeneratorTest extends \Test\TestCase {

Expand All @@ -48,6 +50,9 @@ class GeneratorTest extends \Test\TestCase {
/** @var GeneratorHelper|\PHPUnit_Framework_MockObject_MockObject */
private $helper;

/** @var EventDispatcherInterface|\PHPUnit_Framework_MockObject_MockObject */
private $eventDispatcher;

/** @var Generator */
private $generator;

Expand All @@ -58,12 +63,14 @@ public function setUp() {
$this->previewManager = $this->createMock(IPreview::class);
$this->appData = $this->createMock(IAppData::class);
$this->helper = $this->createMock(GeneratorHelper::class);
$this->eventDispatcher = $this->createMock(EventDispatcherInterface::class);

$this->generator = new Generator(
$this->config,
$this->previewManager,
$this->appData,
$this->helper
$this->helper,
$this->eventDispatcher
);
}

Expand Down Expand Up @@ -96,6 +103,17 @@ public function testGetCachedPreview() {
->with($this->equalTo('128-128.png'))
->willReturn($previewFile);

$this->eventDispatcher->expects($this->once())
->method('dispatch')
->with(
$this->equalTo(IPreview::EVENT),
$this->callback(function(GenericEvent $event) use ($file) {
return $event->getSubject() === $file &&
$event->getArgument('width') === 100 &&
$event->getArgument('height') === 100;
})
);

$result = $this->generator->getPreview($file, 100, 100);
$this->assertSame($previewFile, $result);
}
Expand Down Expand Up @@ -204,6 +222,17 @@ public function testGetNewPreview() {
->method('putContent')
->with('my resized data');

$this->eventDispatcher->expects($this->once())
->method('dispatch')
->with(
$this->equalTo(IPreview::EVENT),
$this->callback(function(GenericEvent $event) use ($file) {
return $event->getSubject() === $file &&
$event->getArgument('width') === 100 &&
$event->getArgument('height') === 100;
})
);

$result = $this->generator->getPreview($file, 100, 100);
$this->assertSame($previewFile, $result);
}
Expand All @@ -217,6 +246,19 @@ public function testInvalidMimeType() {
->with('invalidType')
->willReturn(false);

$this->eventDispatcher->expects($this->once())
->method('dispatch')
->with(
$this->equalTo(IPreview::EVENT),
$this->callback(function(GenericEvent $event) use ($file) {
return $event->getSubject() === $file &&
$event->getArgument('width') === 0 &&
$event->getArgument('height') === 0 &&
$event->getArgument('crop') === true &&
$event->getArgument('mode') === IPreview::MODE_COVER;
})
);

$this->generator->getPreview($file, 0, 0, true, IPreview::MODE_COVER, 'invalidType');
}

Expand All @@ -242,6 +284,17 @@ public function testNoProvider() {
$this->previewManager->method('getProviders')
->willReturn([]);

$this->eventDispatcher->expects($this->once())
->method('dispatch')
->with(
$this->equalTo(IPreview::EVENT),
$this->callback(function(GenericEvent $event) use ($file) {
return $event->getSubject() === $file &&
$event->getArgument('width') === 100 &&
$event->getArgument('height') === 100;
})
);

$this->expectException(NotFoundException::class);
$this->generator->getPreview($file, 100, 100);
}
Expand Down Expand Up @@ -332,6 +385,19 @@ public function testCorrectSize($maxX, $maxY, $reqX, $reqY, $crop, $mode, $expec
->with($this->equalTo($filename))
->willReturn($preview);

$this->eventDispatcher->expects($this->once())
->method('dispatch')
->with(
$this->equalTo(IPreview::EVENT),
$this->callback(function(GenericEvent $event) use ($file, $reqX, $reqY, $crop, $mode) {
return $event->getSubject() === $file &&
$event->getArgument('width') === $reqX &&
$event->getArgument('height') === $reqY &&
$event->getArgument('crop') === $crop &&
$event->getArgument('mode') === $mode;
})
);

$result = $this->generator->getPreview($file, $reqX, $reqY, $crop, $mode);
$this->assertSame($preview, $result);
}
Expand Down