From aa74c4fb1f6523655f346c6e68fb0158956b1eae Mon Sep 17 00:00:00 2001 From: skalidindi53 Date: Thu, 15 Aug 2024 11:27:10 -0500 Subject: [PATCH] feat: Added blurhash to image metadata Signed-off-by: skalidindi53 --- lib/Chat/Parser/SystemMessage.php | 4 ++ lib/Share/Helper/FilesMetadataCache.php | 10 ++- tests/php/Chat/Parser/SystemMessageTest.php | 77 +++++++++++++++++++++ 3 files changed, 89 insertions(+), 2 deletions(-) diff --git a/lib/Chat/Parser/SystemMessage.php b/lib/Chat/Parser/SystemMessage.php index d498612299c..d6f5b5ac13c 100644 --- a/lib/Chat/Parser/SystemMessage.php +++ b/lib/Chat/Parser/SystemMessage.php @@ -817,6 +817,10 @@ protected function getFileFromShare(Room $room, ?Participant $participant, strin $data['width'] = (string) $sizeMetadata['width']; $data['height'] = (string) $sizeMetadata['height']; } + + if (isset($sizeMetadata['blurhash'])) { + $data['blurhash'] = $sizeMetadata['blurhash']; + } } catch (FilesMetadataNotFoundException) { } } diff --git a/lib/Share/Helper/FilesMetadataCache.php b/lib/Share/Helper/FilesMetadataCache.php index aed10ed4e28..bb846c69d52 100644 --- a/lib/Share/Helper/FilesMetadataCache.php +++ b/lib/Share/Helper/FilesMetadataCache.php @@ -15,7 +15,7 @@ use OCP\FilesMetadata\Model\IFilesMetadata; class FilesMetadataCache { - /** @var array */ + /** @var array */ protected array $filesSizeData = []; public function __construct( @@ -41,7 +41,7 @@ public function preloadMetadata(array $fileIds): void { /** * @param int $fileId * @return array - * @psalm-return array{width: int, height: int} + * @psalm-return array{width: int, height: int blurhash?: string} * @throws FilesMetadataNotFoundException */ public function getMetadataPhotosSizeForFileId(int $fileId): array { @@ -75,6 +75,12 @@ protected function cachePhotosSize(int $fileId, IFilesMetadata $metadata): void 'width' => $sizeMetadata['width'], 'height' => $sizeMetadata['height'], ]; + + // Retrieve Blurhash from metadata (if present) + if ($metadata->hasKey('blurhash')) { + $dimensions['blurhash'] = $metadata->getString('blurhash'); + } + $this->filesSizeData[$fileId] = $dimensions; } else { $this->filesSizeData[$fileId] = null; diff --git a/tests/php/Chat/Parser/SystemMessageTest.php b/tests/php/Chat/Parser/SystemMessageTest.php index ed61381fe44..3c1ba28e300 100644 --- a/tests/php/Chat/Parser/SystemMessageTest.php +++ b/tests/php/Chat/Parser/SystemMessageTest.php @@ -658,6 +658,83 @@ public function testGetFileFromShareForGuest(): void { ], self::invokePrivate($parser, 'getFileFromShare', [$room, $participant, '23'])); } + public function testGetFileFromShareForGuestWithBlurhash(): void { + $room = $this->createMock(Room::class); + $node = $this->createMock(Node::class); + $node->expects($this->once()) + ->method('getId') + ->willReturn(54); + $node->expects($this->once()) + ->method('getName') + ->willReturn('name'); + $node->expects($this->atLeastOnce()) + ->method('getMimeType') + ->willReturn('image/png'); + $node->expects($this->once()) + ->method('getSize') + ->willReturn(65530); + $node->expects($this->once()) + ->method('getEtag') + ->willReturn(md5('etag')); + $node->expects($this->once()) + ->method('getPermissions') + ->willReturn(27); + + $share = $this->createMock(IShare::class); + $share->expects($this->once()) + ->method('getNode') + ->willReturn($node); + $share->expects($this->once()) + ->method('getToken') + ->willReturn('token'); + + $this->shareProvider->expects($this->once()) + ->method('getShareById') + ->with('23') + ->willReturn($share); + + $this->url->expects($this->once()) + ->method('linkToRouteAbsolute') + ->with('files_sharing.sharecontroller.showShare', [ + 'token' => 'token', + ]) + ->willReturn('absolute-link'); + + $this->previewManager->expects($this->once()) + ->method('isAvailable') + ->with($node) + ->willReturn(true); + + $this->filesMetadataCache->expects($this->once()) + ->method('getMetadataPhotosSizeForFileId') + ->with(54) + ->willReturn([ + 'width' => 1234, + 'height' => 4567, + 'blurhash' => 'LEHV9uae2yk8pyo0adR*.7kCMdnj' + ]); + + $participant = $this->createMock(Participant::class); + + $parser = $this->getParser(); + + $this->assertSame([ + 'type' => 'file', + 'id' => '54', + 'name' => 'name', + 'size' => '65530', + 'path' => 'name', + 'link' => 'absolute-link', + 'etag' => '1872ade88f3013edeb33decd74a4f947', + 'permissions' => '27', + 'mimetype' => 'image/png', + 'preview-available' => 'yes', + 'width' => '1234', + 'height' => '4567', + 'blurhash' => 'LEHV9uae2yk8pyo0adR*.7kCMdnj', + ], self::invokePrivate($parser, 'getFileFromShare', [$room, $participant, '23'])); + } + public function testGetFileFromShareForOwner(): void { $room = $this->createMock(Room::class); $node = $this->createMock(Node::class);