]> source.dussan.org Git - nextcloud-server.git/commitdiff
feat: add width, height, crop and mode to BeforePreviewFetchedEvent 38679/head
authorDaniel Kesselberg <mail@danielkesselberg.de>
Tue, 6 Jun 2023 21:12:40 +0000 (23:12 +0200)
committerDaniel Kesselberg <mail@danielkesselberg.de>
Fri, 23 Jun 2023 14:27:35 +0000 (16:27 +0200)
user_usage_report keeps a counter for files read.

The app uses

- OC_Filesystem::read hook
- Event listener (the legacy one) for IPreview::EVENT

Request for previews do not trigger the hook, hence the additional event listener.
The thumbnails for a list or grid view should not count, so we need the width and height.

Signed-off-by: Daniel Kesselberg <mail@danielkesselberg.de>
lib/private/Preview/Generator.php
lib/public/Preview/BeforePreviewFetchedEvent.php
tests/lib/Preview/GeneratorTest.php

index 3ce449134987153dd34d5bba005d0e2f9a5e7601..c9949c82a9767f13ed49d448eed8690c34c2beb4 100644 (file)
@@ -91,7 +91,7 @@ class Generator {
         * @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)
@@ -109,7 +109,11 @@ class Generator {
                        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
index 37da63b95a1a15c98f8c08cedc039fbc86ea48e5..19048e08620c91f0cef66dd13fde3c2dff3d0ce6 100644 (file)
@@ -27,19 +27,27 @@ declare(strict_types=1);
 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
         */
-       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;
        }
 
        /**
@@ -48,4 +56,33 @@ class BeforePreviewFetchedEvent extends \OCP\EventDispatcher\Event {
        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;
+       }
 }
index 37fc3935139e01d25853a85f2e3ae2b5f5358c4b..2216674a34c2a4dfa6f2ec3f0b755636b2f6cee6 100644 (file)
@@ -125,7 +125,7 @@ class GeneratorTest extends \Test\TestCase {
 
                $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);
@@ -264,7 +264,7 @@ class GeneratorTest extends \Test\TestCase {
 
                $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);
@@ -316,7 +316,7 @@ class GeneratorTest extends \Test\TestCase {
 
                $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');
        }
@@ -366,7 +366,7 @@ class GeneratorTest extends \Test\TestCase {
 
                $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);
@@ -405,7 +405,7 @@ class GeneratorTest extends \Test\TestCase {
 
                $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);
@@ -543,7 +543,7 @@ class GeneratorTest extends \Test\TestCase {
 
                $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) {