Skip to content

Commit

Permalink
Merge pull request #34854 from nextcloud/backport/34799/stable25
Browse files Browse the repository at this point in the history
[stable25] Emit typed event when preview is requested
  • Loading branch information
skjnldsv authored Oct 27, 2022
2 parents aab74be + 6774514 commit 7508e5d
Show file tree
Hide file tree
Showing 7 changed files with 120 additions and 27 deletions.
1 change: 1 addition & 0 deletions lib/composer/composer/autoload_classmap.php
Original file line number Diff line number Diff line change
Expand Up @@ -494,6 +494,7 @@
'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',
Expand Down
1 change: 1 addition & 0 deletions lib/composer/composer/autoload_static.php
Original file line number Diff line number Diff line change
Expand Up @@ -527,6 +527,7 @@ class ComposerStaticInit749170dad3f5e7f9ca158f5a9f04f6a2
'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',
Expand Down
21 changes: 12 additions & 9 deletions lib/private/Preview/Generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
*/
namespace OC\Preview;

use OCP\EventDispatcher\IEventDispatcher;
use OCP\Files\File;
use OCP\Files\IAppData;
use OCP\Files\InvalidPathException;
Expand All @@ -40,6 +41,7 @@
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;
Expand All @@ -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;
}

Expand All @@ -102,10 +101,14 @@ public function getPreview(File $file, $width = -1, $height = -1, $crop = false,
'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);
Expand Down
25 changes: 15 additions & 10 deletions lib/private/PreviewManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
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;
Expand All @@ -51,7 +52,8 @@ class PreviewManager implements IPreview {
protected IConfig $config;
protected IRootFolder $rootFolder;
protected IAppData $appData;
protected EventDispatcherInterface $eventDispatcher;
protected IEventDispatcher $eventDispatcher;
protected EventDispatcherInterface $legacyEventDispatcher;
private ?Generator $generator = null;
private GeneratorHelper $helper;
protected bool $providerListDirty = false;
Expand All @@ -73,20 +75,22 @@ class PreviewManager implements IPreview {
private IBinaryFinder $binaryFinder;

public function __construct(
IConfig $config,
IRootFolder $rootFolder,
IAppData $appData,
EventDispatcherInterface $eventDispatcher,
GeneratorHelper $helper,
?string $userId,
Coordinator $bootstrapCoordinator,
IServerContainer $container,
IBinaryFinder $binaryFinder
IConfig $config,
IRootFolder $rootFolder,
IAppData $appData,
IEventDispatcher $eventDispatcher,
EventDispatcherInterface $legacyEventDispatcher,
GeneratorHelper $helper,
?string $userId,
Coordinator $bootstrapCoordinator,
IServerContainer $container,
IBinaryFinder $binaryFinder
) {
$this->config = $config;
$this->rootFolder = $rootFolder;
$this->appData = $appData;
$this->eventDispatcher = $eventDispatcher;
$this->legacyEventDispatcher = $legacyEventDispatcher;
$this->helper = $helper;
$this->userId = $userId;
$this->bootstrapCoordinator = $bootstrapCoordinator;
Expand Down Expand Up @@ -153,6 +157,7 @@ private function getGenerator(): Generator {
$this->rootFolder,
$this->config
),
$this->legacyEventDispatcher,
$this->eventDispatcher
);
}
Expand Down
1 change: 1 addition & 0 deletions lib/private/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,7 @@ public function __construct($webRoot, \OC\Config $config) {
$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'),
Expand Down
51 changes: 51 additions & 0 deletions lib/public/Preview/BeforePreviewFetchedEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

declare(strict_types=1);

/**
* @copyright Copyright (c) 2022 Julius Härtl <jus@bitgrid.net>
*
* @author Julius Härtl <jus@bitgrid.net>
*
* @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 <http://www.gnu.org/licenses/>.
*/


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;
}
}
47 changes: 39 additions & 8 deletions tests/lib/Preview/GeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,15 @@

use OC\Preview\Generator;
use OC\Preview\GeneratorHelper;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\Files\File;
use OCP\Files\IAppData;
use OCP\Files\NotFoundException;
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;
Expand All @@ -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;

Expand All @@ -63,13 +68,15 @@ protected function setUp(): void {
$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
);
}
Expand Down Expand Up @@ -109,7 +116,7 @@ public function testGetCachedPreview() {
->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),
Expand All @@ -120,6 +127,10 @@ public function testGetCachedPreview() {
})
);

$this->eventDispatcher->expects($this->once())
->method('dispatchTyped')
->with(new BeforePreviewFetchedEvent($file));

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

$this->eventDispatcher->expects($this->once())
$this->legacyEventDispatcher->expects($this->once())
->method('dispatch')
->with(
$this->equalTo(IPreview::EVENT),
Expand All @@ -250,6 +261,10 @@ public function testGetNewPreview() {
})
);

$this->eventDispatcher->expects($this->once())
->method('dispatchTyped')
->with(new BeforePreviewFetchedEvent($file));

$result = $this->generator->getPreview($file, 100, 100);
$this->assertSame($previewFile, $result);
}
Expand Down Expand Up @@ -285,7 +300,7 @@ public function testInvalidMimeType() {
->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),
Expand All @@ -298,6 +313,10 @@ public function testInvalidMimeType() {
})
);

$this->eventDispatcher->expects($this->once())
->method('dispatchTyped')
->with(new BeforePreviewFetchedEvent($file));

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

Expand Down Expand Up @@ -333,7 +352,7 @@ public function testReturnCachedPreviewsWithoutCheckingSupportedMimetype() {
$this->previewManager->expects($this->never())
->method('isMimeSupported');

$this->eventDispatcher->expects($this->once())
$this->legacyEventDispatcher->expects($this->once())
->method('dispatch')
->with(
$this->equalTo(IPreview::EVENT),
Expand All @@ -346,6 +365,10 @@ public function testReturnCachedPreviewsWithoutCheckingSupportedMimetype() {
})
);

$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);
}
Expand All @@ -370,7 +393,7 @@ public function testNoProvider() {
$this->previewManager->method('getProviders')
->willReturn([]);

$this->eventDispatcher->expects($this->once())
$this->legacyEventDispatcher->expects($this->once())
->method('dispatch')
->with(
$this->equalTo(IPreview::EVENT),
Expand All @@ -381,6 +404,10 @@ public function testNoProvider() {
})
);

$this->eventDispatcher->expects($this->once())
->method('dispatchTyped')
->with(new BeforePreviewFetchedEvent($file));

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

$this->eventDispatcher->expects($this->once())
$this->legacyEventDispatcher->expects($this->once())
->method('dispatch')
->with(
$this->equalTo(IPreview::EVENT),
Expand All @@ -515,6 +542,10 @@ public function testCorrectSize($maxX, $maxY, $reqX, $reqY, $crop, $mode, $expec
})
);

$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);
Expand Down

0 comments on commit 7508e5d

Please sign in to comment.