mirror of
https://github.com/nextcloud/server.git
synced 2026-02-19 02:38:40 -05:00
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>
This commit is contained in:
parent
2238548278
commit
867be352f3
4 changed files with 22 additions and 74 deletions
|
|
@ -2230,24 +2230,9 @@
|
|||
</InvalidReturnStatement>
|
||||
</file>
|
||||
<file src="lib/private/Preview/Generator.php">
|
||||
<InvalidArgument>
|
||||
<code><![CDATA[$maxPreviewImage]]></code>
|
||||
</InvalidArgument>
|
||||
<LessSpecificReturnType>
|
||||
<code><![CDATA[null|string]]></code>
|
||||
</LessSpecificReturnType>
|
||||
<MismatchingDocblockParamType>
|
||||
<code><![CDATA[ISimpleFile]]></code>
|
||||
</MismatchingDocblockParamType>
|
||||
<UndefinedInterfaceMethod>
|
||||
<code><![CDATA[height]]></code>
|
||||
<code><![CDATA[height]]></code>
|
||||
<code><![CDATA[preciseResizeCopy]]></code>
|
||||
<code><![CDATA[resizeCopy]]></code>
|
||||
<code><![CDATA[valid]]></code>
|
||||
<code><![CDATA[width]]></code>
|
||||
<code><![CDATA[width]]></code>
|
||||
</UndefinedInterfaceMethod>
|
||||
</file>
|
||||
<file src="lib/private/Preview/ProviderV1Adapter.php">
|
||||
<InvalidReturnStatement>
|
||||
|
|
|
|||
|
|
@ -19,6 +19,7 @@ use OCP\Files\NotPermittedException;
|
|||
use OCP\FilesMetadata\AMetadataEvent;
|
||||
use OCP\FilesMetadata\Event\MetadataBackgroundEvent;
|
||||
use OCP\FilesMetadata\Event\MetadataLiveEvent;
|
||||
use OCP\IPreview;
|
||||
use OCP\Lock\LockedException;
|
||||
|
||||
/**
|
||||
|
|
@ -27,11 +28,14 @@ use OCP\Lock\LockedException;
|
|||
* @template-implements IEventListener<AMetadataEvent>
|
||||
*/
|
||||
class GenerateBlurhashMetadata implements IEventListener {
|
||||
private const RESIZE_BOXSIZE = 30;
|
||||
|
||||
private const COMPONENTS_X = 4;
|
||||
private const COMPONENTS_Y = 3;
|
||||
|
||||
public function __construct(
|
||||
private IPreview $preview,
|
||||
) {
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws NotPermittedException
|
||||
* @throws GenericFileException
|
||||
|
|
@ -64,7 +68,9 @@ class GenerateBlurhashMetadata implements IEventListener {
|
|||
return;
|
||||
}
|
||||
|
||||
$image = $this->resizedImageFromFile($file);
|
||||
$preview = $this->preview->getPreview($file, 64, 64, cacheResult: false);
|
||||
$image = @imagecreatefromstring($preview->getContent());
|
||||
|
||||
if (!$image) {
|
||||
return;
|
||||
}
|
||||
|
|
@ -73,35 +79,6 @@ class GenerateBlurhashMetadata implements IEventListener {
|
|||
->setEtag('blurhash', $currentEtag);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param File $file
|
||||
*
|
||||
* @return GdImage|false
|
||||
* @throws GenericFileException
|
||||
* @throws NotPermittedException
|
||||
* @throws LockedException
|
||||
*/
|
||||
private function resizedImageFromFile(File $file): GdImage|false {
|
||||
$image = @imagecreatefromstring($file->getContent());
|
||||
if ($image === false) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$currX = imagesx($image);
|
||||
$currY = imagesy($image);
|
||||
|
||||
if ($currX > $currY) {
|
||||
$newX = self::RESIZE_BOXSIZE;
|
||||
$newY = intval($currY * $newX / $currX);
|
||||
} else {
|
||||
$newY = self::RESIZE_BOXSIZE;
|
||||
$newX = intval($currX * $newY / $currY);
|
||||
}
|
||||
|
||||
$newImage = @imagescale($image, $newX, $newY);
|
||||
return ($newImage !== false) ? $newImage : $image;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param GdImage $image
|
||||
*
|
||||
|
|
|
|||
|
|
@ -350,11 +350,10 @@ class Generator {
|
|||
|
||||
$path = $this->generatePath($preview->width(), $preview->height(), $crop, $max, $preview->dataMimeType(), $prefix);
|
||||
try {
|
||||
$file = $previewFolder->newFile($path);
|
||||
if ($preview instanceof IStreamImage) {
|
||||
$file->putContent($preview->resource());
|
||||
return $previewFolder->newFile($path, $preview->resource());
|
||||
} else {
|
||||
$file->putContent($preview->data());
|
||||
return $previewFolder->newFile($path, $preview->data());
|
||||
}
|
||||
} catch (NotPermittedException $e) {
|
||||
throw new NotFoundException();
|
||||
|
|
@ -540,14 +539,13 @@ class Generator {
|
|||
$path = $this->generatePath($width, $height, $crop, false, $preview->dataMimeType(), $prefix);
|
||||
try {
|
||||
if ($cacheResult) {
|
||||
$file = $previewFolder->newFile($path, $preview->data());
|
||||
return $previewFolder->newFile($path, $preview->data());
|
||||
} else {
|
||||
return new InMemoryFile($path, $preview->data());
|
||||
}
|
||||
} catch (NotPermittedException $e) {
|
||||
throw new NotFoundException();
|
||||
}
|
||||
|
||||
return $file;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -22,25 +22,25 @@ use OCP\Preview\IProviderV2;
|
|||
use Psr\Log\LoggerInterface;
|
||||
|
||||
class GeneratorTest extends \Test\TestCase {
|
||||
/** @var IConfig|\PHPUnit\Framework\MockObject\MockObject */
|
||||
/** @var IConfig&\PHPUnit\Framework\MockObject\MockObject */
|
||||
private $config;
|
||||
|
||||
/** @var IPreview|\PHPUnit\Framework\MockObject\MockObject */
|
||||
/** @var IPreview&\PHPUnit\Framework\MockObject\MockObject */
|
||||
private $previewManager;
|
||||
|
||||
/** @var IAppData|\PHPUnit\Framework\MockObject\MockObject */
|
||||
/** @var IAppData&\PHPUnit\Framework\MockObject\MockObject */
|
||||
private $appData;
|
||||
|
||||
/** @var GeneratorHelper|\PHPUnit\Framework\MockObject\MockObject */
|
||||
/** @var GeneratorHelper&\PHPUnit\Framework\MockObject\MockObject */
|
||||
private $helper;
|
||||
|
||||
/** @var IEventDispatcher|\PHPUnit\Framework\MockObject\MockObject */
|
||||
/** @var IEventDispatcher&\PHPUnit\Framework\MockObject\MockObject */
|
||||
private $eventDispatcher;
|
||||
|
||||
/** @var Generator */
|
||||
private $generator;
|
||||
|
||||
private LoggerInterface|\PHPUnit\Framework\MockObject\MockObject $logger;
|
||||
private LoggerInterface&\PHPUnit\Framework\MockObject\MockObject $logger;
|
||||
|
||||
protected function setUp(): void {
|
||||
parent::setUp();
|
||||
|
|
@ -196,18 +196,10 @@ class GeneratorTest extends \Test\TestCase {
|
|||
$previewFolder->method('getDirectoryListing')
|
||||
->willReturn([]);
|
||||
$previewFolder->method('newFile')
|
||||
->willReturnCallback(function ($filename) use ($maxPreview, $previewFile) {
|
||||
if ($filename === '2048-2048-max.png') {
|
||||
return $maxPreview;
|
||||
} elseif ($filename === '256-256.png') {
|
||||
return $previewFile;
|
||||
}
|
||||
$this->fail('Unexpected file');
|
||||
});
|
||||
|
||||
$maxPreview->expects($this->once())
|
||||
->method('putContent')
|
||||
->with($this->equalTo('my data'));
|
||||
->willReturnMap([
|
||||
['2048-2048-max.png', 'my data', $maxPreview],
|
||||
['256-256.png', 'my resized data', $previewFile],
|
||||
]);
|
||||
|
||||
$previewFolder->method('getFile')
|
||||
->with($this->equalTo('256-256.png'))
|
||||
|
|
@ -218,10 +210,6 @@ class GeneratorTest extends \Test\TestCase {
|
|||
->with($this->equalTo($maxPreview))
|
||||
->willReturn($image);
|
||||
|
||||
$previewFile->expects($this->once())
|
||||
->method('putContent')
|
||||
->with('my resized data');
|
||||
|
||||
$this->eventDispatcher->expects($this->once())
|
||||
->method('dispatchTyped')
|
||||
->with(new BeforePreviewFetchedEvent($file, 100, 100, false, IPreview::MODE_FILL, null));
|
||||
|
|
|
|||
Loading…
Reference in a new issue