From 6dc2b162c588fa9f9c2ff1989f489cf8b34dab8a Mon Sep 17 00:00:00 2001 From: =?utf8?q?Julius=20H=C3=A4rtl?= Date: Tue, 25 Oct 2022 12:53:10 +0200 Subject: [PATCH] Emit typed event when preview is requested MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Julius Härtl --- lib/composer/composer/autoload_classmap.php | 1 + lib/composer/composer/autoload_static.php | 1 + lib/private/Preview/Generator.php | 21 ++++---- lib/private/PreviewManager.php | 17 +++++-- lib/private/Server.php | 1 + .../Preview/BeforePreviewFetchedEvent.php | 51 +++++++++++++++++++ tests/lib/Preview/GeneratorTest.php | 47 ++++++++++++++--- 7 files changed, 119 insertions(+), 20 deletions(-) create mode 100644 lib/public/Preview/BeforePreviewFetchedEvent.php diff --git a/lib/composer/composer/autoload_classmap.php b/lib/composer/composer/autoload_classmap.php index 7361d59c0c8..9fb76646f2b 100644 --- a/lib/composer/composer/autoload_classmap.php +++ b/lib/composer/composer/autoload_classmap.php @@ -474,6 +474,7 @@ return array( 'OCP\\Notification\\INotifier' => $baseDir . '/lib/public/Notification/INotifier.php', 'OCP\\OCS\\IDiscoveryService' => $baseDir . '/lib/public/OCS/IDiscoveryService.php', 'OCP\\PreConditionNotMetException' => $baseDir . '/lib/public/PreConditionNotMetException.php', + 'OCP\\Preview\\BeforePreviewFetchedEvent' => $baseDir . '/lib/public/Preview/BeforePreviewFetchedEvent.php', 'OCP\\Preview\\IProvider' => $baseDir . '/lib/public/Preview/IProvider.php', 'OCP\\Preview\\IProviderV2' => $baseDir . '/lib/public/Preview/IProviderV2.php', 'OCP\\Preview\\IVersionedPreviewFile' => $baseDir . '/lib/public/Preview/IVersionedPreviewFile.php', diff --git a/lib/composer/composer/autoload_static.php b/lib/composer/composer/autoload_static.php index c3d8e809cbc..778168316da 100644 --- a/lib/composer/composer/autoload_static.php +++ b/lib/composer/composer/autoload_static.php @@ -503,6 +503,7 @@ class ComposerStaticInit53792487c5a8370acc0b06b1a864ff4c 'OCP\\Notification\\INotifier' => __DIR__ . '/../../..' . '/lib/public/Notification/INotifier.php', 'OCP\\OCS\\IDiscoveryService' => __DIR__ . '/../../..' . '/lib/public/OCS/IDiscoveryService.php', 'OCP\\PreConditionNotMetException' => __DIR__ . '/../../..' . '/lib/public/PreConditionNotMetException.php', + 'OCP\\Preview\\BeforePreviewFetchedEvent' => __DIR__ . '/../../..' . '/lib/public/Preview/BeforePreviewFetchedEvent.php', 'OCP\\Preview\\IProvider' => __DIR__ . '/../../..' . '/lib/public/Preview/IProvider.php', 'OCP\\Preview\\IProviderV2' => __DIR__ . '/../../..' . '/lib/public/Preview/IProviderV2.php', 'OCP\\Preview\\IVersionedPreviewFile' => __DIR__ . '/../../..' . '/lib/public/Preview/IVersionedPreviewFile.php', diff --git a/lib/private/Preview/Generator.php b/lib/private/Preview/Generator.php index 2edea868ed0..20a39380568 100644 --- a/lib/private/Preview/Generator.php +++ b/lib/private/Preview/Generator.php @@ -29,6 +29,7 @@ */ namespace OC\Preview; +use OCP\EventDispatcher\IEventDispatcher; use OCP\Files\File; use OCP\Files\IAppData; use OCP\Files\InvalidPathException; @@ -40,6 +41,7 @@ use OCP\IConfig; use OCP\IImage; use OCP\IPreview; use OCP\IStreamImage; +use OCP\Preview\BeforePreviewFetchedEvent; use OCP\Preview\IProviderV2; use OCP\Preview\IVersionedPreviewFile; use Symfony\Component\EventDispatcher\EventDispatcherInterface; @@ -56,26 +58,23 @@ class Generator { /** @var GeneratorHelper */ private $helper; /** @var EventDispatcherInterface */ + private $legacyEventDispatcher; + /** @var IEventDispatcher */ 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, - EventDispatcherInterface $eventDispatcher + EventDispatcherInterface $legacyEventDispatcher, + IEventDispatcher $eventDispatcher ) { $this->config = $config; $this->previewManager = $previewManager; $this->appData = $appData; $this->helper = $helper; + $this->legacyEventDispatcher = $legacyEventDispatcher; $this->eventDispatcher = $eventDispatcher; } @@ -102,10 +101,14 @@ class Generator { 'crop' => $crop, 'mode' => $mode, ]; - $this->eventDispatcher->dispatch( + + $this->legacyEventDispatcher->dispatch( IPreview::EVENT, new GenericEvent($file, $specification) ); + $this->eventDispatcher->dispatchTyped(new BeforePreviewFetchedEvent( + $file + )); // since we only ask for one preview, and the generate method return the last one it created, it returns the one we want return $this->generatePreviews($file, [$specification], $mimeType); diff --git a/lib/private/PreviewManager.php b/lib/private/PreviewManager.php index 6c17dd58b4b..c347ceafb92 100644 --- a/lib/private/PreviewManager.php +++ b/lib/private/PreviewManager.php @@ -34,6 +34,7 @@ use OC\AppFramework\Bootstrap\Coordinator; use OC\Preview\Generator; use OC\Preview\GeneratorHelper; use OCP\AppFramework\QueryException; +use OCP\EventDispatcher\IEventDispatcher; use OCP\Files\File; use OCP\Files\IAppData; use OCP\Files\IRootFolder; @@ -56,9 +57,12 @@ class PreviewManager implements IPreview { /** @var IAppData */ protected $appData; - /** @var EventDispatcherInterface */ + /** @var IEventDispatcher */ protected $eventDispatcher; + /** @var EventDispatcherInterface */ + protected $legacyEventDispatcher; + /** @var Generator */ private $generator; @@ -103,13 +107,18 @@ class PreviewManager implements IPreview { * @param IConfig $config * @param IRootFolder $rootFolder * @param IAppData $appData - * @param EventDispatcherInterface $eventDispatcher + * @param IEventDispatcher $eventDispatcher + * @param EventDispatcherInterface $legacyEventDispatcher + * @param GeneratorHelper $helper * @param string $userId + * @param Coordinator $bootstrapCoordinator + * @param IServerContainer $container */ public function __construct(IConfig $config, IRootFolder $rootFolder, IAppData $appData, - EventDispatcherInterface $eventDispatcher, + IEventDispatcher $eventDispatcher, + EventDispatcherInterface $legacyEventDispatcher, GeneratorHelper $helper, $userId, Coordinator $bootstrapCoordinator, @@ -118,6 +127,7 @@ class PreviewManager implements IPreview { $this->rootFolder = $rootFolder; $this->appData = $appData; $this->eventDispatcher = $eventDispatcher; + $this->legacyEventDispatcher = $legacyEventDispatcher; $this->helper = $helper; $this->userId = $userId; $this->bootstrapCoordinator = $bootstrapCoordinator; @@ -185,6 +195,7 @@ class PreviewManager implements IPreview { $this->rootFolder, $this->config ), + $this->legacyEventDispatcher, $this->eventDispatcher ); } diff --git a/lib/private/Server.php b/lib/private/Server.php index e70d9d2a7b2..0d67012e70d 100644 --- a/lib/private/Server.php +++ b/lib/private/Server.php @@ -328,6 +328,7 @@ class Server extends ServerContainer implements IServerContainer { $c->get(IRootFolder::class), $c->get(SystemConfig::class) ), + $c->get(IEventDispatcher::class), $c->get(SymfonyAdapter::class), $c->get(GeneratorHelper::class), $c->get(ISession::class)->get('user_id'), diff --git a/lib/public/Preview/BeforePreviewFetchedEvent.php b/lib/public/Preview/BeforePreviewFetchedEvent.php new file mode 100644 index 00000000000..37da63b95a1 --- /dev/null +++ b/lib/public/Preview/BeforePreviewFetchedEvent.php @@ -0,0 +1,51 @@ + + * + * @author Julius Härtl + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + + +namespace OCP\Preview; + +use OCP\Files\Node; + +/** + * @since 25.0.1 + */ +class BeforePreviewFetchedEvent extends \OCP\EventDispatcher\Event { + private Node $node; + + /** + * @since 25.0.1 + */ + public function __construct(Node $node) { + parent::__construct(); + $this->node = $node; + } + + /** + * @since 25.0.1 + */ + public function getNode(): Node { + return $this->node; + } +} diff --git a/tests/lib/Preview/GeneratorTest.php b/tests/lib/Preview/GeneratorTest.php index 43f5c1e0d36..856d229681e 100644 --- a/tests/lib/Preview/GeneratorTest.php +++ b/tests/lib/Preview/GeneratorTest.php @@ -25,6 +25,7 @@ namespace Test\Preview; use OC\Preview\Generator; use OC\Preview\GeneratorHelper; +use OCP\EventDispatcher\IEventDispatcher; use OCP\Files\File; use OCP\Files\IAppData; use OCP\Files\NotFoundException; @@ -32,6 +33,7 @@ use OCP\Files\SimpleFS\ISimpleFile; use OCP\Files\SimpleFS\ISimpleFolder; use OCP\IConfig; use OCP\IPreview; +use OCP\Preview\BeforePreviewFetchedEvent; use OCP\Preview\IProviderV2; use Symfony\Component\EventDispatcher\EventDispatcherInterface; use Symfony\Component\EventDispatcher\GenericEvent; @@ -50,9 +52,12 @@ class GeneratorTest extends \Test\TestCase { /** @var GeneratorHelper|\PHPUnit\Framework\MockObject\MockObject */ private $helper; - /** @var EventDispatcherInterface|\PHPUnit\Framework\MockObject\MockObject */ + /** @var IEventDispatcher|\PHPUnit\Framework\MockObject\MockObject */ private $eventDispatcher; + /** @var EventDispatcherInterface|\PHPUnit\Framework\MockObject\MockObject */ + private $legacyEventDispatcher; + /** @var Generator */ private $generator; @@ -63,13 +68,15 @@ class GeneratorTest extends \Test\TestCase { $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->eventDispatcher = $this->createMock(IEventDispatcher::class); + $this->legacyEventDispatcher = $this->createMock(EventDispatcherInterface::class); $this->generator = new Generator( $this->config, $this->previewManager, $this->appData, $this->helper, + $this->legacyEventDispatcher, $this->eventDispatcher ); } @@ -107,7 +114,7 @@ class GeneratorTest extends \Test\TestCase { ->with($this->equalTo('256-256.png')) ->willReturn($previewFile); - $this->eventDispatcher->expects($this->once()) + $this->legacyEventDispatcher->expects($this->once()) ->method('dispatch') ->with( $this->equalTo(IPreview::EVENT), @@ -118,6 +125,10 @@ class GeneratorTest extends \Test\TestCase { }) ); + $this->eventDispatcher->expects($this->once()) + ->method('dispatchTyped') + ->with(new BeforePreviewFetchedEvent($file)); + $result = $this->generator->getPreview($file, 100, 100); $this->assertSame($previewFile, $result); } @@ -235,7 +246,7 @@ class GeneratorTest extends \Test\TestCase { ->method('putContent') ->with('my resized data'); - $this->eventDispatcher->expects($this->once()) + $this->legacyEventDispatcher->expects($this->once()) ->method('dispatch') ->with( $this->equalTo(IPreview::EVENT), @@ -246,6 +257,10 @@ class GeneratorTest extends \Test\TestCase { }) ); + $this->eventDispatcher->expects($this->once()) + ->method('dispatchTyped') + ->with(new BeforePreviewFetchedEvent($file)); + $result = $this->generator->getPreview($file, 100, 100); $this->assertSame($previewFile, $result); } @@ -281,7 +296,7 @@ class GeneratorTest extends \Test\TestCase { ->with($this->equalTo('1024-512-crop.png')) ->willThrowException(new NotFoundException()); - $this->eventDispatcher->expects($this->once()) + $this->legacyEventDispatcher->expects($this->once()) ->method('dispatch') ->with( $this->equalTo(IPreview::EVENT), @@ -294,6 +309,10 @@ class GeneratorTest extends \Test\TestCase { }) ); + $this->eventDispatcher->expects($this->once()) + ->method('dispatchTyped') + ->with(new BeforePreviewFetchedEvent($file)); + $this->generator->getPreview($file, 1024, 512, true, IPreview::MODE_COVER, 'invalidType'); } @@ -327,7 +346,7 @@ class GeneratorTest extends \Test\TestCase { $this->previewManager->expects($this->never()) ->method('isMimeSupported'); - $this->eventDispatcher->expects($this->once()) + $this->legacyEventDispatcher->expects($this->once()) ->method('dispatch') ->with( $this->equalTo(IPreview::EVENT), @@ -340,6 +359,10 @@ class GeneratorTest extends \Test\TestCase { }) ); + $this->eventDispatcher->expects($this->once()) + ->method('dispatchTyped') + ->with(new BeforePreviewFetchedEvent($file)); + $result = $this->generator->getPreview($file, 1024, 512, true, IPreview::MODE_COVER, 'invalidType'); $this->assertSame($preview, $result); } @@ -364,7 +387,7 @@ class GeneratorTest extends \Test\TestCase { $this->previewManager->method('getProviders') ->willReturn([]); - $this->eventDispatcher->expects($this->once()) + $this->legacyEventDispatcher->expects($this->once()) ->method('dispatch') ->with( $this->equalTo(IPreview::EVENT), @@ -375,6 +398,10 @@ class GeneratorTest extends \Test\TestCase { }) ); + $this->eventDispatcher->expects($this->once()) + ->method('dispatchTyped') + ->with(new BeforePreviewFetchedEvent($file)); + $this->expectException(NotFoundException::class); $this->generator->getPreview($file, 100, 100); } @@ -494,7 +521,7 @@ class GeneratorTest extends \Test\TestCase { ->with($this->equalTo($filename)) ->willReturn($preview); - $this->eventDispatcher->expects($this->once()) + $this->legacyEventDispatcher->expects($this->once()) ->method('dispatch') ->with( $this->equalTo(IPreview::EVENT), @@ -507,6 +534,10 @@ class GeneratorTest extends \Test\TestCase { }) ); + $this->eventDispatcher->expects($this->once()) + ->method('dispatchTyped') + ->with(new BeforePreviewFetchedEvent($file)); + $result = $this->generator->getPreview($file, $reqX, $reqY, $crop, $mode); if ($expectedX === $maxX && $expectedY === $maxY) { $this->assertSame($maxPreview, $result); -- 2.39.5