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
55 changes: 54 additions & 1 deletion apps/files_trashbin/lib/Sabre/TrashbinPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,12 @@
*/
namespace OCA\Files_Trashbin\Sabre;

use OC\Files\FileInfo;
use OC\Files\View;
use OCA\DAV\Connector\Sabre\FilesPlugin;
use OCA\Files_Trashbin\Trash\ITrashItem;
use OCP\IPreview;
use Psr\Log\LoggerInterface;
use Sabre\DAV\INode;
use Sabre\DAV\PropFind;
use Sabre\DAV\Server;
Expand All @@ -31,17 +35,23 @@ class TrashbinPlugin extends ServerPlugin {
/** @var IPreview */
private $previewManager;

/** @var View */
private $view;

public function __construct(
IPreview $previewManager
IPreview $previewManager,
View $view
) {
$this->previewManager = $previewManager;
$this->view = $view;
}

public function initialize(Server $server) {
$this->server = $server;

$this->server->on('propFind', [$this, 'propFind']);
$this->server->on('afterMethod:GET', [$this,'httpGet']);
$this->server->on('beforeMove', [$this, 'beforeMove']);
}


Expand Down Expand Up @@ -123,4 +133,47 @@ public function httpGet(RequestInterface $request, ResponseInterface $response):
$response->addHeader('Content-Disposition', 'attachment; filename="' . $node->getFilename() . '"');
}
}

/**
* Check if a user has available space before attempting to
* restore from trashbin unless they have unlimited quota.
*
* @param string $sourcePath
* @param string $destinationPath
* @return bool
*/
public function beforeMove(string $sourcePath, string $destinationPath): bool {
try {
$node = $this->server->tree->getNodeForPath($sourcePath);
$destinationNodeParent = $this->server->tree->getNodeForPath(dirname($destinationPath));
} catch (\Sabre\DAV\Exception $e) {
\OCP\Server::get(LoggerInterface::class)
->error($e->getMessage(), ['app' => 'files_trashbin', 'exception' => $e]);
return true;
}

// Check if a file is being restored before proceeding
if (!$node instanceof ITrash || !$destinationNodeParent instanceof RestoreFolder) {
return true;
}

$fileInfo = $node->getFileInfo();
if (!$fileInfo instanceof ITrashItem) {
return true;
}
$restoreFolder = dirname($fileInfo->getOriginalLocation());
$freeSpace = $this->view->free_space($restoreFolder);
if ($freeSpace === FileInfo::SPACE_NOT_COMPUTED ||
$freeSpace === FileInfo::SPACE_UNKNOWN ||
$freeSpace === FileInfo::SPACE_UNLIMITED) {
return true;
}
$filesize = $fileInfo->getSize();
if ($freeSpace < $filesize) {
$this->server->httpResponse->setStatus(507);
return false;
}

return true;
}
}
4 changes: 4 additions & 0 deletions apps/files_trashbin/src/actions/restoreAction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
import { emit } from '@nextcloud/event-bus'
import { showError } from '@nextcloud/dialogs'
import { encodePath } from '@nextcloud/paths'
import { generateRemoteUrl } from '@nextcloud/router'
import { getCurrentUser } from '@nextcloud/auth'
Expand Down Expand Up @@ -48,6 +49,9 @@ registerFileAction(new FileAction({
emit('files:node:deleted', node)
return true
} catch (error) {
if (error.response?.status === 507) {
showError(t('files_trashbin', 'Not enough free space to restore the file/folder'))
}
logger.error(error)
return false
}
Expand Down
67 changes: 67 additions & 0 deletions apps/files_trashbin/tests/Sabre/TrashbinPluginTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php

/**
* SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

namespace OCA\Files_Trashbin\Tests\Sabre;

use OC\Files\FileInfo;
use OC\Files\View;
use OCA\Files_Trashbin\Sabre\ITrash;
use OCA\Files_Trashbin\Sabre\RestoreFolder;
use OCA\Files_Trashbin\Sabre\TrashbinPlugin;
use OCA\Files_Trashbin\Trash\ITrashItem;
use OCP\IPreview;
use Sabre\DAV\Server;
use Sabre\DAV\Tree;
use Test\TestCase;

class TrashbinPluginTest extends TestCase {
private Server $server;

protected function setUp(): void {
parent::setUp();

$tree = $this->createMock(Tree::class);
$this->server = new Server($tree);
}

/**
* @dataProvider quotaProvider
*/
public function testQuota(int $quota, int $fileSize, bool $expectedResult): void {
$fileInfo = $this->createMock(ITrashItem::class);
$fileInfo->method('getSize')->willReturn($fileSize);

$trashNode = $this->createMock(ITrash::class);
$trashNode->method('getFileInfo')->willReturn($fileInfo);

$restoreNode = $this->createMock(RestoreFolder::class);

$this->server->tree->method('getNodeForPath')->willReturn($trashNode, $restoreNode);

$previewManager = $this->createMock(IPreview::class);

$view = $this->createMock(View::class);
$view->method('free_space')->willReturn($quota);

$plugin = new TrashbinPlugin($previewManager, $view);
$plugin->initialize($this->server);

$sourcePath = 'trashbin/test/trash/file1';
$destinationPath = 'trashbin/test/restore/file1';
$this->assertEquals($expectedResult, $plugin->beforeMove($sourcePath, $destinationPath));
}

public function quotaProvider(): array {
return [
[ 1024, 512, true ],
[ 512, 513, false ],
[ FileInfo::SPACE_NOT_COMPUTED, 1024, true ],
[ FileInfo::SPACE_UNKNOWN, 1024, true ],
[ FileInfo::SPACE_UNLIMITED, 1024, true ]
];
}
}
4 changes: 2 additions & 2 deletions dist/6189-6189.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/6189-6189.js.map

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions dist/comments-comments-app.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/comments-comments-app.js.map

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions dist/comments-comments-tab.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/comments-comments-tab.js.map

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions dist/core-common.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/core-common.js.map

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions dist/core-legacy-unified-search.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/core-legacy-unified-search.js.map

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions dist/core-login.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/core-login.js.map

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions dist/core-main.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/core-main.js.map

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions dist/core-profile.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/core-profile.js.map

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions dist/dav-settings-personal-availability.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/dav-settings-personal-availability.js.map

Large diffs are not rendered by default.

Loading
Loading