]> source.dussan.org Git - nextcloud-server.git/commitdiff
Fix quota calculation on new dav upload endpoint 6527/head
authorRoeland Jago Douma <roeland@famdouma.nl>
Wed, 13 Sep 2017 11:12:16 +0000 (13:12 +0200)
committerRoeland Jago Douma <roeland@famdouma.nl>
Fri, 15 Sep 2017 18:21:37 +0000 (20:21 +0200)
Signed-off-by: Roeland Jago Douma <roeland@famdouma.nl>
apps/dav/lib/Connector/Sabre/QuotaPlugin.php
apps/dav/lib/Connector/Sabre/ServerFactory.php
apps/dav/lib/Server.php
apps/dav/tests/unit/Connector/Sabre/QuotaPluginTest.php

index f0958c353a1ac6d06af2a6f12f89438ccade1c20..a41a4678f30ddae4b672e2c2455361f3de91cb9e 100644 (file)
  *
  */
 namespace OCA\DAV\Connector\Sabre;
+use OCA\DAV\Files\FilesHome;
+use OCA\DAV\Upload\FutureFile;
+use OCA\DAV\Upload\UploadFolder;
 use OCP\Files\FileInfo;
 use OCP\Files\StorageNotAvailableException;
 use Sabre\DAV\Exception\InsufficientStorage;
 use Sabre\DAV\Exception\ServiceUnavailable;
