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 authored and backportbot[bot] committed Dec 11, 2024
1 parent 262644a commit fde94e6
Showing 1 changed file with 18 additions and 3 deletions.
21 changes: 18 additions & 3 deletions lib/Trash/TrashBackend.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,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 @@ -206,7 +207,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 @@ -218,9 +226,16 @@ public function moveToTrash(IStorage $storage, string $internalPath): bool {
$result = $trashStorage->moveFromStorage($storage, $internalPath, $targetInternalPath);
}
if ($result) {
$this->trashManager->addTrashItem($folderId, $name, $time, $internalPath, $fileEntry->getId(), $this->userSession->getUser()->getUID());
if ($trashStorage->getCache()->getId($targetInternalPath) !== $fileEntry->getId()) {
$this->trashManager->addTrashItem($folderId, $name, $time, $internalPath, $fileEntry->getId(), $user->getUID());

// 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 Down

0 comments on commit fde94e6

Please sign in to comment.