Skip to content

Commit

Permalink
fix: fix moving files to trash
Browse files Browse the repository at this point in the history
Signed-off-by: Robin Appelman <robin@icewind.nl>
  • Loading branch information
icewind1991 committed Dec 12, 2024
1 parent 8adb8e9 commit 8e11625
Showing 1 changed file with 22 additions and 2 deletions.
24 changes: 22 additions & 2 deletions lib/Trash/TrashBackend.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

use OC\Files\Storage\Wrapper\Encryption;
use OCA\Files_Trashbin\Expiration;
use OCA\Files_Trashbin\Storage;
use OCA\Files_Trashbin\Trash\ITrashBackend;
use OCA\Files_Trashbin\Trash\ITrashItem;
use OCA\GroupFolders\ACL\ACLManagerFactory;
Expand Down Expand Up @@ -229,7 +230,14 @@ public function moveToTrash(IStorage $storage, string $internalPath): bool {
$name = basename($internalPath);
$fileEntry = $storage->getCache()->get($internalPath);
$folderId = $storage->getFolderId();
$trashFolder = $this->getTrashFolder($folderId);
$user = $this->userSession->getUser();
if (!$user) {
throw new \Exception("file moved to trash with no user in context");
}
// ensure the folder exists
$this->getTrashFolder($folderId);

$trashFolder = $this->rootFolder->get('/' . $user->getUID() . '/files_trashbin/groupfolders/' . $folderId);
$trashStorage = $trashFolder->getStorage();
$time = time();
$trashName = $name . '.d' . $time;
Expand All @@ -242,8 +250,15 @@ public function moveToTrash(IStorage $storage, string $internalPath): bool {
}
if ($result) {
$this->trashManager->addTrashItem($folderId, $name, $time, $internalPath, $fileEntry->getId());
if ($trashStorage->getCache()->getId($targetInternalPath) !== $fileEntry->getId()) {

// some storage backends (object/encryption) can either already move the cache item or cause the target to be scanned
// so we only conditionally do the cache move here
if (!$trashStorage->getCache()->inCache($targetInternalPath)) {
// doesn't exist in target yet, do the move
$trashStorage->getCache()->moveFromCache($storage->getCache(), $internalPath, $targetInternalPath);
} elseif ($storage->getCache()->inCache($internalPath)) {
// exists in both source and target, cleanup source
$storage->getCache()->remove($internalPath);
}
} else {
throw new \Exception("Failed to move groupfolder item to trash");
Expand All @@ -264,6 +279,11 @@ private function moveFromEncryptedStorage(IStorage $sourceStorage, IStorage $tar
return false;
}

// the trash should be the top wrapper, remove it to prevent recursive attempts to move to trash
if ($sourceStorage instanceof Storage) {
$sourceStorage = $sourceStorage->getWrapperStorage();
}

$result = $targetStorage->copyFromStorage($sourceStorage, $sourceInternalPath, $targetInternalPath, true);
if ($result) {
if ($sourceStorage->instanceOfStorage(ObjectStoreStorage::class)) {
Expand Down

0 comments on commit 8e11625

Please sign in to comment.