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 15, 2024
1 parent fb4e297 commit aa74c4f
Show file tree
Hide file tree
Showing 3 changed files with 89 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
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}

Check failure on line 44 in lib/Share/Helper/FilesMetadataCache.php

View workflow job for this annotation

GitHub Actions / static-psalm-analysis

UndefinedDocblockClass

lib/Share/Helper/FilesMetadataCache.php:44:19: UndefinedDocblockClass: Docblock-defined class, interface or enum named OCA\Talk\Share\Helper\intblurhash does not exist (see https://psalm.dev/200)
* @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
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 aa74c4f

Please sign in to comment.