Skip to content

Commit 2d02cb1

Browse files
artongeAndyScherzinger
authored andcommitted
feat: Support X-NC-Skip-Trashbin header
This is useful for clients that want to directly and permanently delete a file. Signed-off-by: Louis Chemineau <louis@chmn.me>
1 parent c8df47f commit 2d02cb1

File tree

1 file changed

+24
-18
lines changed

1 file changed

+24
-18
lines changed

apps/files_trashbin/lib/Storage.php

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,15 @@
1010
use OC\Files\Storage\Wrapper\Wrapper;
1111
use OCA\Files_Trashbin\Events\MoveToTrashEvent;
1212
use OCA\Files_Trashbin\Trash\ITrashManager;
13+
use OCP\App\IAppManager;
1314
use OCP\Encryption\Exceptions\GenericEncryptionException;
1415
use OCP\EventDispatcher\IEventDispatcher;
1516
use OCP\Files\IRootFolder;
1617
use OCP\Files\Node;
1718
use OCP\Files\Storage\IStorage;
19+
use OCP\IRequest;
1820
use OCP\IUserManager;
21+
use OCP\Server;
1922
use Psr\Log\LoggerInterface;
2023

2124
class Storage extends Wrapper {
@@ -33,6 +36,7 @@ public function __construct(
3336
private ?LoggerInterface $logger = null,
3437
private ?IEventDispatcher $eventDispatcher = null,
3538
private ?IRootFolder $rootFolder = null,
39+
private ?IRequest $request = null,
3640
) {
3741
$this->mountPoint = $parameters['mountPoint'];
3842
parent::__construct($parameters);
@@ -118,26 +122,26 @@ protected function createMoveToTrashEvent(Node $node): MoveToTrashEvent {
118122
* @return bool true if the operation succeeded, false otherwise
119123
*/
120124
private function doDelete(string $path, string $method): bool {
121-
if (
122-
!\OC::$server->getAppManager()->isEnabledForUser('files_trashbin')
123-
|| (pathinfo($path, PATHINFO_EXTENSION) === 'part')
124-
|| $this->shouldMoveToTrash($path) === false
125-
) {
126-
return call_user_func([$this->storage, $method], $path);
127-
}
125+
$isTrashbinEnabled = Server::get(IAppManager::class)->isEnabledForUser('files_trashbin');
126+
$isPartFile = pathinfo($path, PATHINFO_EXTENSION) === 'part';
127+
$isSkipTrashHeaderSet = $this->request !== null && $this->request->getHeader('X-NC-Skip-Trashbin') === 'true';
128+
// We keep the shouldMoveToTrash call at the end to prevent emitting unnecessary event.
129+
$shouldMoveToTrash = $isTrashbinEnabled && !$isPartFile && !$isSkipTrashHeaderSet && $this->shouldMoveToTrash($path);
130+
131+
if ($shouldMoveToTrash) {
132+
// check permissions before we continue, this is especially important for
133+
// shared files
134+
if (!$this->isDeletable($path)) {
135+
return false;
136+
}
128137

129-
// check permissions before we continue, this is especially important for
130-
// shared files
131-
if (!$this->isDeletable($path)) {
132-
return false;
138+
$isMovedToTrash = $this->trashManager->moveToTrash($this, $path);
139+
if ($isMovedToTrash) {
140+
return true;
141+
}
133142
}
134143

135-
$isMovedToTrash = $this->trashManager->moveToTrash($this, $path);
136-
if (!$isMovedToTrash) {
137-
return call_user_func([$this->storage, $method], $path);
138-
} else {
139-
return true;
140-
}
144+
return call_user_func([$this->storage, $method], $path);
141145
}
142146

143147
/**
@@ -149,16 +153,18 @@ public static function setupStorage(): void {
149153
$logger = \OC::$server->get(LoggerInterface::class);
150154
$eventDispatcher = \OC::$server->get(IEventDispatcher::class);
151155
$rootFolder = \OC::$server->get(IRootFolder::class);
156+
$request = \OC::$server->get(IRequest::class);
152157
Filesystem::addStorageWrapper(
153158
'oc_trashbin',
154-
function (string $mountPoint, IStorage $storage) use ($trashManager, $userManager, $logger, $eventDispatcher, $rootFolder) {
159+
function (string $mountPoint, IStorage $storage) use ($trashManager, $userManager, $logger, $eventDispatcher, $rootFolder, $request) {
155160
return new Storage(
156161
['storage' => $storage, 'mountPoint' => $mountPoint],
157162
$trashManager,
158163
$userManager,
159164
$logger,
160165
$eventDispatcher,
161166
$rootFolder,
167+
$request,
162168
);
163169
},
164170
1);

0 commit comments

Comments
 (0)