Skip to content

Commit 5b11664

Browse files
authored
Merge pull request #52660 from nextcloud/backport/52360/stable30
2 parents 8f6fe59 + a7dcc5c commit 5b11664

File tree

6 files changed

+62
-118
lines changed

6 files changed

+62
-118
lines changed

build/psalm-baseline.xml

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2455,24 +2455,9 @@
24552455
</InvalidReturnStatement>
24562456
</file>
24572457
<file src="lib/private/Preview/Generator.php">
2458-
<InvalidArgument>
2459-
<code><![CDATA[$maxPreviewImage]]></code>
2460-
</InvalidArgument>
24612458
<LessSpecificReturnType>
24622459
<code><![CDATA[null|string]]></code>
24632460
</LessSpecificReturnType>
2464-
<MismatchingDocblockParamType>
2465-
<code><![CDATA[ISimpleFile]]></code>
2466-
</MismatchingDocblockParamType>
2467-
<UndefinedInterfaceMethod>
2468-
<code><![CDATA[height]]></code>
2469-
<code><![CDATA[height]]></code>
2470-
<code><![CDATA[preciseResizeCopy]]></code>
2471-
<code><![CDATA[resizeCopy]]></code>
2472-
<code><![CDATA[valid]]></code>
2473-
<code><![CDATA[width]]></code>
2474-
<code><![CDATA[width]]></code>
2475-
</UndefinedInterfaceMethod>
24762461
</file>
24772462
<file src="lib/private/Preview/ProviderV1Adapter.php">
24782463
<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: 31 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use OCP\Files\InvalidPathException;
1313
use OCP\Files\NotFoundException;
1414
use OCP\Files\NotPermittedException;
15+
use OCP\Files\SimpleFS\InMemoryFile;
1516
use OCP\Files\SimpleFS\ISimpleFile;
1617
use OCP\Files\SimpleFS\ISimpleFolder;
1718
use OCP\IConfig;
@@ -57,17 +58,19 @@ public function __construct(
5758
* The cache is searched first and if nothing usable was found then a preview is
5859
* generated by one of the providers
5960
*
60-
* @param File $file
61-
* @param int $width
62-
* @param int $height
63-
* @param bool $crop
64-
* @param string $mode
65-
* @param string|null $mimeType
6661
* @return ISimpleFile
6762
* @throws NotFoundException
6863
* @throws \InvalidArgumentException if the preview would be invalid (in case the original image is invalid)
6964
*/
70-
public function getPreview(File $file, $width = -1, $height = -1, $crop = false, $mode = IPreview::MODE_FILL, $mimeType = null) {
65+
public function getPreview(
66+
File $file,
67+
int $width = -1,
68+
int $height = -1,
69+
bool $crop = false,
70+
string $mode = IPreview::MODE_FILL,
71+
?string $mimeType = null,
72+
bool $cacheResult = true,
73+
): ISimpleFile {
7174
$specification = [
7275
'width' => $width,
7376
'height' => $height,
@@ -84,20 +87,16 @@ public function getPreview(File $file, $width = -1, $height = -1, $crop = false,
8487
));
8588

8689
// since we only ask for one preview, and the generate method return the last one it created, it returns the one we want
87-
return $this->generatePreviews($file, [$specification], $mimeType);
90+
return $this->generatePreviews($file, [$specification], $mimeType, $cacheResult);
8891
}
8992

9093
/**
9194
* Generates previews of a file
9295
*
93-
* @param File $file
94-
* @param non-empty-array $specifications
95-
* @param string $mimeType
96-
* @return ISimpleFile the last preview that was generated
9796
* @throws NotFoundException
9897
* @throws \InvalidArgumentException if the preview would be invalid (in case the original image is invalid)
9998
*/
100-
public function generatePreviews(File $file, array $specifications, $mimeType = null) {
99+
public function generatePreviews(File $file, array $specifications, ?string $mimeType = null, bool $cacheResult = true): ISimpleFile {
101100
//Make sure that we can read the file
102101
if (!$file->isReadable()) {
103102
throw new NotFoundException('Cannot read file');
@@ -167,7 +166,7 @@ public function generatePreviews(File $file, array $specifications, $mimeType =
167166
$maxPreviewImage = $this->helper->getImage($maxPreview);
168167
}
169168

170-
$preview = $this->generatePreview($previewFolder, $maxPreviewImage, $width, $height, $crop, $maxWidth, $maxHeight, $previewVersion);
169+
$preview = $this->generatePreview($previewFolder, $maxPreviewImage, $width, $height, $crop, $maxWidth, $maxHeight, $previewVersion, $cacheResult);
171170
// New file, augment our array
172171
$previewFiles[] = $preview;
173172
}
@@ -346,11 +345,10 @@ private function generateProviderPreview(ISimpleFolder $previewFolder, File $fil
346345

347346
$path = $this->generatePath($preview->width(), $preview->height(), $crop, $max, $preview->dataMimeType(), $prefix);
348347
try {
349-
$file = $previewFolder->newFile($path);
350348
if ($preview instanceof IStreamImage) {
351-
$file->putContent($preview->resource());
349+
return $previewFolder->newFile($path, $preview->resource());
352350
} else {
353-
$file->putContent($preview->data());
351+
return $previewFolder->newFile($path, $preview->data());
354352
}
355353
} catch (NotPermittedException $e) {
356354
throw new NotFoundException();
@@ -485,19 +483,20 @@ private function calculateSize($width, $height, $crop, $mode, $maxWidth, $maxHei
485483
}
486484

487485
/**
488-
* @param ISimpleFolder $previewFolder
489-
* @param ISimpleFile $maxPreview
490-
* @param int $width
491-
* @param int $height
492-
* @param bool $crop
493-
* @param int $maxWidth
494-
* @param int $maxHeight
495-
* @param string $prefix
496-
* @return ISimpleFile
497486
* @throws NotFoundException
498487
* @throws \InvalidArgumentException if the preview would be invalid (in case the original image is invalid)
499488
*/
500-
private function generatePreview(ISimpleFolder $previewFolder, IImage $maxPreview, $width, $height, $crop, $maxWidth, $maxHeight, $prefix) {
489+
private function generatePreview(
490+
ISimpleFolder $previewFolder,
491+
IImage $maxPreview,
492+
int $width,
493+
int $height,
494+
bool $crop,
495+
int $maxWidth,
496+
int $maxHeight,
497+
string $prefix,
498+
bool $cacheResult,
499+
): ISimpleFile {
501500
$preview = $maxPreview;
502501
if (!$preview->valid()) {
503502
throw new \InvalidArgumentException('Failed to generate preview, failed to load image');
@@ -534,12 +533,14 @@ private function generatePreview(ISimpleFolder $previewFolder, IImage $maxPrevie
534533

535534
$path = $this->generatePath($width, $height, $crop, false, $preview->dataMimeType(), $prefix);
536535
try {
537-
$file = $previewFolder->newFile($path);
538-
$file->putContent($preview->data());
536+
if ($cacheResult) {
537+
return $previewFolder->newFile($path, $preview->data());
538+
} else {
539+
return new InMemoryFile($path, $preview->data());
540+
}
539541
} catch (NotPermittedException $e) {
540542
throw new NotFoundException();
541543
}
542-
543544
return $file;
544545
}
545546

lib/private/PreviewManager.php

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -142,29 +142,20 @@ private function getGenerator(): Generator {
142142
return $this->generator;
143143
}
144144

145-
/**
146-
* Returns a preview of a file
147-
*
148-
* The cache is searched first and if nothing usable was found then a preview is
149-
* generated by one of the providers
150-
*
151-
* @param File $file
152-
* @param int $width
153-
* @param int $height
154-
* @param bool $crop
155-
* @param string $mode
156-
* @param string $mimeType
157-
* @return ISimpleFile
158-
* @throws NotFoundException
159-
* @throws \InvalidArgumentException if the preview would be invalid (in case the original image is invalid)
160-
* @since 11.0.0 - \InvalidArgumentException was added in 12.0.0
161-
*/
162-
public function getPreview(File $file, $width = -1, $height = -1, $crop = false, $mode = IPreview::MODE_FILL, $mimeType = null) {
145+
public function getPreview(
146+
File $file,
147+
$width = -1,
148+
$height = -1,
149+
$crop = false,
150+
$mode = IPreview::MODE_FILL,
151+
$mimeType = null,
152+
bool $cacheResult = true,
153+
): ISimpleFile {
163154
$this->throwIfPreviewsDisabled();
164155
$previewConcurrency = $this->getGenerator()->getNumConcurrentPreviews('preview_concurrency_all');
165156
$sem = Generator::guardWithSemaphore(Generator::SEMAPHORE_ID_ALL, $previewConcurrency);
166157
try {
167-
$preview = $this->getGenerator()->getPreview($file, $width, $height, $crop, $mode, $mimeType);
158+
$preview = $this->getGenerator()->getPreview($file, $width, $height, $crop, $mode, $mimeType, $cacheResult);
168159
} finally {
169160
Generator::unguardWithSemaphore($sem);
170161
}

lib/public/IPreview.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,12 +71,14 @@ public function hasProviders();
7171
* @param bool $crop
7272
* @param string $mode
7373
* @param string $mimeType To force a given mimetype for the file (files_versions needs this)
74+
* @param bool $cacheResult Whether or not to cache the preview on the filesystem. Default to true. Can be useful to set to false to limit the amount of stored previews.
7475
* @return ISimpleFile
7576
* @throws NotFoundException
7677
* @throws \InvalidArgumentException if the preview would be invalid (in case the original image is invalid)
7778
* @since 11.0.0 - \InvalidArgumentException was added in 12.0.0
79+
* @since 32.0.0 - getPreview($cacheResult) added the $cacheResult argument to the signature
7880
*/
79-
public function getPreview(File $file, $width = -1, $height = -1, $crop = false, $mode = IPreview::MODE_FILL, $mimeType = null);
81+
public function getPreview(File $file, $width = -1, $height = -1, $crop = false, $mode = IPreview::MODE_FILL, $mimeType = null, bool $cacheResult = true);
8082

8183
/**
8284
* Returns true if the passed mime type is supported

tests/lib/Preview/GeneratorTest.php

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -21,19 +21,19 @@
2121
use OCP\Preview\IProviderV2;
2222

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

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

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

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

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

3939
/** @var Generator */
@@ -191,18 +191,10 @@ public function testGetNewPreview() {
191191
$previewFolder->method('getDirectoryListing')
192192
->willReturn([]);
193193
$previewFolder->method('newFile')
194-
->willReturnCallback(function ($filename) use ($maxPreview, $previewFile) {
195-
if ($filename === '2048-2048-max.png') {
196-
return $maxPreview;
197-
} elseif ($filename === '256-256.png') {
198-
return $previewFile;
199-
}
200-
$this->fail('Unexpected file');
201-
});
202-
203-
$maxPreview->expects($this->once())
204-
->method('putContent')
205-
->with($this->equalTo('my data'));
194+
->willReturnMap([
195+
['2048-2048-max.png', 'my data', $maxPreview],
196+
['256-256.png', 'my resized data', $previewFile],
197+
]);
206198

207199
$previewFolder->method('getFile')
208200
->with($this->equalTo('256-256.png'))
@@ -213,10 +205,6 @@ public function testGetNewPreview() {
213205
->with($this->equalTo($maxPreview))
214206
->willReturn($image);
215207

216-
$previewFile->expects($this->once())
217-
->method('putContent')
218-
->with('my resized data');
219-
220208
$this->eventDispatcher->expects($this->once())
221209
->method('dispatchTyped')
222210
->with(new BeforePreviewFetchedEvent($file, 100, 100, false, IPreview::MODE_FILL));

0 commit comments

Comments
 (0)