Skip to content

Commit 5250066

Browse files
committed
fix(dav): Give proper HTTP status code on MKCOL when quota exceeded
Signed-off-by: Marcel Klehr <[email protected]>
1 parent 196b2f1 commit 5250066

File tree

1 file changed

+29
-1
lines changed

1 file changed

+29
-1
lines changed

apps/dav/lib/Connector/Sabre/QuotaPlugin.php

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
use Sabre\DAV\Exception\InsufficientStorage;
1616
use Sabre\DAV\Exception\ServiceUnavailable;
1717
use Sabre\DAV\INode;
18+
use Sabre\HTTP\RequestInterface;
19+
use Sabre\HTTP\ResponseInterface;
1820

1921
/**
2022
* This plugin check user quota and deny creating files when they exceeds the quota.
@@ -55,6 +57,7 @@ public function initialize(\Sabre\DAV\Server $server) {
5557

5658
$server->on('beforeWriteContent', [$this, 'beforeWriteContent'], 10);
5759
$server->on('beforeCreateFile', [$this, 'beforeCreateFile'], 10);
60+
$server->on('method:MKCOL', [$this, 'onCreateCollection'], 30);
5861
$server->on('beforeMove', [$this, 'beforeMove'], 10);
5962
$server->on('beforeCopy', [$this, 'beforeCopy'], 10);
6063
}
@@ -88,6 +91,27 @@ public function beforeCreateFile($uri, $data, INode $parent, $modified) {
8891
return $this->checkQuota($parent->getPath() . '/' . basename($uri));
8992
}
9093

94+
/**
95+
* Check quota before creating directory
96+
*
97+
* @param RequestInterface $request
98+
* @param ResponseInterface $response
99+
* @return bool
100+
* @throws InsufficientStorage
101+
* @throws \Sabre\DAV\Exception\Forbidden
102+
*/
103+
public function onCreateCollection(RequestInterface $request, ResponseInterface $response): bool {
104+
$destinationPath = $this->server->calculateUri($request->getUrl());
105+
$quotaPath = $this->getPathForDestination($destinationPath);
106+
if ($quotaPath) {
107+
// MKCOL does not have a Content-Length header, so we can use
108+
// a fixed value for the quota check.
109+
return $this->checkQuota($quotaPath, 4096);
110+
}
111+
112+
return true;
113+
}
114+
91115
/**
92116
* Check quota before writing content
93117
*
@@ -174,7 +198,7 @@ private function getPathForDestination(string $destinationPath): string {
174198
* @throws InsufficientStorage
175199
* @return bool
176200
*/
177-
public function checkQuota(string $path, $length = null) {
201+
public function checkQuota(string $path, $length = null, $isDir = false) {
178202
if ($length === null) {
179203
$length = $this->getLength();
180204
}
@@ -191,6 +215,10 @@ public function checkQuota(string $path, $length = null) {
191215

192216
$freeSpace = $this->getFreeSpace($path);
193217
if ($freeSpace >= 0 && $length > $freeSpace) {
218+
if ($isDir) {
219+
throw new InsufficientStorage("Insufficient space in $path. $freeSpace available. Cannot create directory");
220+
}
221+
194222
throw new InsufficientStorage("Insufficient space in $path, $length required, $freeSpace available");
195223
}
196224
}

0 commit comments

Comments
 (0)