1515use Sabre \DAV \Exception \InsufficientStorage ;
1616use Sabre \DAV \Exception \ServiceUnavailable ;
1717use 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,31 @@ 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+ try {
105+ $ destinationPath = $ this ->server ->calculateUri ($ request ->getUrl ());
106+ $ quotaPath = $ this ->getPathForDestination ($ destinationPath );
107+ } catch (\Exception $ e ) {
108+ return true ;
109+ }
110+ if ($ quotaPath ) {
111+ // MKCOL does not have a Content-Length header, so we can use
112+ // a fixed value for the quota check.
113+ return $ this ->checkQuota ($ quotaPath , 4096 , true );
114+ }
115+
116+ return true ;
117+ }
118+
91119 /**
92120 * Check quota before writing content
93121 *
@@ -174,7 +202,7 @@ private function getPathForDestination(string $destinationPath): string {
174202 * @throws InsufficientStorage
175203 * @return bool
176204 */
177- public function checkQuota (string $ path , $ length = null ) {
205+ public function checkQuota (string $ path , $ length = null , $ isDir = false ) {
178206 if ($ length === null ) {
179207 $ length = $ this ->getLength ();
180208 }
@@ -191,6 +219,10 @@ public function checkQuota(string $path, $length = null) {
191219
192220 $ freeSpace = $ this ->getFreeSpace ($ path );
193221 if ($ freeSpace >= 0 && $ length > $ freeSpace ) {
222+ if ($ isDir ) {
223+ throw new InsufficientStorage ("Insufficient space in $ path. $ freeSpace available. Cannot create directory " );
224+ }
225+
194226 throw new InsufficientStorage ("Insufficient space in $ path, $ length required, $ freeSpace available " );
195227 }
196228 }
0 commit comments