Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add width, height, crop and mode to BeforePreviewFetchedEvent #38679

Merged
merged 1 commit into from
Jun 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions lib/private/Preview/Generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public function __construct(
* @param int $height
* @param bool $crop
* @param string $mode
* @param string $mimeType
* @param string|null $mimeType
* @return ISimpleFile
* @throws NotFoundException
* @throws \InvalidArgumentException if the preview would be invalid (in case the original image is invalid)
Expand All @@ -109,7 +109,11 @@ 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,
));

// 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
45 changes: 41 additions & 4 deletions lib/public/Preview/BeforePreviewFetchedEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,27 @@
namespace OCP\Preview;

use OCP\Files\Node;
use OCP\IPreview;

/**
* @since 25.0.1
*/
class BeforePreviewFetchedEvent extends \OCP\EventDispatcher\Event {
private Node $node;

/**
* @since 25.0.1
*/
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
*/
* @param string|IPreview::MODE_*
*/

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea

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

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

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

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

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

/**
* @since 28.0.0
* @return null|IPreview::MODE_FILL|IPreview::MODE_COVER
*/
public function getMode(): ?string {
return $this->mode;
}
}
12 changes: 6 additions & 6 deletions tests/lib/Preview/GeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,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));

$result = $this->generator->getPreview($file, 100, 100);
$this->assertSame($previewFile, $result);
Expand Down Expand Up @@ -264,7 +264,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));

$result = $this->generator->getPreview($file, 100, 100);
$this->assertSame($previewFile, $result);
Expand Down Expand Up @@ -316,7 +316,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));

$this->generator->getPreview($file, 1024, 512, true, IPreview::MODE_COVER, 'invalidType');
}
Expand Down Expand Up @@ -366,7 +366,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));

$result = $this->generator->getPreview($file, 1024, 512, true, IPreview::MODE_COVER, 'invalidType');
$this->assertSame($preview, $result);
Expand Down Expand Up @@ -405,7 +405,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));

$this->expectException(NotFoundException::class);
$this->generator->getPreview($file, 100, 100);
Expand Down Expand Up @@ -543,7 +543,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));

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