Skip to content

Commit

Permalink
Add preview specifications into BeforePreviewFetchedEvent event
Browse files Browse the repository at this point in the history
Signed-off-by: Thomas Citharel <[email protected]>
  • Loading branch information
tcitworld committed Mar 13, 2023
1 parent 37b1469 commit e72d6f6
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 8 deletions.
7 changes: 6 additions & 1 deletion lib/private/Preview/Generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,12 @@ public function getPreview(File $file, $width = -1, $height = -1, $crop = false,
new GenericEvent($file, $specification)
);
$this->eventDispatcher->dispatchTyped(new BeforePreviewFetchedEvent(
$file
$file,
$width,
$height,
$crop,
$mode,
$mimeType
));

// since we only ask for one preview, and the generate method return the last one it created, it returns the one we want
Expand Down
47 changes: 46 additions & 1 deletion lib/public/Preview/BeforePreviewFetchedEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,23 @@
*/
class BeforePreviewFetchedEvent extends \OCP\EventDispatcher\Event {
private Node $node;
private int $width;
private int $height;
private bool $crop;
private string $mode;
private ?string $mimeType;

/**
* @since 25.0.1
*/
public function __construct(Node $node) {
public function __construct(Node $node, int $width, int $height, bool $crop, string $mode, ?string $mimeType) {
parent::__construct();
$this->node = $node;
$this->width = $width;
$this->height = $height;
$this->crop = $crop;
$this->mode = $mode;
$this->mimeType = $mimeType;
}

/**
Expand All @@ -48,4 +58,39 @@ public function __construct(Node $node) {
public function getNode(): Node {
return $this->node;
}

/**
* @since 27.0.0
*/
public function getWidth(): int {
return $this->width;
}

/**
* @since 27.0.0
*/
public function getHeight(): int {
return $this->height;
}

/**
* @since 27.0.0
*/
public function isCrop(): bool {
return $this->crop;
}

/**
* @since 27.0.0
*/
public function getMode(): string {
return $this->mode;
}

/**
* @since 27.0.0
*/
public function getMimeType(): ?string {
return $this->mimeType;
}
}
12 changes: 6 additions & 6 deletions tests/lib/Preview/GeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ public function testGetCachedPreview() {

$this->eventDispatcher->expects($this->once())
->method('dispatchTyped')
->with(new BeforePreviewFetchedEvent($file));
->with(new BeforePreviewFetchedEvent($file, 100, 100, false, IPreview::MODE_FILL, null));

$result = $this->generator->getPreview($file, 100, 100);
$this->assertSame($previewFile, $result);
Expand Down Expand Up @@ -267,7 +267,7 @@ public function testGetNewPreview() {

$this->eventDispatcher->expects($this->once())
->method('dispatchTyped')
->with(new BeforePreviewFetchedEvent($file));
->with(new BeforePreviewFetchedEvent($file, 100, 100, false, IPreview::MODE_FILL, null));

$result = $this->generator->getPreview($file, 100, 100);
$this->assertSame($previewFile, $result);
Expand Down Expand Up @@ -319,7 +319,7 @@ public function testInvalidMimeType() {

$this->eventDispatcher->expects($this->once())
->method('dispatchTyped')
->with(new BeforePreviewFetchedEvent($file));
->with(new BeforePreviewFetchedEvent($file, 1024, 512, true, IPreview::MODE_COVER, 'invalidType'));

$this->generator->getPreview($file, 1024, 512, true, IPreview::MODE_COVER, 'invalidType');
}
Expand Down Expand Up @@ -371,7 +371,7 @@ public function testReturnCachedPreviewsWithoutCheckingSupportedMimetype() {

$this->eventDispatcher->expects($this->once())
->method('dispatchTyped')
->with(new BeforePreviewFetchedEvent($file));
->with(new BeforePreviewFetchedEvent($file, 1024, 512, true, IPreview::MODE_COVER, 'invalidType'));

$result = $this->generator->getPreview($file, 1024, 512, true, IPreview::MODE_COVER, 'invalidType');
$this->assertSame($preview, $result);
Expand Down Expand Up @@ -410,7 +410,7 @@ public function testNoProvider() {

$this->eventDispatcher->expects($this->once())
->method('dispatchTyped')
->with(new BeforePreviewFetchedEvent($file));
->with(new BeforePreviewFetchedEvent($file, 100, 100, false, IPreview::MODE_FILL, null));

$this->expectException(NotFoundException::class);
$this->generator->getPreview($file, 100, 100);
Expand Down Expand Up @@ -548,7 +548,7 @@ public function testCorrectSize($maxX, $maxY, $reqX, $reqY, $crop, $mode, $expec

$this->eventDispatcher->expects($this->once())
->method('dispatchTyped')
->with(new BeforePreviewFetchedEvent($file));
->with(new BeforePreviewFetchedEvent($file, $reqX, $reqY, $crop, $mode, null));

$result = $this->generator->getPreview($file, $reqX, $reqY, $crop, $mode);
if ($expectedX === $maxX && $expectedY === $maxY) {
Expand Down

0 comments on commit e72d6f6

Please sign in to comment.