Skip to content

Commit dfbaf31

Browse files
committed
Emit typed event when preview is requested
Signed-off-by: Julius Härtl <jus@bitgrid.net>
1 parent ae86359 commit dfbaf31

File tree

6 files changed

+81
-19
lines changed

6 files changed

+81
-19
lines changed

lib/composer/composer/autoload_classmap.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -493,6 +493,7 @@
493493
'OCP\\Notification\\INotifier' => $baseDir . '/lib/public/Notification/INotifier.php',
494494
'OCP\\OCS\\IDiscoveryService' => $baseDir . '/lib/public/OCS/IDiscoveryService.php',
495495
'OCP\\PreConditionNotMetException' => $baseDir . '/lib/public/PreConditionNotMetException.php',
496+
'OCP\\Preview\\BeforeFetchPreviewEvent' => $baseDir . '/lib/public/Preview/BeforeFetchPreviewEvent.php',
496497
'OCP\\Preview\\IProvider' => $baseDir . '/lib/public/Preview/IProvider.php',
497498
'OCP\\Preview\\IProviderV2' => $baseDir . '/lib/public/Preview/IProviderV2.php',
498499
'OCP\\Preview\\IVersionedPreviewFile' => $baseDir . '/lib/public/Preview/IVersionedPreviewFile.php',

lib/composer/composer/autoload_static.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -526,6 +526,7 @@ class ComposerStaticInit749170dad3f5e7f9ca158f5a9f04f6a2
526526
'OCP\\Notification\\INotifier' => __DIR__ . '/../../..' . '/lib/public/Notification/INotifier.php',
527527
'OCP\\OCS\\IDiscoveryService' => __DIR__ . '/../../..' . '/lib/public/OCS/IDiscoveryService.php',
528528
'OCP\\PreConditionNotMetException' => __DIR__ . '/../../..' . '/lib/public/PreConditionNotMetException.php',
529+
'OCP\\Preview\\BeforeFetchPreviewEvent' => __DIR__ . '/../../..' . '/lib/public/Preview/BeforeFetchPreviewEvent.php',
529530
'OCP\\Preview\\IProvider' => __DIR__ . '/../../..' . '/lib/public/Preview/IProvider.php',
530531
'OCP\\Preview\\IProviderV2' => __DIR__ . '/../../..' . '/lib/public/Preview/IProviderV2.php',
531532
'OCP\\Preview\\IVersionedPreviewFile' => __DIR__ . '/../../..' . '/lib/public/Preview/IVersionedPreviewFile.php',

lib/private/Preview/Generator.php

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
*/
3030
namespace OC\Preview;
3131

