Dispatch event on preview request

Fixes: #73

Signed-off-by: Roeland Jago Douma <roeland@famdouma.nl>
This commit is contained in:
Roeland Jago Douma 2016-11-10 14:04:59 +01:00
parent cfda17d8f3
commit 99ada40df4
No known key found for this signature in database
GPG Key ID: 1E152838F164D13B
5 changed files with 106 additions and 7 deletions

View File

@ -32,6 +32,8 @@ use OCP\IConfig;
use OCP\IImage; use OCP\IImage;
use OCP\IPreview; use OCP\IPreview;
use OCP\Preview\IProvider; use OCP\Preview\IProvider;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\EventDispatcher\GenericEvent;
class Generator { class Generator {
@ -43,23 +45,28 @@ class Generator {
private $appData; private $appData;
/** @var GeneratorHelper */ /** @var GeneratorHelper */
private $helper; private $helper;
/** @var EventDispatcherInterface */
private $eventDispatcher;
/** /**
* @param IConfig $config * @param IConfig $config
* @param IPreview $previewManager * @param IPreview $previewManager
* @param IAppData $appData * @param IAppData $appData
* @param GeneratorHelper $helper * @param GeneratorHelper $helper
* @param EventDispatcherInterface $eventDispatcher
*/ */
public function __construct( public function __construct(
IConfig $config, IConfig $config,
IPreview $previewManager, IPreview $previewManager,
IAppData $appData, IAppData $appData,
GeneratorHelper $helper GeneratorHelper $helper,
EventDispatcherInterface $eventDispatcher
) { ) {
$this->config = $config; $this->config = $config;
$this->previewManager = $previewManager; $this->previewManager = $previewManager;
$this->appData = $appData; $this->appData = $appData;
$this->helper = $helper; $this->helper = $helper;
$this->eventDispatcher = $eventDispatcher;
} }
/** /**
@ -78,6 +85,16 @@ class Generator {
* @throws NotFoundException * @throws NotFoundException
*/ */
public function getPreview(File $file, $width = -1, $height = -1, $crop = false, $mode = IPreview::MODE_FILL, $mimeType = null) { 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) { if ($mimeType === null) {
$mimeType = $file->getMimeType(); $mimeType = $file->getMimeType();
} }

View File

@ -35,6 +35,7 @@ use OCP\Files\SimpleFS\ISimpleFile;
use OCP\IConfig; use OCP\IConfig;
use OCP\IPreview; use OCP\IPreview;
use OCP\Preview\IProvider; use OCP\Preview\IProvider;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
class PreviewManager implements IPreview { class PreviewManager implements IPreview {
/** @var IConfig */ /** @var IConfig */
@ -46,6 +47,9 @@ class PreviewManager implements IPreview {
/** @var IAppData */ /** @var IAppData */
protected $appData; protected $appData;
/** @var EventDispatcherInterface */
protected $eventDispatcher;
/** @var Generator */ /** @var Generator */
private $generator; private $generator;
@ -65,16 +69,21 @@ class PreviewManager implements IPreview {
protected $defaultProviders; 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, public function __construct(IConfig $config,
IRootFolder $rootFolder, IRootFolder $rootFolder,
IAppData $appData) { IAppData $appData,
EventDispatcherInterface $eventDispatcher) {
$this->config = $config; $this->config = $config;
$this->rootFolder = $rootFolder; $this->rootFolder = $rootFolder;
$this->appData = $appData; $this->appData = $appData;
$this->eventDispatcher = $eventDispatcher;
} }
/** /**
@ -165,7 +174,8 @@ class PreviewManager implements IPreview {
$this->appData, $this->appData,
new GeneratorHelper( new GeneratorHelper(
$this->rootFolder $this->rootFolder
) ),
$this->eventDispatcher
); );
} }

View File

@ -121,7 +121,8 @@ class Server extends ServerContainer implements IServerContainer {
return new PreviewManager( return new PreviewManager(
$c->getConfig(), $c->getConfig(),
$c->getRootFolder(), $c->getRootFolder(),
$c->getAppDataDir('preview') $c->getAppDataDir('preview'),
$c->getEventDispatcher()
); );
}); });

View File

@ -43,6 +43,11 @@ use OCP\Files\NotFoundException;
*/ */
interface IPreview { interface IPreview {
/**
* @since 9.2.0
*/
const EVENT = self::class . ':' . 'PreviewRequested';
const MODE_FILL = 'fill'; const MODE_FILL = 'fill';
const MODE_COVER = 'cover'; const MODE_COVER = 'cover';

View File

@ -33,6 +33,8 @@ use OCP\IConfig;
use OCP\IImage; use OCP\IImage;
use OCP\IPreview; use OCP\IPreview;
use OCP\Preview\IProvider; use OCP\Preview\IProvider;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\EventDispatcher\GenericEvent;
class GeneratorTest extends \Test\TestCase { class GeneratorTest extends \Test\TestCase {
@ -48,6 +50,9 @@ class GeneratorTest extends \Test\TestCase {
/** @var GeneratorHelper|\PHPUnit_Framework_MockObject_MockObject */ /** @var GeneratorHelper|\PHPUnit_Framework_MockObject_MockObject */
private $helper; private $helper;
/** @var EventDispatcherInterface|\PHPUnit_Framework_MockObject_MockObject */
private $eventDispatcher;
/** @var Generator */ /** @var Generator */
private $generator; private $generator;
@ -58,12 +63,14 @@ class GeneratorTest extends \Test\TestCase {
$this->previewManager = $this->createMock(IPreview::class); $this->previewManager = $this->createMock(IPreview::class);
$this->appData = $this->createMock(IAppData::class); $this->appData = $this->createMock(IAppData::class);
$this->helper = $this->createMock(GeneratorHelper::class); $this->helper = $this->createMock(GeneratorHelper::class);
$this->eventDispatcher = $this->createMock(EventDispatcherInterface::class);
$this->generator = new Generator( $this->generator = new Generator(
$this->config, $this->config,
$this->previewManager, $this->previewManager,
$this->appData, $this->appData,
$this->helper $this->helper,
$this->eventDispatcher
); );
} }
@ -96,6 +103,17 @@ class GeneratorTest extends \Test\TestCase {
->with($this->equalTo('128-128.png')) ->with($this->equalTo('128-128.png'))
->willReturn($previewFile); ->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); $result = $this->generator->getPreview($file, 100, 100);
$this->assertSame($previewFile, $result); $this->assertSame($previewFile, $result);
} }
@ -204,6 +222,17 @@ class GeneratorTest extends \Test\TestCase {
->method('putContent') ->method('putContent')
->with('my resized data'); ->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); $result = $this->generator->getPreview($file, 100, 100);
$this->assertSame($previewFile, $result); $this->assertSame($previewFile, $result);
} }
@ -217,6 +246,19 @@ class GeneratorTest extends \Test\TestCase {
->with('invalidType') ->with('invalidType')
->willReturn(false); ->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'); $this->generator->getPreview($file, 0, 0, true, IPreview::MODE_COVER, 'invalidType');
} }
@ -242,6 +284,17 @@ class GeneratorTest extends \Test\TestCase {
$this->previewManager->method('getProviders') $this->previewManager->method('getProviders')
->willReturn([]); ->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->expectException(NotFoundException::class);
$this->generator->getPreview($file, 100, 100); $this->generator->getPreview($file, 100, 100);
} }
@ -332,6 +385,19 @@ class GeneratorTest extends \Test\TestCase {
->with($this->equalTo($filename)) ->with($this->equalTo($filename))
->willReturn($preview); ->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); $result = $this->generator->getPreview($file, $reqX, $reqY, $crop, $mode);
$this->assertSame($preview, $result); $this->assertSame($preview, $result);
} }