Skip to content

Commit

Permalink
IFilesMetadata
Browse files Browse the repository at this point in the history
Signed-off-by: Maxence Lange <maxence@artificial-owl.com>
  • Loading branch information
ArtificialOwl committed Nov 7, 2023
1 parent e62e9e3 commit b9640f1
Show file tree
Hide file tree
Showing 9 changed files with 27 additions and 14 deletions.
5 changes: 4 additions & 1 deletion apps/dav/tests/unit/Files/FileSearchBackendTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
use OCP\Files\Search\ISearchBinaryOperator;
use OCP\Files\Search\ISearchComparison;
use OCP\Files\Search\ISearchQuery;
use OCP\FilesMetadata\IFilesMetadataManager;
use OCP\IUser;
use OCP\Share\IManager;
use SearchDAV\Backend\SearchPropertyDefinition;
Expand Down Expand Up @@ -114,7 +115,9 @@ protected function setUp(): void {
->method('get')
->willReturn($this->searchFolder);

$this->search = new FileSearchBackend($this->tree, $this->user, $this->rootFolder, $this->shareManager, $this->view);
$filesMetadataManager = $this->createMock(IFilesMetadataManager::class);

$this->search = new FileSearchBackend($this->tree, $this->user, $this->rootFolder, $this->shareManager, $this->view, $filesMetadataManager);
}

public function testSearchFilename(): void {
Expand Down
4 changes: 2 additions & 2 deletions core/Command/FilesMetadata/Get.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
if ($input->getOption('reset')) {
$this->filesMetadataManager->deleteMetadata($fileId);
if (!$input->getOption('refresh')) {
return 0;
return self::SUCCESS;
}
}

Expand All @@ -114,6 +114,6 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$output->writeln(json_encode($metadata, JSON_PRETTY_PRINT));
}

