From 6d5fb1772ff003f2d2a708c38272443a1509fbf7 Mon Sep 17 00:00:00 2001 From: skalidindi53 Date: Thu, 15 Aug 2024 11:27:10 -0500 Subject: [PATCH 1/3] feat: Added blurhash to image metadata Signed-off-by: skalidindi53 --- lib/Chat/Parser/SystemMessage.php | 4 ++ lib/ResponseDefinitions.php | 1 + lib/Share/Helper/FilesMetadataCache.php | 10 ++- openapi-backend-sipbridge.json | 3 + openapi-federation.json | 3 + openapi-full.json | 3 + openapi.json | 3 + tests/php/Chat/Parser/SystemMessageTest.php | 77 +++++++++++++++++++++ 8 files changed, 102 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/ResponseDefinitions.php b/lib/ResponseDefinitions.php index c00ee108b2f..f690e01829e 100644 --- a/lib/ResponseDefinitions.php +++ b/lib/ResponseDefinitions.php @@ -91,6 +91,7 @@ * permissions?: string, * width?: string, * height?: string, + * blurhash?: string, * } * * @psalm-type TalkBaseMessage = array{ diff --git a/lib/Share/Helper/FilesMetadataCache.php b/lib/Share/Helper/FilesMetadataCache.php index aed10ed4e28..94f7db6d5f3 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/openapi-backend-sipbridge.json b/openapi-backend-sipbridge.json index 8d904486240..ce5268b859b 100644 --- a/openapi-backend-sipbridge.json +++ b/openapi-backend-sipbridge.json @@ -495,6 +495,9 @@ }, "height": { "type": "string" + }, + "blurhash": { + "type": "string" } } }, diff --git a/openapi-federation.json b/openapi-federation.json index 246aac982c1..9e0c2eebee1 100644 --- a/openapi-federation.json +++ b/openapi-federation.json @@ -549,6 +549,9 @@ }, "height": { "type": "string" + }, + "blurhash": { + "type": "string" } } }, diff --git a/openapi-full.json b/openapi-full.json index 129b99a23b2..c2f4e4d592b 100644 --- a/openapi-full.json +++ b/openapi-full.json @@ -1083,6 +1083,9 @@ }, "height": { "type": "string" + }, + "blurhash": { + "type": "string" } } }, diff --git a/openapi.json b/openapi.json index ef79433e22d..874479f2eab 100644 --- a/openapi.json +++ b/openapi.json @@ -970,6 +970,9 @@ }, "height": { "type": "string" + }, + "blurhash": { + "type": "string" } } }, 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); From dfc2b70e46bfb291fe69e9fa55c958db5cc77563 Mon Sep 17 00:00:00 2001 From: skalidindi53 Date: Mon, 19 Aug 2024 10:01:53 -0500 Subject: [PATCH 2/3] fix: openai changes Signed-off-by: skalidindi53 --- src/types/openapi/openapi-backend-sipbridge.ts | 1 + src/types/openapi/openapi-federation.ts | 1 + src/types/openapi/openapi-full.ts | 1 + src/types/openapi/openapi.ts | 1 + 4 files changed, 4 insertions(+) diff --git a/src/types/openapi/openapi-backend-sipbridge.ts b/src/types/openapi/openapi-backend-sipbridge.ts index e01255b5f30..686128647a0 100644 --- a/src/types/openapi/openapi-backend-sipbridge.ts +++ b/src/types/openapi/openapi-backend-sipbridge.ts @@ -242,6 +242,7 @@ export type components = { permissions?: string; width?: string; height?: string; + blurhash?: string; }; Room: { actorId: string; diff --git a/src/types/openapi/openapi-federation.ts b/src/types/openapi/openapi-federation.ts index 6a8190e05ed..8af3187829c 100644 --- a/src/types/openapi/openapi-federation.ts +++ b/src/types/openapi/openapi-federation.ts @@ -289,6 +289,7 @@ export type components = { permissions?: string; width?: string; height?: string; + blurhash?: string; }; Room: { actorId: string; diff --git a/src/types/openapi/openapi-full.ts b/src/types/openapi/openapi-full.ts index 4ce32fd3cfc..3ad0bb8c8cb 100644 --- a/src/types/openapi/openapi-full.ts +++ b/src/types/openapi/openapi-full.ts @@ -2082,6 +2082,7 @@ export type components = { permissions?: string; width?: string; height?: string; + blurhash?: string; }; Room: { actorId: string; diff --git a/src/types/openapi/openapi.ts b/src/types/openapi/openapi.ts index cbd10a1422b..0b8d8bfc954 100644 --- a/src/types/openapi/openapi.ts +++ b/src/types/openapi/openapi.ts @@ -1563,6 +1563,7 @@ export type components = { permissions?: string; width?: string; height?: string; + blurhash?: string; }; Room: { actorId: string; From 51d419bf54d6deddeb95d47623e442aec3d85827 Mon Sep 17 00:00:00 2001 From: skalidindi53 Date: Tue, 20 Aug 2024 10:24:41 -0500 Subject: [PATCH 3/3] fix: Renamed getMetadataPhotosSizeForFileId to getImageMetadataForFileId Signed-off-by: skalidindi53 --- lib/Chat/Parser/SystemMessage.php | 2 +- lib/Share/Helper/FilesMetadataCache.php | 2 +- tests/php/Chat/Parser/SystemMessageTest.php | 8 ++++---- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/lib/Chat/Parser/SystemMessage.php b/lib/Chat/Parser/SystemMessage.php index d6f5b5ac13c..6e78f89010e 100644 --- a/lib/Chat/Parser/SystemMessage.php +++ b/lib/Chat/Parser/SystemMessage.php @@ -812,7 +812,7 @@ protected function getFileFromShare(Room $room, ?Participant $participant, strin // If a preview is available, check if we can get the dimensions of the file from the metadata API if ($isPreviewAvailable && str_starts_with($node->getMimeType(), 'image/')) { try { - $sizeMetadata = $this->metadataCache->getMetadataPhotosSizeForFileId($fileId); + $sizeMetadata = $this->metadataCache->getImageMetadataForFileId($fileId); if (isset($sizeMetadata['width'], $sizeMetadata['height'])) { $data['width'] = (string) $sizeMetadata['width']; $data['height'] = (string) $sizeMetadata['height']; diff --git a/lib/Share/Helper/FilesMetadataCache.php b/lib/Share/Helper/FilesMetadataCache.php index 94f7db6d5f3..5bf3bf7aed5 100644 --- a/lib/Share/Helper/FilesMetadataCache.php +++ b/lib/Share/Helper/FilesMetadataCache.php @@ -44,7 +44,7 @@ public function preloadMetadata(array $fileIds): void { * @psalm-return array{width: int, height: int, blurhash?: string} * @throws FilesMetadataNotFoundException */ - public function getMetadataPhotosSizeForFileId(int $fileId): array { + public function getImageMetadataForFileId(int $fileId): array { if (!array_key_exists($fileId, $this->filesSizeData)) { try { $this->cachePhotosSize($fileId, $this->filesMetadataManager->getMetadata($fileId, true)); diff --git a/tests/php/Chat/Parser/SystemMessageTest.php b/tests/php/Chat/Parser/SystemMessageTest.php index 3c1ba28e300..1d4f3c96343 100644 --- a/tests/php/Chat/Parser/SystemMessageTest.php +++ b/tests/php/Chat/Parser/SystemMessageTest.php @@ -635,7 +635,7 @@ public function testGetFileFromShareForGuest(): void { ->willReturn(true); $this->filesMetadataCache->expects($this->once()) - ->method('getMetadataPhotosSizeForFileId') + ->method('getImageMetadataForFileId') ->with(54) ->willReturn(['width' => 1234, 'height' => 4567]); @@ -706,7 +706,7 @@ public function testGetFileFromShareForGuestWithBlurhash(): void { ->willReturn(true); $this->filesMetadataCache->expects($this->once()) - ->method('getMetadataPhotosSizeForFileId') + ->method('getImageMetadataForFileId') ->with(54) ->willReturn([ 'width' => 1234, @@ -786,7 +786,7 @@ public function testGetFileFromShareForOwner(): void { ->willReturn('absolute-link-owner'); $this->filesMetadataCache->expects($this->never()) - ->method('getMetadataPhotosSizeForFileId'); + ->method('getImageMetadataForFileId'); $participant = $this->createMock(Participant::class); $attendee = Attendee::fromRow([ @@ -873,7 +873,7 @@ public function testGetFileFromShareForRecipient(): void { ->willReturn(false); $this->filesMetadataCache->expects($this->never()) - ->method('getMetadataPhotosSizeForFileId'); + ->method('getImageMetadataForFileId'); $this->url->expects($this->once()) ->method('linkToRouteAbsolute')