Skip to content

Commit

Permalink
feat: added backend metadata interface, allows JSON storage
Browse files Browse the repository at this point in the history
Signed-off-by: Eduardo Morales <emoral435@gmail.com>
  • Loading branch information
emoral435 committed Mar 11, 2024
1 parent 88b40bb commit b2c8554
Show file tree
Hide file tree
Showing 6 changed files with 159 additions and 4 deletions.
19 changes: 19 additions & 0 deletions apps/files_versions/lib/Db/VersionEntity.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,23 @@ public function setLabel(string $label): void {
$this->metadata['label'] = $label;
$this->markFieldUpdated('metadata');
}

/**
* @abstract given a key, return the value associated with the key in the metadata column
* if nothing is found, we return an empty string
* @param string $key key associated with the value
*/
public function getMetadataValue(string $key): string {
return $this->metadata[$key] ?? '';
}

/**
* @abstract sets a key value pair in the metadata column
* @param string $key key associated with the value
* @param string $value value associated with the key
*/
public function setMetadataValue(string $key, $value): void {
$this->metadata[$key] = $value;
$this->markFieldUpdated('metadata');
}
}
2 changes: 1 addition & 1 deletion apps/files_versions/lib/Db/VersionsMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public function deleteAllVersionsForFileId(int $fileId): int {
->executeStatement();
}

public function deleteAllVersionsForUser(int $storageId, string $path = null): void {
public function deleteAllVersionsForUser(int $storageId, ?string $path = null): void {
$fileIdsGenerator = $this->getFileIdsGenerator($storageId, $path);

$versionEntitiesDeleteQuery = $this->db->getQueryBuilder();
Expand Down
38 changes: 38 additions & 0 deletions apps/files_versions/lib/Versions/IMetadataVersion.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

declare(strict_types=1);

/**
* @copyright Copyright (c) 2024 Eduardo Morales <emoral435@gmail.com>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
namespace OCA\Files_Versions\Versions;

/**
* This interface allows for just direct accessing of the metadata column JSON
* @since 29.0.0
*/
interface IMetadataVersion {
/**
* @abstract retrieves the metadata value from our $key param
*
* @param string $key the key for the json value of the metadata column
* @since 29.0.0
*/
public function getMetadataValue(string $key): string;
}
52 changes: 52 additions & 0 deletions apps/files_versions/lib/Versions/IMetadataVersionBackend.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

declare(strict_types=1);

/**
* @copyright Copyright (c) 2024 Eduardo Morales <emoral435@gmail.com>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
namespace OCA\Files_Versions\Versions;

use OCP\Files\Node;

/**
* This interface edits the metadata column of a node.
* Each column of the metadata has a key => value mapping.
* @since 29.0.0
*/
interface IMetadataVersionBackend {
/**
* Sets a key value pair in the metadata column corresponding to the node's version.
*
* @param Node $node the node that triggered the Metadata event listener, aka, the file version
* @param string $key the key for the json value of the metadata column
* @param string $value the value that corresponds to the key in the metadata column
* @since 29.0.0
*/
public function setMetadataValue(Node $node, string $key, string $value): void;

/**
* Retrieves a corresponding value from the metadata column using the key.
*
* @param Node $node the node that triggered the Metadata event listener, aka, the file version
* @param string $key the key for the json value of the metadata column
* @since 29.0.0
*/
public function getMetadataValue(Node $node, string $key): ?string;
}
32 changes: 31 additions & 1 deletion apps/files_versions/lib/Versions/LegacyVersionsBackend.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,14 @@
use OCP\Files\Folder;
use OCP\Files\IMimeTypeLoader;
use OCP\Files\IRootFolder;
use OCP\Files\Node;
use OCP\Files\NotFoundException;
use OCP\Files\Storage\IStorage;
use OCP\IUser;
use OCP\IUserManager;
use OCP\IUserSession;

class LegacyVersionsBackend implements IVersionBackend, INameableVersionBackend, IDeletableVersionBackend, INeedSyncVersionBackend {
class LegacyVersionsBackend implements IVersionBackend, INameableVersionBackend, IDeletableVersionBackend, INeedSyncVersionBackend, IMetadataVersionBackend {
private IRootFolder $rootFolder;
private IUserManager $userManager;
private VersionsMapper $versionsMapper;
Expand Down Expand Up @@ -312,4 +313,33 @@ private function currentUserHasPermissions(IVersion $version, int $permissions):

return ($sourceFile->getPermissions() & $permissions) === $permissions;
}

public function setMetadataValue(Node $node, string $key, string $value): void {
// Do not handle folders.
if ($node instanceof File) {

try {
$versionEntity = $this->versionsMapper->findVersionForFileId($node->getId(), $node->getMTime());
} catch (\Exception $e) {
throw $e; // the version does not exist or too many versions exist
}

$currentMetadata = $versionEntity->getMetadata() ?? [];

$currentMetadata[$key] = $value;
$versionEntity->setMetadata($currentMetadata);
$this->versionsMapper->update($versionEntity);
}

}

public function getMetadataValue(Node $node, string $key): ?string {
try {
$versionEntity = $this->versionsMapper->findVersionForFileId($node->getId(), $node->getMTime());
return $versionEntity->getMetadataValue($key);
} catch (\InvalidArgumentException $e) {
// we tried to find a version or key that doesn't exist
return null;
}
}
}
20 changes: 18 additions & 2 deletions apps/files_versions/lib/Versions/VersionManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,12 @@
use OCP\Files\Lock\ILock;
use OCP\Files\Lock\ILockManager;
use OCP\Files\Lock\LockContext;
use OCP\Files\Node;
use OCP\Files\Storage\IStorage;
use OCP\IUser;
use OCP\Lock\ManuallyLockedException;

class VersionManager implements IVersionManager, INameableVersionBackend, IDeletableVersionBackend, INeedSyncVersionBackend {
class VersionManager implements IVersionManager, INameableVersionBackend, IDeletableVersionBackend, INeedSyncVersionBackend, IMetadataVersionBackend {
/** @var (IVersionBackend[])[] */
private $backends = [];

Expand Down Expand Up @@ -160,6 +161,21 @@ public function deleteVersionsEntity(File $file): void {
}
}

public function setMetadataValue(Node $node, string $key, string $value): void {
$backend = $this->getBackendForStorage($node->getStorage());
if ($backend instanceof IMetadataVersionBackend) {
$backend->setMetadataValue($node, $key, $value);
}
}

public function getMetadataValue(Node $node, string $key): ?string {
$backend = $this->getBackendForStorage($node->getStorage());
if ($backend instanceof IMetadataVersionBackend) {
return $backend->getMetadataValue($node, $key);
}
return null;
}

/**
* Catch ManuallyLockedException and retry in app context if possible.
*
Expand All @@ -184,7 +200,7 @@ private static function handleAppLocks(callable $callback): ?bool {
return $callback();
} catch (ManuallyLockedException $e) {
$owner = (string) $e->getOwner();
$appsThatHandleUpdates = array("text", "richdocuments");
$appsThatHandleUpdates = ["text", "richdocuments"];
if (!in_array($owner, $appsThatHandleUpdates)) {
throw $e;
}
Expand Down

0 comments on commit b2c8554

Please sign in to comment.