Skip to content

Commit bea982d

Browse files
artongebackportbot[bot]
authored andcommitted
fix(blurhash): Use preview API to generate the previews
This allows to benefit from all the checks done by the preview API. This also use the newly introduced `cacheResult` argument to limit disk usage. Signed-off-by: Louis Chemineau <louis@chmn.me>
1 parent 8e255bc commit bea982d

4 files changed

Lines changed: 22 additions & 74 deletions

File tree

build/psalm-baseline.xml

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2249,24 +2249,9 @@
22492249
</InvalidReturnStatement>
22502250
</file>
22512251
<file src="lib/private/Preview/Generator.php">
2252-
<InvalidArgument>
2253-
<code><![CDATA[$maxPreviewImage]]></code>
2254-
</InvalidArgument>
22552252
<LessSpecificReturnType>
22562253
<code><![CDATA[null|string]]></code>
22572254
</LessSpecificReturnType>
2258-
<MismatchingDocblockParamType>
2259-
<code><![CDATA[ISimpleFile]]></code>
2260-
</MismatchingDocblockParamType>
2261-
<UndefinedInterfaceMethod>
2262-
<code><![CDATA[height]]></code>
2263-
<code><![CDATA[height]]></code>
2264-
<code><![CDATA[preciseResizeCopy]]></code>
2265-
<code><![CDATA[resizeCopy]]></code>
2266-
<code><![CDATA[valid]]></code>
2267-
<code><![CDATA[width]]></code>
2268-
<code><![CDATA[width]]></code>
2269-
</UndefinedInterfaceMethod>
22702255
</file>
22712256
<file src="lib/private/Preview/ProviderV1Adapter.php">
22722257
<InvalidReturnStatement>

lib/private/Blurhash/Listener/GenerateBlurhashMetadata.php

Lines changed: 9 additions & 32 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
/**
@@ -27,11 +28,14 @@
2728
* @template-implements IEventListener<AMetadataEvent>
2829
*/
2930
class GenerateBlurhashMetadata implements IEventListener {
30-
private const RESIZE_BOXSIZE = 30;
31-
3231
private const COMPONENTS_X = 4;
3332
private const COMPONENTS_Y = 3;
3433

34+
public function __construct(
35+
private IPreview $preview,
36+
) {
37+
}
38+
3539
/**
3640
* @throws NotPermittedException
3741
* @throws GenericFileException
@@ -64,7 +68,9 @@ public function handle(Event $event): void {
6468
return;
6569
}
6670

67-
$image = $this->resizedImageFromFile($file);
71+
$preview = $this->preview->getPreview($file, 64, 64, cacheResult: false);
72+
$image = @imagecreatefromstring($preview->getContent());
73+
6874
if (!$image) {
6975
return;
7076
}
@@ -73,35 +79,6 @@ public function handle(Event $event): void {
7379
->setEtag('blurhash', $currentEtag);
7480
}
7581

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-
10582
/**
10683
* @param GdImage $image
10784
*

lib/private/Preview/Generator.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -350,11 +350,10 @@ private function generateProviderPreview(ISimpleFolder $previewFolder, File $fil
350350

351351
$path = $this->generatePath($preview->width(), $preview->height(), $crop, $max, $preview->dataMimeType(), $prefix);
352352
try {
353-
$file = $previewFolder->newFile($path);
354353
if ($preview instanceof IStreamImage) {
355-
$file->putContent($preview->resource());
354+
return $previewFolder->newFile($path, $preview->resource());
356355
} else {
357-
$file->putContent($preview->data());
356+
return $previewFolder->newFile($path, $preview->data());
358357
}
359358
} catch (NotPermittedException $e) {
360359
throw new NotFoundException();
@@ -540,14 +539,13 @@ private function generatePreview(
540539
$path = $this->generatePath($width, $height, $crop, false, $preview->dataMimeType(), $prefix);
541540
try {
542541
if ($cacheResult) {
543-
$file = $previewFolder->newFile($path, $preview->data());
542+
return $previewFolder->newFile($path, $preview->data());
544543
} else {
545544
return new InMemoryFile($path, $preview->data());
546545
}
547546
} catch (NotPermittedException $e) {
548547
throw new NotFoundException();
549548
}
550-
551549
return $file;
552550
}
553551

tests/lib/Preview/GeneratorTest.php

Lines changed: 10 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -22,25 +22,25 @@
2222
use Psr\Log\LoggerInterface;
2323

2424
class GeneratorTest extends \Test\TestCase {
25-
/** @var IConfig|\PHPUnit\Framework\MockObject\MockObject */
25+
/** @var IConfig&\PHPUnit\Framework\MockObject\MockObject */
2626
private $config;
2727

28-
/** @var IPreview|\PHPUnit\Framework\MockObject\MockObject */
28+
/** @var IPreview&\PHPUnit\Framework\MockObject\MockObject */
2929
private $previewManager;
3030

31-
/** @var IAppData|\PHPUnit\Framework\MockObject\MockObject */
31+
/** @var IAppData&\PHPUnit\Framework\MockObject\MockObject */
3232
private $appData;
3333

34-
/** @var GeneratorHelper|\PHPUnit\Framework\MockObject\MockObject */
34+
/** @var GeneratorHelper&\PHPUnit\Framework\MockObject\MockObject */
3535
private $helper;
3636

37-
/** @var IEventDispatcher|\PHPUnit\Framework\MockObject\MockObject */
37+
/** @var IEventDispatcher&\PHPUnit\Framework\MockObject\MockObject */
3838
private $eventDispatcher;
3939

4040
/** @var Generator */
4141
private $generator;
4242

43-
private LoggerInterface|\PHPUnit\Framework\MockObject\MockObject $logger;
43+
private LoggerInterface&\PHPUnit\Framework\MockObject\MockObject $logger;
4444

4545
protected function setUp(): void {
4646
parent::setUp();
@@ -196,18 +196,10 @@ public function testGetNewPreview(): void {
196196
$previewFolder->method('getDirectoryListing')
197197
->willReturn([]);
198198
$previewFolder->method('newFile')
199-
->willReturnCallback(function ($filename) use ($maxPreview, $previewFile) {
200-
if ($filename === '2048-2048-max.png') {
201-
return $maxPreview;
202-
} elseif ($filename === '256-256.png') {
203-
return $previewFile;
204-
}
205-
$this->fail('Unexpected file');
206-
});
207-
208-
$maxPreview->expects($this->once())
209-
->method('putContent')
210-
->with($this->equalTo('my data'));
199+
->willReturnMap([
200+
['2048-2048-max.png', 'my data', $maxPreview],
201+
['256-256.png', 'my resized data', $previewFile],
202+
]);
211203

212204
$previewFolder->method('getFile')
213205
->with($this->equalTo('256-256.png'))
@@ -218,10 +210,6 @@ public function testGetNewPreview(): void {
218210
->with($this->equalTo($maxPreview))
219211
->willReturn($image);
220212

221-
$previewFile->expects($this->once())
222-
->method('putContent')
223-
->with('my resized data');
224-
225213
$this->eventDispatcher->expects($this->once())
226214
->method('dispatchTyped')
227215
->with(new BeforePreviewFetchedEvent($file, 100, 100, false, IPreview::MODE_FILL, null));

0 commit comments

Comments
 (0)