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

Signed-off-by: Kent Delante <kent.delante@proton.me>

[skip ci]
pull/53129/head
Kent Delante 2025-05-12 15:32:19 +07:00 committed by backportbot[bot]
parent 86bf0eb4ad
commit 142bf44127
2 changed files with 114 additions and 0 deletions

@ -8,8 +8,11 @@ declare(strict_types=1);
*/
namespace OCA\Files_Trashbin\Sabre;
use OC\Files\FileInfo;
use OC\Files\View;
use OCA\DAV\Connector\Sabre\FilesPlugin;
use OCP\IPreview;
use Psr\Log\LoggerInterface;
use Sabre\DAV\INode;
use Sabre\DAV\PropFind;
use Sabre\DAV\Server;
@ -42,6 +45,7 @@ class TrashbinPlugin extends ServerPlugin {
$this->server->on('propFind', [$this, 'propFind']);
$this->server->on('afterMethod:GET', [$this,'httpGet']);
$this->server->on('beforeMove', [$this, 'beforeMove']);
}
@ -123,4 +127,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;
}
}

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