Skip to content

Commit 548bfd7

Browse files
committed
fix: Use preview API to generate the previews
This allows to benefit from all the check done by the API. This also used the newly introduced cacheResult argument to limit disk usage. Signed-off-by: Louis Chemineau <louis@chmn.me>
1 parent dd843da commit 548bfd7

1 file changed

Lines changed: 9 additions & 30 deletions

File tree

lib/private/Blurhash/Listener/GenerateBlurhashMetadata.php

Lines changed: 9 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use OCP\FilesMetadata\AMetadataEvent;
2020
use OCP\FilesMetadata\Event\MetadataBackgroundEvent;
2121
use OCP\FilesMetadata\Event\MetadataLiveEvent;
22+
use OCP\IPreview;
2223
use OCP\Lock\LockedException;
2324

2425
/**
@@ -32,6 +33,11 @@ class GenerateBlurhashMetadata implements IEventListener {
3233
private const COMPONENTS_X = 4;
3334
private const COMPONENTS_Y = 3;
3435

36+
public function __construct(
37+
private IPreview $preview,
38+
) {
39+
}
40+
3541
/**
3642
* @throws NotPermittedException
3743
* @throws GenericFileException
@@ -64,7 +70,9 @@ public function handle(Event $event): void {
6470
return;
6571
}
6672

67-
$image = $this->resizedImageFromFile($file);
73+
$preview = $this->preview->getPreview($file, 256, 256, cacheResult: false);
74+
$image = @imagecreatefromstring($preview->getContent());
75+
6876
if (!$image) {
6977
return;
7078
}
@@ -73,35 +81,6 @@ public function handle(Event $event): void {
7381
->setEtag('blurhash', $currentEtag);
7482
}
7583

76-
/**
77-
* @param File $file
78-
*
79-
* @return GdImage|false
80-
* @throws GenericFileException
81-
* @throws NotPermittedException
82-
* @throws LockedException
83-
*/
84-
private function resizedImageFromFile(File $file): GdImage|false {
85-
$image = @imagecreatefromstring($file->getContent());
86-
if ($image === false) {
87-
return false;
88-
}
89-
90-
$currX = imagesx($image);
91-
$currY = imagesy($image);
92-
93-
if ($currX > $currY) {
94-
$newX = self::RESIZE_BOXSIZE;
95-
$newY = intval($currY * $newX / $currX);
96-
} else {
97-
$newY = self::RESIZE_BOXSIZE;
98-
$newX = intval($currX * $newY / $currY);
99-
}
100-
101-
$newImage = @imagescale($image, $newX, $newY);
102-
return ($newImage !== false) ? $newImage : $image;
103-
}
104-
10584
/**
10685
* @param GdImage $image
10786
*

0 commit comments

Comments
 (0)