+use Sabre\DAV\INode;
 use Sabre\HTTP\URLUtil;
 
 /**
@@ -40,9 +44,7 @@ use Sabre\HTTP\URLUtil;
  */
 class QuotaPlugin extends \Sabre\DAV\ServerPlugin {
 
-       /**
-        * @var \OC\Files\View
-        */
+       /** @var \OC\Files\View */
        private $view;
 
        /**
@@ -74,26 +76,86 @@ class QuotaPlugin extends \Sabre\DAV\ServerPlugin {
 
                $this->server = $server;
 
-               $server->on('beforeWriteContent', array($this, 'checkQuota'), 10);
-               $server->on('beforeCreateFile', array($this, 'checkQuota'), 10);
+               $server->on('beforeWriteContent', [$this, 'beforeWriteContent'], 10);
+               $server->on('beforeCreateFile', [$this, 'beforeCreateFile'], 10);
+               $server->on('beforeMove', [$this, 'beforeMove'], 10);
+       }
+
+       /**
+        * Check quota before creating file
+        *
+        * @param string $uri target file URI
+        * @param resource $data data
+        * @param INode $parent Sabre Node
+        * @param bool $modified modified
+        */
+       public function beforeCreateFile($uri, $data, INode $parent, $modified) {
+               if (!$parent instanceof Node) {
+                       return;
+               }
+
+               return $this->checkQuota($parent->getPath() . '/' . basename($uri));
+       }
+
+       /**
+        * Check quota before writing content
+        *
+        * @param string $uri target file URI
+        * @param INode $node Sabre Node
+        * @param resource $data data
+        * @param bool $modified modified
+        */
+       public function beforeWriteContent($uri, INode $node, $data, $modified) {
+               if (!$node instanceof Node) {
+                       return;
+               }
+
+               return $this->checkQuota($node->getPath());
        }
 
+       /**
+        * Check if we're moving a Futurefile in which case we need to check
+        * the quota on the target destination.
+        *
+        * @param string $source source path
+        * @param string $destination destination path
+        */
+       public function beforeMove($source, $destination) {
+               $sourceNode = $this->server->tree->getNodeForPath($source);
+               if (!$sourceNode instanceof FutureFile) {
+                       return;
+               }
+
+               // get target node for proper path conversion
+               if ($this->server->tree->nodeExists($destination)) {
+                       $destinationNode = $this->server->tree->getNodeForPath($destination);
+                       $path = $destinationNode->getPath();
+               } else {
+                       $parentNode = $this->server->tree->getNodeForPath(dirname($destination));
+                       $path = $parentNode->getPath();
+               }
+
+               return $this->checkQuota($path, $sourceNode->getSize());
+       }
+
+
        /**
         * This method is called before any HTTP method and validates there is enough free space to store the file
         *
-        * @param string $uri
+        * @param string $path relative to the users home
+        * @param int $length
         * @throws InsufficientStorage
         * @return bool
         */
-       public function checkQuota($uri) {
-               $length = $this->getLength();
+       public function checkQuota($path, $length = null) {
+               if ($length === null) {
+                       $length = $this->getLength();
+               }
+
                if ($length) {
-                       if (substr($uri, 0, 1) !== '/') {
-                               $uri = '/' . $uri;
-                       }
-                       list($parentUri, $newName) = URLUtil::splitPath($uri);
-                       if(is_null($parentUri)) {
-                               $parentUri = '';
+                       list($parentPath, $newName) = \Sabre\Uri\split($path);
+                       if(is_null($parentPath)) {
+                               $parentPath = '';
                        }
                        $req = $this->server->httpRequest;
                        if ($req->getHeader('OC-Chunked')) {
@@ -103,9 +165,9 @@ class QuotaPlugin extends \Sabre\DAV\ServerPlugin {
                                // there is still enough space for the remaining chunks
                                $length -= $chunkHandler->getCurrentSize();
                                // use target file name for free space check in case of shared files
-                               $uri = rtrim($parentUri, '/') . '/' . $info['name'];
+                               $path = rtrim($parentPath, '/') . '/' . $info['name'];
                        }
-                       $freeSpace = $this->getFreeSpace($uri);
+                       $freeSpace = $this->getFreeSpace($path);
                        if ($freeSpace !== FileInfo::SPACE_UNKNOWN && $freeSpace !== FileInfo::SPACE_UNLIMITED && $length > $freeSpace) {
                                if (isset($chunkHandler)) {
                                        $chunkHandler->cleanup();
index 329aa335ea4951749573a5da27d5d021f64a7126..c120b0be2e20d6609ae02c4d511d6d99da6d0fe8 100644 (file)
@@ -161,7 +161,7 @@ class ServerFactory {
                                        !$this->config->getSystemValue('debug', false)
                                )
                        );
-                       $server->addPlugin(new \OCA\DAV\Connector\Sabre\QuotaPlugin($view));
+                       $server->addPlugin(new \OCA\DAV\Connector\Sabre\QuotaPlugin($view, true));
 
                        if($this->userSession->isLoggedIn()) {
                                $server->addPlugin(new \OCA\DAV\Connector\Sabre\TagsPlugin($objectTree, $this->tagManager));
index 30c0a4afbd7bcec413f0f0c46afdd73548c7a82e..02a306dacc2024935005cb7eb00fa0422fc921cd 100644 (file)
@@ -211,7 +211,7 @@ class Server {
                                );
                                if ($view !== null) {
                                        $this->server->addPlugin(
-                                               new QuotaPlugin($view));
+                                               new QuotaPlugin($view, false));
                                }
                                $this->server->addPlugin(
                                        new TagsPlugin(
index 6286362f47dc101ed92cb525f175e3707ba5bb25..d29080539e60cc364173a1bf7ec138d6e2a5165d 100644 (file)
  *
  */
 namespace OCA\DAV\Tests\unit\Connector\Sabre;
+use OC\Files\View;
+use OCA\DAV\Connector\Sabre\Directory;
+use OCA\DAV\Connector\Sabre\QuotaPlugin;
+use OCA\DAV\Files\FilesHome;
 use OCP\Files\FileInfo;
+use Sabre\DAV\Exception\InsufficientStorage;
+use Sabre\DAV\Tree;
 use Test\TestCase;
 
 /**
@@ -44,7 +50,7 @@ class QuotaPluginTest extends TestCase {
        private function init($quota, $checkedPath = '') {
                $view = $this->buildFileViewMock($quota, $checkedPath);
                $this->server = new \Sabre\DAV\Server();
-               $this->plugin = $this->getMockBuilder('\OCA\DAV\Connector\Sabre\QuotaPlugin')
+               $this->plugin = $this->getMockBuilder(QuotaPlugin::class)
                        ->setConstructorArgs([$view])
                        ->setMethods(['getFileChunking'])
                        ->getMock();
@@ -224,7 +230,7 @@ class QuotaPluginTest extends TestCase {
 
        private function buildFileViewMock($quota, $checkedPath) {
                // mock filesysten
-               $view = $this->getMockBuilder('\OC\Files\View')
+               $view = $this->getMockBuilder(View::class)
                        ->setMethods(['free_space'])
                        ->disableOriginalConstructor()
                        ->getMock();
@@ -235,5 +241,4 @@ class QuotaPluginTest extends TestCase {
 
                return $view;
        }
-
 }