Skip to content

Commit

Permalink
Support setting metadata in PROPPATCH requests
Browse files Browse the repository at this point in the history
Signed-off-by: Louis Chemineau <louis@chmn.me>
  • Loading branch information
artonge committed Oct 25, 2023
1 parent e7ac6e4 commit 34daa84
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 1 deletion.
53 changes: 53 additions & 0 deletions apps/dav/lib/Connector/Sabre/FilesPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,11 @@
namespace OCA\DAV\Connector\Sabre;

use OC\AppFramework\Http\Request;
use OC\FilesMetadata\Model\MetadataValueWrapper;
use OCP\Constants;
use OCP\Files\ForbiddenException;
use OCP\Files\StorageNotAvailableException;
use OCP\FilesMetadata\IFilesMetadataManager;
use OCP\IConfig;
use OCP\IPreview;
use OCP\IRequest;
Expand Down Expand Up @@ -516,6 +518,57 @@ public function handleUpdateProperties($path, PropPatch $propPatch) {
$node->setCreationTime((int) $time);
return true;
});


/** @var IFilesMetadataManager */
$filesMetadataManager = \OCP\Server::get(IFilesMetadataManager::class);
$metadata = $filesMetadataManager->getMetadata((int)$node->getFileId());

foreach ($metadata->getKeys() as $metadataKey) {
$propPatch->handle(self::FILE_METADATA_PREFIX.$metadataKey, function (mixed $value) use ($metadata, $metadataKey, $filesMetadataManager) {
switch ($metadata->getType($metadataKey)) {
case MetadataValueWrapper::TYPE_STRING:
$metadata->set($metadataKey, $value);
break;
case MetadataValueWrapper::TYPE_INT:
$metadata->setInt($metadataKey, $value);
break;
case MetadataValueWrapper::TYPE_FLOAT:
$metadata->setFloat($metadataKey, $value);
break;
case MetadataValueWrapper::TYPE_BOOL:
$metadata->setBool($metadataKey, $value);
break;
case MetadataValueWrapper::TYPE_ARRAY:
$metadata->setArray($metadataKey, $value);
break;
case MetadataValueWrapper::TYPE_STRING_LIST:
$metadata->setStringList($metadataKey, $value);
break;
case MetadataValueWrapper::TYPE_INT_LIST:
$metadata->setIntList($metadataKey, $value);
break;
}

$filesMetadataManager->saveMetadata($metadata);
return true;
});
}

foreach ($propPatch->getRemainingMutations() as $mutation) {
if (!str_starts_with($mutation, self::FILE_METADATA_PREFIX)) {
continue;
}

$propPatch->handle($mutation, function (string $value) use ($metadata, $mutation, $filesMetadataManager) {
$metadataKey = substr($mutation, strlen(self::FILE_METADATA_PREFIX));
$metadata->set($metadataKey, $value);
$filesMetadataManager->saveMetadata($metadata);
return true;
});
}


/**
* Disable modification of the displayname property for files and
* folders via PROPPATCH. See PROPFIND for more information.
Expand Down
2 changes: 1 addition & 1 deletion lib/public/Files/FileInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
*/
namespace OCP\Files;

use OC\FilesMetadata\Model\MetadataValueWrapper;
use OCP\Files\Storage\IStorage;

/**
Expand Down Expand Up @@ -312,7 +313,6 @@ public function getParentId(): int;
/**
* Get the metadata, if available
*
* @return array
* @since 28.0.0
*/
public function getMetadata(): array;
Expand Down

0 comments on commit 34daa84

Please sign in to comment.