Skip to content

Commit

Permalink
Merge pull request #13009 from nextcloud/skalidindi53/12351/Add-blurh…
Browse files Browse the repository at this point in the history
…ash-to-image-metadata

feat: Added blurhash to image metadata
  • Loading branch information
skalidindi53 committed Aug 20, 2024
2 parents 0933ec1 + 51d419b commit 73a0aeb
Show file tree
Hide file tree
Showing 12 changed files with 111 additions and 7 deletions.
6 changes: 5 additions & 1 deletion lib/Chat/Parser/SystemMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -814,11 +814,15 @@ 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'];
}

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
12 changes: 9 additions & 3 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,10 +41,10 @@ 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 {
public function getImageMetadataForFileId(int $fileId): array {
if (!array_key_exists($fileId, $this->filesSizeData)) {
try {
$this->cachePhotosSize($fileId, $this->filesMetadataManager->getMetadata($fileId, true));
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
1 change: 1 addition & 0 deletions src/types/openapi/openapi-backend-sipbridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,7 @@ export type components = {
permissions?: string;
width?: string;
height?: string;
blurhash?: string;
};
Room: {
actorId: string;
Expand Down
1 change: 1 addition & 0 deletions src/types/openapi/openapi-federation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,7 @@ export type components = {
permissions?: string;
width?: string;
height?: string;
blurhash?: string;
};
Room: {
actorId: string;
Expand Down
1 change: 1 addition & 0 deletions src/types/openapi/openapi-full.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2082,6 +2082,7 @@ export type components = {
permissions?: string;
width?: string;
height?: string;
blurhash?: string;
};
Room: {
actorId: string;
Expand Down
1 change: 1 addition & 0 deletions src/types/openapi/openapi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1563,6 +1563,7 @@ export type components = {
permissions?: string;
width?: string;
height?: string;
blurhash?: string;
};
Room: {
actorId: string;
Expand Down
83 changes: 80 additions & 3 deletions tests/php/Chat/Parser/SystemMessageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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]);

Expand All @@ -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('getImageMetadataForFileId')
->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 Expand Up @@ -709,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([
Expand Down Expand Up @@ -796,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')
Expand Down

0 comments on commit 73a0aeb

Please sign in to comment.