return 0;
return self::SUCCESS;
}
}
2 changes: 0 additions & 2 deletions lib/private/Files/Node/Folder.php
Original file line number Diff line number Diff line change
Expand Up @@ -252,8 +252,6 @@ public function search($query) {
if ($order) {
usort($files, function (FileInfo $a, FileInfo $b) use ($order) {
foreach ($order as $orderField) {
// needed !?
// if ($orderField->isExtra()) { continue; }
$cmp = $orderField->sortFileInfo($a, $b);
if ($cmp !== 0) {
return $cmp;
Expand Down
9 changes: 5 additions & 4 deletions lib/private/FilesMetadata/FilesMetadataManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,7 @@ public function getKnownMetadata(): IFilesMetadata {
/**
* @param string $key metadata key
* @param string $type metadata type
* @param bool $indexed TRUE if metadata can be search
*
* @inheritDoc
* @since 28.0.0
Expand All @@ -253,17 +254,17 @@ public function getKnownMetadata(): IFilesMetadata {
* @see IMetadataValueWrapper::TYPE_INT_LIST
* @see IMetadataValueWrapper::TYPE_STRING
*/
public function initMetadataIndex(string $key, string $type): void {
public function initMetadata(string $key, string $type, bool $indexed): void {
$current = $this->getKnownMetadata();
try {
if ($current->getType($key) === $type && $current->isIndex($key)) {
return; // if key exists, with same type and is already indexed, we do nothing.
if ($current->getType($key) === $type && $indexed === $current->isIndex($key)) {
return; // if key exists, with same type and indexed, we do nothing.
}
} catch (FilesMetadataNotFoundException) {
// if value does not exist, we keep on the writing of course
}

$current->import([$key => ['type' => $type, 'indexed' => true]]);
$current->import([$key => ['type' => $type, 'indexed' => $indexed]]);
$this->config->setAppValue('core', self::CONFIG_KEY, json_encode($current));
}

Expand Down
5 changes: 4 additions & 1 deletion lib/private/FilesMetadata/Job/UpdateSingleMetadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
use OCP\Files\NotPermittedException;
use OCP\FilesMetadata\Event\MetadataLiveEvent;
use OCP\FilesMetadata\IFilesMetadataManager;
use Psr\Log\LoggerInterface;

/**
* Simple background job, created when requested by an app during the
Expand All @@ -47,6 +48,7 @@ public function __construct(
ITimeFactory $time,
private IRootFolder $rootFolder,
private FilesMetadataManager $filesMetadataManager,
private LoggerInterface $logger
) {
parent::__construct($time);
}
Expand All @@ -60,7 +62,8 @@ protected function run($argument) {
$file = array_shift($node);
$this->filesMetadataManager->refreshMetadata($file, IFilesMetadataManager::PROCESS_BACKGROUND);
}
} catch (NotPermittedException|NoUserException $e) {
} catch (\Exception $e) {
$this->logger->warning('issue while running UpdateSingleMetadata', ['exception' => $e, 'userId' => $userId, 'fileId' => $fileId]);
}
}
}
2 changes: 2 additions & 0 deletions lib/private/FilesMetadata/Listener/MetadataDelete.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
class MetadataDelete implements IEventListener {
public function __construct(
private IFilesMetadataManager $filesMetadataManager,
private LoggerInterface $logger

Check failure on line 42 in lib/private/FilesMetadata/Listener/MetadataDelete.php

View workflow job for this annotation

GitHub Actions / static-code-analysis

UndefinedClass

lib/private/FilesMetadata/Listener/MetadataDelete.php:42:3: UndefinedClass: Class, interface or enum named OC\FilesMetadata\Listener\LoggerInterface does not exist (see https://psalm.dev/019)

Check failure

Code scanning / Psalm

UndefinedClass Error

Class, interface or enum named OC\FilesMetadata\Listener\LoggerInterface does not exist
) {
}

Expand All @@ -56,6 +57,7 @@ public function handle(Event $event): void {
$this->filesMetadataManager->deleteMetadata($nodeId);
}
} catch (Exception $e) {
$this->logger->warning('issue while running MetadataDelete', ['exception' => $e]);

Check failure on line 60 in lib/private/FilesMetadata/Listener/MetadataDelete.php

View workflow job for this annotation

GitHub Actions / static-code-analysis

UndefinedClass

lib/private/FilesMetadata/Listener/MetadataDelete.php:60:4: UndefinedClass: Class, interface or enum named OC\FilesMetadata\Listener\LoggerInterface does not exist (see https://psalm.dev/019)

Check failure

Code scanning / Psalm

UndefinedClass Error

Class, interface or enum named OC\FilesMetadata\Listener\LoggerInterface does not exist
}
}
}
3 changes: 3 additions & 0 deletions lib/private/FilesMetadata/Listener/MetadataUpdate.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
use OCP\Files\Events\Node\NodeCreatedEvent;
use OCP\Files\Events\Node\NodeWrittenEvent;
use OCP\FilesMetadata\IFilesMetadataManager;
use Psr\Log\LoggerInterface;

/**
* Handle file creation/modification events and initiate a new event related to the created/edited file.
Expand All @@ -42,6 +43,7 @@
class MetadataUpdate implements IEventListener {
public function __construct(
private IFilesMetadataManager $filesMetadataManager,
private LoggerInterface $logger
) {
}

Expand All @@ -56,6 +58,7 @@ public function handle(Event $event): void {
try {
$this->filesMetadataManager->refreshMetadata($event->getNode());
} catch (Exception $e) {
$this->logger->warning('issue while running MetadataUpdate', ['exception' => $e]);
}
}
}
10 changes: 6 additions & 4 deletions lib/public/FilesMetadata/IFilesMetadataManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,9 @@
* @since 28.0.0
*/
interface IFilesMetadataManager {
/**
* @since 28.0.0
*/
/** @since 28.0.0 */
public const PROCESS_LIVE = 1;
/** @since 28.0.0 */
public const PROCESS_BACKGROUND = 2;

/**
Expand Down Expand Up @@ -128,10 +127,13 @@ public function getKnownMetadata(): IFilesMetadata;
/**
* initiate a metadata key with its type.
* The call is mandatory before using the metadata property in a webdav request.
* It is not needed to only use this method when the app is enabled: the method can be
* called each time during the app loading as the metadata will only be initiated if not known
*
* @param string $key metadata key
* @param string $type metadata type
* @param bool $indexed TRUE if metadata can be search
* @since 28.0.0
*/
public function initMetadataIndex(string $key, string $type): void;
public function initMetadata(string $key, string $type, bool $indexed): void;
}
1 change: 1 addition & 0 deletions lib/public/FilesMetadata/Model/IMetadataQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
* @since 28.0.0
*/
interface IMetadataQuery {
/** @since 28.0.0 */
public const EXTRA = 'metadata';

/**
Expand Down

0 comments on commit b9640f1

Please sign in to comment.