32+
use OCP\EventDispatcher\IEventDispatcher;
3233
use OCP\Files\File;
3334
use OCP\Files\IAppData;
3435
use OCP\Files\InvalidPathException;
@@ -40,6 +41,7 @@
4041
use OCP\IImage;
4142
use OCP\IPreview;
4243
use OCP\IStreamImage;
44+
use OCP\Preview\BeforeFetchPreviewEvent;
4345
use OCP\Preview\IProviderV2;
4446
use OCP\Preview\IVersionedPreviewFile;
4547
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
@@ -56,26 +58,23 @@ class Generator {
5658
/** @var GeneratorHelper */
5759
private $helper;
5860
/** @var EventDispatcherInterface */
61+
private $legacyEventDispatcher;
62+
/** @var IEventDispatcher */
5963
private $eventDispatcher;
6064

61-
/**
62-
* @param IConfig $config
63-
* @param IPreview $previewManager
64-
* @param IAppData $appData
65-
* @param GeneratorHelper $helper
66-
* @param EventDispatcherInterface $eventDispatcher
67-
*/
6865
public function __construct(
6966
IConfig $config,
7067
IPreview $previewManager,
7168
IAppData $appData,
7269
GeneratorHelper $helper,
73-
EventDispatcherInterface $eventDispatcher
70+
EventDispatcherInterface $legacyEventDispatcher,
71+
IEventDispatcher $eventDispatcher
7472
) {
7573
$this->config = $config;
7674
$this->previewManager = $previewManager;
7775
$this->appData = $appData;
7876
$this->helper = $helper;
77+
$this->legacyEventDispatcher = $legacyEventDispatcher;
7978
$this->eventDispatcher = $eventDispatcher;
8079
}
8180

@@ -102,10 +101,14 @@ public function getPreview(File $file, $width = -1, $height = -1, $crop = false,
102101
'crop' => $crop,
103102
'mode' => $mode,
104103
];
105-
$this->eventDispatcher->dispatch(
104+
105+
$this->legacyEventDispatcher->dispatch(
106106
IPreview::EVENT,
107107
new GenericEvent($file, $specification)
108108
);
109+
$this->eventDispatcher->dispatchTyped(new BeforeFetchPreviewEvent(
110+
$file
111+
));
109112

110113
// since we only ask for one preview, and the generate method return the last one it created, it returns the one we want
111114
return $this->generatePreviews($file, [$specification], $mimeType);

lib/private/PreviewManager.php

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
use OC\Preview\Generator;
3535
use OC\Preview\GeneratorHelper;
3636
use OCP\AppFramework\QueryException;
37+
use OCP\EventDispatcher\IEventDispatcher;
3738
use OCP\Files\File;
3839
use OCP\Files\IAppData;
3940
use OCP\Files\IRootFolder;
@@ -51,7 +52,8 @@ class PreviewManager implements IPreview {
5152
protected IConfig $config;
5253
protected IRootFolder $rootFolder;
5354
protected IAppData $appData;
54-
protected EventDispatcherInterface $eventDispatcher;
55+
protected IEventDispatcher $eventDispatcher;
56+
protected EventDispatcherInterface $legacyEventDispatcher;
5557
private ?Generator $generator = null;
5658
private GeneratorHelper $helper;
5759
protected bool $providerListDirty = false;
@@ -73,20 +75,22 @@ class PreviewManager implements IPreview {
7375
private IBinaryFinder $binaryFinder;
7476

7577
public function __construct(
76-
IConfig $config,
77-
IRootFolder $rootFolder,
78-
IAppData $appData,
79-
EventDispatcherInterface $eventDispatcher,
80-
GeneratorHelper $helper,
81-
?string $userId,
82-
Coordinator $bootstrapCoordinator,
83-
IServerContainer $container,
84-
IBinaryFinder $binaryFinder
78+
IConfig $config,
79+
IRootFolder $rootFolder,
80+
IAppData $appData,
81+
IEventDispatcher $eventDispatcher,
82+
EventDispatcherInterface $legacyEventDispatcher,
83+
GeneratorHelper $helper,
84+
?string $userId,
85+
Coordinator $bootstrapCoordinator,
86+
IServerContainer $container,
87+
IBinaryFinder $binaryFinder
8588
) {
8689
$this->config = $config;
8790
$this->rootFolder = $rootFolder;
8891
$this->appData = $appData;
8992
$this->eventDispatcher = $eventDispatcher;
93+
$this->legacyEventDispatcher = $legacyEventDispatcher;
9094
$this->helper = $helper;
9195
$this->userId = $userId;
9296
$this->bootstrapCoordinator = $bootstrapCoordinator;
@@ -153,6 +157,7 @@ private function getGenerator(): Generator {
153157
$this->rootFolder,
154158
$this->config
155159
),
160+
$this->legacyEventDispatcher,
156161
$this->eventDispatcher
157162
);
158163
}

lib/private/Server.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,7 @@ public function __construct($webRoot, \OC\Config $config) {
335335
$c->get(IRootFolder::class),
336336
$c->get(SystemConfig::class)
337337
),
338+
$c->get(IEventDispatcher::class),
338339
$c->get(SymfonyAdapter::class),
339340
$c->get(GeneratorHelper::class),
340341
$c->get(ISession::class)->get('user_id'),
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* @copyright Copyright (c) 2022 Julius Härtl <jus@bitgrid.net>
7+
*
8+
* @author Julius Härtl <jus@bitgrid.net>
9+
*
10+
* @license GNU AGPL version 3 or any later version
11+
*
12+
* This program is free software: you can redistribute it and/or modify
13+
* it under the terms of the GNU Affero General Public License as
14+
* published by the Free Software Foundation, either version 3 of the
15+
* License, or (at your option) any later version.
16+
*
17+
* This program is distributed in the hope that it will be useful,
18+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
19+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20+
* GNU Affero General Public License for more details.
21+
*
22+
* You should have received a copy of the GNU Affero General Public License
23+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
24+
*/
25+
26+
27+
namespace OCP\Preview;
28+
29+
use OCP\Files\Node;
30+
31+
/**
32+
* @since 25.0.1
33+
*/
34+
class BeforeFetchPreviewEvent extends \OCP\EventDispatcher\Event {
35+
private Node $node;
36+
37+
/**
38+
* @since 25.0.1
39+
*/
40+
public function __construct(Node $node) {
41+
parent::__construct();
42+
$this->node = $node;
43+
}
44+
45+
/**
46+
* @since 25.0.1
47+
*/
48+
public function getNode(): Node {
49+
return $this->node;
50+
}
51+
}

0 commit comments

Comments
 (0)