Skip to content

Commit

Permalink
feat: Added blurhash to image metadata
Browse files Browse the repository at this point in the history
Signed-off-by: skalidindi53 <[email protected]>
  • Loading branch information
skalidindi53 committed Aug 16, 2024
1 parent fb4e297 commit 6d5fb17
Show file tree
Hide file tree
Showing 8 changed files with 102 additions and 2 deletions.
4 changes: 4 additions & 0 deletions lib/Chat/Parser/SystemMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
}
}
Expand Down
1 change: 1 addition & 0 deletions lib/ResponseDefinitions.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@
* permissions?: string,
* width?: string,
* height?: string,
* blurhash?: string,
* }
*
* @psalm-type TalkBaseMessage = array{
Expand Down
10 changes: 8 additions & 2 deletions lib/Share/Helper/FilesMetadataCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
use OCP\FilesMetadata\Model\IFilesMetadata;

class FilesMetadataCache {
/** @var array<int, ?array{width: int, height: int}> */
/** @var array<int, ?array{width: int, height: int, blurhash?: string}> */
protected array $filesSizeData = [];

public function __construct(
Expand All @@ -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 {
Expand Down Expand Up @@ -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;
Expand Down
3 changes: 3 additions & 0 deletions openapi-backend-sipbridge.json
Original file line number Diff line number Diff line change
Expand Up @@ -495,6 +495,9 @@
},
"height": {
"type": "string"
},
"blurhash": {
"type": "string"
}
}
},
Expand Down
3 changes: 3 additions & 0 deletions openapi-federation.json
Original file line number Diff line number Diff line change
Expand Up @@ -549,6 +549,9 @@
},
"height": {
"type": "string"
},
"blurhash": {
"type": "string"
}
}
},
Expand Down
3 changes: 3 additions & 0 deletions openapi-full.json
Original file line number Diff line number Diff line change
Expand Up @@ -1083,6 +1083,9 @@
},
"height": {
"type": "string"
},
"blurhash": {
"type": "string"
}
}
},
Expand Down
3 changes: 3 additions & 0 deletions openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -970,6 +970,9 @@
},
"height": {
"type": "string"
},
"blurhash": {
"type": "string"
}
}
},
Expand Down
77 changes: 77 additions & 0 deletions tests/php/Chat/Parser/SystemMessageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 6d5fb17

Please sign in to comment.