Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
13 changes: 12 additions & 1 deletion lib/private/Files/ObjectStore/ObjectStoreStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -592,7 +592,14 @@ public function copyFromStorage(

public function moveFromStorage(IStorage $sourceStorage, string $sourceInternalPath, string $targetInternalPath, ?ICacheEntry $sourceCacheEntry = null): bool {
$sourceCache = $sourceStorage->getCache();
if ($sourceStorage->instanceOfStorage(ObjectStoreStorage::class) && $sourceStorage->getObjectStore()->getStorageId() === $this->getObjectStore()->getStorageId()) {
if (
$sourceStorage->instanceOfStorage(ObjectStoreStorage::class) &&
$sourceStorage->getObjectStore()->getStorageId() === $this->getObjectStore()->getStorageId()
) {
if ($this->getCache()->get($targetInternalPath)) {
$this->unlink($targetInternalPath);
$this->getCache()->remove($targetInternalPath);
}
$this->getCache()->moveFromCache($sourceCache, $sourceInternalPath, $targetInternalPath);
// Do not import any data when source and target bucket are identical.
return true;
Expand All @@ -615,6 +622,10 @@ public function moveFromStorage(IStorage $sourceStorage, string $sourceInternalP
/** @var ObjectStoreStorage $sourceStorage */
$sourceStorage->setPreserveCacheOnDelete(false);
}
if ($this->getCache()->get($targetInternalPath)) {
$this->unlink($targetInternalPath);
$this->getCache()->remove($targetInternalPath);
}
$this->getCache()->moveFromCache($sourceCache, $sourceInternalPath, $targetInternalPath);

return true;
Expand Down
18 changes: 18 additions & 0 deletions tests/lib/Files/Storage/StoragesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,24 @@ public function testMoveFileFromStorage() {
$this->assertEquals('foo', $this->storage1->file_get_contents($target));
}

public function testMoveFileFromStorageWithExistingTarget() {
$source = 'source.txt';
$target = 'target.txt';
$this->storage1->file_put_contents($target, 'bar');
$this->storage2->file_put_contents($source, 'foo');

$targetURN = $this->storage1->getURN($this->storage1->getCache()->get($target)->getID());
$sourceURN = $this->storage2->getURN($this->storage2->getCache()->get($source)->getID());

$this->storage1->moveFromStorage($this->storage2, $source, $target);

$this->assertTrue($this->storage1->file_exists($target), $target . ' was not created in DB');
$this->assertFalse($this->storage2->file_exists($source), $source . ' still exists in DB');
$this->assertTrue($this->storage1->getObjectStore()->objectExists($sourceURN), $sourceURN . ' was not created in bucket');
$this->assertFalse($this->storage1->getObjectStore()->objectExists($targetURN), $targetURN . ' still exists in bucket');
$this->assertEquals('foo', $this->storage1->file_get_contents($target));
}

public function testMoveDirectoryFromStorage() {
$this->storage2->mkdir('source');
$this->storage2->file_put_contents('source/test1.txt', 'foo');
Expand Down
Loading