From 89c782f781f638605e0ca8a13e5ff6a26f841fa3 Mon Sep 17 00:00:00 2001 From: Robin Appelman Date: Thu, 20 Jun 2024 15:57:59 +0200 Subject: [PATCH] fix: write object to the correct urn when moving from another storage to object store Signed-off-by: Robin Appelman --- .../Files/ObjectStore/ObjectStoreStorage.php | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/lib/private/Files/ObjectStore/ObjectStoreStorage.php b/lib/private/Files/ObjectStore/ObjectStoreStorage.php index d0e3562797309..b7697c76de475 100644 --- a/lib/private/Files/ObjectStore/ObjectStoreStorage.php +++ b/lib/private/Files/ObjectStore/ObjectStoreStorage.php @@ -581,6 +581,31 @@ public function copyFromStorage(IStorage $sourceStorage, $sourceInternalPath, $t return parent::copyFromStorage($sourceStorage, $sourceInternalPath, $targetInternalPath); } + public function moveFromStorage(IStorage $sourceStorage, $sourceInternalPath, $targetInternalPath, ?ICacheEntry $sourceCacheEntry = null): bool { + $sourceCache = $sourceStorage->getCache(); + if (!$sourceCacheEntry) { + $sourceCacheEntry = $sourceCache->get($sourceInternalPath); + } + if ($sourceCacheEntry->getMimeType() === FileInfo::MIMETYPE_FOLDER) { + foreach ($sourceCache->getFolderContents($sourceInternalPath) as $child) { + $this->moveFromStorage($sourceStorage, $child->getPath(), $targetInternalPath . '/' . $child->getName()); + } + $sourceStorage->rmdir($sourceInternalPath); + } else { + // move the cache entry before the contents so that we have the correct fileid/urn for the target + $this->getCache()->moveFromCache($sourceCache, $sourceInternalPath, $targetInternalPath); + try { + $this->writeStream($targetInternalPath, $sourceStorage->fopen($sourceInternalPath, 'r'), $sourceCacheEntry->getSize()); + } catch (\Exception $e) { + // restore the cache entry + $sourceCache->moveFromCache($this->getCache(), $targetInternalPath, $sourceInternalPath); + throw $e; + } + $sourceStorage->unlink($sourceInternalPath); + } + return true; + } + public function copy($source, $target) { $source = $this->normalizePath($source); $target = $this->normalizePath($target);