fix(files_trashbin): check if there is enough space before restoring

Signed-off-by: Kent Delante <kent.delante@proton.me>
This commit is contained in:
Kent Delante 2025-05-12 15:32:19 +08:00
parent cc22d74887
commit 705aee5aa0
5 changed files with 122 additions and 3 deletions

View file

@ -8,9 +8,12 @@ declare(strict_types=1);
*/
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;
@ -32,6 +35,7 @@ class TrashbinPlugin extends ServerPlugin {
public function __construct(
private IPreview $previewManager,
private View $view,
) {
}
@ -40,6 +44,7 @@ class TrashbinPlugin extends ServerPlugin {
$this->server->on('propFind', [$this, 'propFind']);
$this->server->on('afterMethod:GET', [$this,'httpGet']);
$this->server->on('beforeMove', [$this, 'beforeMove']);
}
@ -129,4 +134,47 @@ class TrashbinPlugin extends ServerPlugin {
$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;
}
}

View file

@ -3,6 +3,7 @@
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
import { getCurrentUser } from '@nextcloud/auth'
import { showError } from '@nextcloud/dialogs'
import { emit } from '@nextcloud/event-bus'
import { Permission, Node, View, FileAction } from '@nextcloud/files'
import { t } from '@nextcloud/l10n'
@ -52,6 +53,9 @@ export const restoreAction = 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('Failed to restore node', { error, node })
return false
}

View file

@ -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 ]
];
}
}

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long