Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Add metadata to file parameters
Signed-off-by: Marcel Müller <[email protected]>
  • Loading branch information
SystemKeeper authored and nickvergessen committed Dec 5, 2023
commit fda6853aeb74bd469d4da490682a2f9404415dab
27 changes: 25 additions & 2 deletions lib/Chat/Parser/SystemMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@
use OCP\Files\IRootFolder;
use OCP\Files\Node;
use OCP\Files\NotFoundException;
use OCP\FilesMetadata\Exceptions\FilesMetadataNotFoundException;
use OCP\FilesMetadata\IFilesMetadataManager;
use OCP\IGroup;
use OCP\IGroupManager;
use OCP\IL10N;
Expand Down Expand Up @@ -85,6 +87,7 @@ public function __construct(
protected IRootFolder $rootFolder,
protected ICloudIdManager $cloudIdManager,
protected IURLGenerator $url,
protected IFilesMetadataManager $metadataManager,
) {
}

Expand Down Expand Up @@ -719,19 +722,39 @@ protected function getFileFromShare(?Participant $participant, string $shareId):
]);
}

$fileId = $node->getId();
$isPreviewAvailable = $this->previewManager->isAvailable($node);

$data = [
'type' => 'file',
'id' => (string) $node->getId(),
'id' => (string) $fileId,
'name' => $name,
'size' => $size,
'path' => $path,
'link' => $url,
'etag' => $node->getEtag(),
'permissions' => $node->getPermissions(),
'mimetype' => $node->getMimeType(),
'preview-available' => $this->previewManager->isAvailable($node) ? 'yes' : 'no',
'preview-available' => $isPreviewAvailable ? 'yes' : 'no',
];

// If a preview is available, check if we can get the dimensions of the file from the metadata API
if ($isPreviewAvailable) {
try {
$metadata = $this->metadataManager->getMetaData($fileId, false);

if ($metadata->hasKey('photos-size')) {
$sizeMetadata = $metadata->getArray('photos-size');

if (isset($sizeMetadata['width']) && isset($sizeMetadata['height'])) {
$data['width'] = $sizeMetadata['width'];
$data['height'] = $sizeMetadata['height'];
}
}
} catch (FilesMetadataNotFoundException $e) {
}
}

if ($node->getMimeType() === 'text/vcard') {
$vCard = $node->getContent();

Expand Down
39 changes: 35 additions & 4 deletions tests/php/Chat/Parser/SystemMessageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@
use OCP\Files\IRootFolder;
use OCP\Files\Node;
use OCP\Files\NotFoundException;
use OCP\FilesMetadata\IFilesMetadataManager;
use OCP\FilesMetadata\Model\IFilesMetadata;
use OCP\IGroup;
use OCP\IGroupManager;
use OCP\IL10N;
Expand Down Expand Up @@ -75,6 +77,8 @@ class SystemMessageTest extends TestCase {
protected $url;
/** @var ICloudIdManager|MockObject */
protected $cloudIdManager;
/** @var IFilesMetadataManager|MockObject */
protected $metadataManager;
/** @var IL10N|MockObject */
protected $l;

Expand All @@ -91,6 +95,7 @@ public function setUp(): void {
$this->rootFolder = $this->createMock(IRootFolder::class);
$this->url = $this->createMock(IURLGenerator::class);
$this->cloudIdManager = $this->createMock(ICloudIdManager::class);
$this->metadataManager = $this->createMock(IFilesMetadataManager::class);
$this->l = $this->createMock(IL10N::class);
$this->l->method('t')
->willReturnCallback(function ($text, $parameters = []) {
Expand Down Expand Up @@ -121,6 +126,7 @@ protected function getParser(array $methods = []): SystemMessage {
$this->rootFolder,
$this->cloudIdManager,
$this->url,
$this->metadataManager,
])
->onlyMethods($methods)
->getMock();
Expand All @@ -137,7 +143,8 @@ protected function getParser(array $methods = []): SystemMessage {
$this->photoCache,
$this->rootFolder,
$this->cloudIdManager,
$this->url
$this->url,
$this->metadataManager,
);
}

Expand Down Expand Up @@ -611,7 +618,7 @@ public function testGetFileFromShareForGuest() {
$node = $this->createMock(Node::class);
$node->expects($this->once())
->method('getId')
->willReturn('54');
->willReturn(54);
$node->expects($this->once())
->method('getName')
->willReturn('name');
Expand Down Expand Up @@ -653,6 +660,22 @@ public function testGetFileFromShareForGuest() {
->with($node)
->willReturn(true);

$metadata = $this->createMock(IFilesMetadata::class);
$metadata->expects($this->once())
->method('hasKey')
->with('photos-size')
->willReturn(true);

$metadata->expects($this->once())
->method('getArray')
->with('photos-size')
->willReturn(['width' => 1234, 'height' => 4567]);

$this->metadataManager->expects($this->once())
->method('getMetaData')
->with(54, false)
->willReturn($metadata);

$participant = $this->createMock(Participant::class);
$participant->expects($this->once())
->method('isGuest')
Expand All @@ -670,14 +693,16 @@ public function testGetFileFromShareForGuest() {
'permissions' => 27,
'mimetype' => 'text/plain',
'preview-available' => 'yes',
'width' => 1234,
'height' => 4567,
], self::invokePrivate($parser, 'getFileFromShare', [$participant, '23']));
}

public function testGetFileFromShareForOwner() {
$node = $this->createMock(Node::class);
$node->expects($this->exactly(2))
->method('getId')
->willReturn('54');
->willReturn(54);
$node->expects($this->once())
->method('getName')
->willReturn('name');
Expand Down Expand Up @@ -722,6 +747,9 @@ public function testGetFileFromShareForOwner() {
])
->willReturn('absolute-link-owner');

$this->metadataManager->expects($this->never())
->method('getMetaData');

$participant = $this->createMock(Participant::class);
$participant->expects($this->once())
->method('isGuest')
Expand Down Expand Up @@ -775,7 +803,7 @@ public function testGetFileFromShareForRecipient() {
$file = $this->createMock(Node::class);
$file->expects($this->any())
->method('getId')
->willReturn('54');
->willReturn(54);
$file->expects($this->once())
->method('getName')
->willReturn('different');
Expand Down Expand Up @@ -811,6 +839,9 @@ public function testGetFileFromShareForRecipient() {
->with($file)
->willReturn(false);

$this->metadataManager->expects($this->never())
->method('getMetaData');

$this->url->expects($this->once())
->method('linkToRouteAbsolute')
->with('files.viewcontroller.showFile', [
Expand Down