Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Support X-NC-Skip-Trashbin header #50353

Merged
merged 1 commit into from
Jan 24, 2025
Merged
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
42 changes: 24 additions & 18 deletions apps/files_trashbin/lib/Storage.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,15 @@
use OC\Files\Storage\Wrapper\Wrapper;
use OCA\Files_Trashbin\Events\MoveToTrashEvent;
use OCA\Files_Trashbin\Trash\ITrashManager;
use OCP\App\IAppManager;
use OCP\Encryption\Exceptions\GenericEncryptionException;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\Files\IRootFolder;
use OCP\Files\Node;
use OCP\Files\Storage\IStorage;
use OCP\IRequest;
use OCP\IUserManager;
use OCP\Server;
use Psr\Log\LoggerInterface;

class Storage extends Wrapper {
Expand All @@ -33,6 +36,7 @@ public function __construct(
private ?LoggerInterface $logger = null,
private ?IEventDispatcher $eventDispatcher = null,
private ?IRootFolder $rootFolder = null,
private ?IRequest $request = null,
) {
$this->mountPoint = $parameters['mountPoint'];
parent::__construct($parameters);
Expand Down Expand Up @@ -118,26 +122,26 @@ protected function createMoveToTrashEvent(Node $node): MoveToTrashEvent {
* @return bool true if the operation succeeded, false otherwise
*/
private function doDelete(string $path, string $method): bool {
if (
!\OC::$server->getAppManager()->isEnabledForUser('files_trashbin')
|| (pathinfo($path, PATHINFO_EXTENSION) === 'part')
|| $this->shouldMoveToTrash($path) === false
) {
return call_user_func([$this->storage, $method], $path);
}
$isTrashbinEnabled = Server::get(IAppManager::class)->isEnabledForUser('files_trashbin');
$isPartFile = pathinfo($path, PATHINFO_EXTENSION) === 'part';
$isSkipTrashHeaderSet = $this->request !== null && $this->request->getHeader('X-NC-Skip-Trashbin') === 'true';
// We keep the shouldMoveToTrash call at the end to prevent emitting unnecessary event.
$shouldMoveToTrash = $isTrashbinEnabled && !$isPartFile && !$isSkipTrashHeaderSet && $this->shouldMoveToTrash($path);

if ($shouldMoveToTrash) {
// check permissions before we continue, this is especially important for
// shared files
if (!$this->isDeletable($path)) {
return false;
}

// check permissions before we continue, this is especially important for
// shared files
if (!$this->isDeletable($path)) {
return false;
$isMovedToTrash = $this->trashManager->moveToTrash($this, $path);
if ($isMovedToTrash) {
return true;
}
}

$isMovedToTrash = $this->trashManager->moveToTrash($this, $path);
if (!$isMovedToTrash) {
return call_user_func([$this->storage, $method], $path);
} else {
return true;
}
return call_user_func([$this->storage, $method], $path);
}

/**
Expand All @@ -149,16 +153,18 @@ public static function setupStorage(): void {
$logger = \OC::$server->get(LoggerInterface::class);
$eventDispatcher = \OC::$server->get(IEventDispatcher::class);
$rootFolder = \OC::$server->get(IRootFolder::class);
$request = \OC::$server->get(IRequest::class);
Filesystem::addStorageWrapper(
'oc_trashbin',
function (string $mountPoint, IStorage $storage) use ($trashManager, $userManager, $logger, $eventDispatcher, $rootFolder) {
function (string $mountPoint, IStorage $storage) use ($trashManager, $userManager, $logger, $eventDispatcher, $rootFolder, $request) {
return new Storage(
['storage' => $storage, 'mountPoint' => $mountPoint],
$trashManager,
$userManager,
$logger,
$eventDispatcher,
$rootFolder,
$request,
);
},
1);
Expand Down
Loading