Filemanager - searchNodes returns quota info
authorPaul Mehrer <p.mehrer@metaways.de>
Tue, 11 Jul 2017 08:14:31 +0000 (10:14 +0200)
committerPaul Mehrer <p.mehrer@metaways.de>
Wed, 12 Jul 2017 11:17:01 +0000 (13:17 +0200)
* not returned for recursive search
* quota of 0 means unlimited
* localQuota may be 0, still effective quota may overwrite it

Change-Id: I6e0f5bba8a10cbabffeecbe722a519688ed2650f
Reviewed-on: http://gerrit.tine20.com/customers/5090
Tested-by: Jenkins CI (http://ci.tine20.com/)
Reviewed-by: Paul Mehrer <p.mehrer@metaways.de>
Tested-by: Paul Mehrer <p.mehrer@metaways.de>
tine20/Filemanager/Controller/Node.php
tine20/Filemanager/Frontend/Json.php
tine20/Tinebase/FileSystem.php

index cc73b97..396676b 100644 (file)
@@ -291,8 +291,8 @@ class Filemanager_Controller_Node extends Tinebase_Controller_Record_Abstract
         
         if ($path->containerType === Tinebase_Model_Tree_Node_Path::TYPE_ROOT) {
             $result = $this->_getRootNodes();
-        } else if ($path->containerType === Tinebase_FileSystem::FOLDER_TYPE_PERSONAL && ! $path->containerOwner) {
-            if (! file_exists($path->statpath)) {
+        } elseif ($path->containerType === Tinebase_FileSystem::FOLDER_TYPE_PERSONAL && ! $path->containerOwner) {
+            if (! $this->_backend->fileExists($path->statpath)) {
                 $this->_backend->mkdir($path->statpath);
             }
             $result = $this->_getOtherUserNodes();
@@ -319,9 +319,17 @@ class Filemanager_Controller_Node extends Tinebase_Controller_Record_Abstract
                 }
             }
             $this->resolvePath($result, $path);
-            // TODO still needed?
-            //$this->_sortContainerNodes($result, $path, $_pagination);
         }
+
+        $parentNode = $this->_backend->stat($path->statpath);
+        $quota = $this->_backend->getEffectiveAndLocalQuota($parentNode);
+        $context = $this->getRequestContext();
+        if (!is_array($context)) {
+            $context = array();
+        }
+        $context['quotaResult'] = $quota;
+        $this->setRequestContext($quota);
+
         $this->resolveGrants($result);
         return $result;
     }
index 8eac480..5bcaebc 100644 (file)
@@ -36,8 +36,14 @@ class Filemanager_Frontend_Json extends Tinebase_Frontend_Json_Abstract
      */
     public function searchNodes($filter, $paging)
     {
-        $result = $this->_search($filter, $paging, Filemanager_Controller_Node::getInstance(), 'Filemanager_Model_NodeFilter');
+        $controller = Filemanager_Controller_Node::getInstance();
+        $result = $this->_search($filter, $paging, $controller, 'Filemanager_Model_NodeFilter');
         $this->_removeAppIdFromPathFilter($result);
+
+        $context = $controller->getRequestContext();
+        if (is_array($context) && isset($context['quotaResult'])) {
+            $result['quota'] = $context['quotaResult'];
+        }
         
         return $result;
     }
index aaebbe9..a25499c 100644 (file)
@@ -693,6 +693,101 @@ class Tinebase_FileSystem implements
         return $newFileObject;
     }
 
+    public function getEffectiveAndLocalQuota(Tinebase_Model_Tree_Node $node)
+    {
+        $quotaConfig = Tinebase_Config::getInstance()->{Tinebase_Config::QUOTA};
+        $total = $quotaConfig->{Tinebase_Config::QUOTA_TOTALINMB} * 1024 * 1024;
+        $effectiveQuota = $total;
+        $localQuota = null !== $node->quota ? (int)$node->quota : null;
+        if ($quotaConfig->{Tinebase_Config::QUOTA_INCLUDE_REVISION}) {
+            $localSize = $node->size;
+        } else {
+            $localSize = $node->revision_size;
+        }
+        $totalByUser = $quotaConfig->{Tinebase_Config::QUOTA_TOTALBYUSERINMB} * 1024 * 1024;
+        $personalNode = null;
+        if (Tinebase_Application::getInstance()->isInstalled('Filemanager')) {
+            $personalNode = $this->stat('/Filemanager/folders/personal');
+        }
+
+        /** @var Tinebase_Model_Application $tinebaseApplication */
+        $tinebaseApplication = Tinebase_Application::getInstance()->getApplicationByName('Tinebase');
+        $tinebaseState = $tinebaseApplication->xprops('state');
+
+        $totalUsage = 0;
+        if ($quotaConfig->{Tinebase_Config::QUOTA_INCLUDE_REVISION}) {
+            if (isset($tinebaseState[Tinebase_Model_Application::STATE_FILESYSTEM_ROOT_REVISION_SIZE])) {
+                $totalUsage = $tinebaseState[Tinebase_Model_Application::STATE_FILESYSTEM_ROOT_REVISION_SIZE];
+            }
+        } else {
+            if (isset($tinebaseState[Tinebase_Model_Application::STATE_FILESYSTEM_ROOT_SIZE])) {
+                $totalUsage = $tinebaseState[Tinebase_Model_Application::STATE_FILESYSTEM_ROOT_SIZE];
+            }
+        }
+        $effectiveUsage = $totalUsage;
+
+
+        $effectiveFree = $effectiveQuota > 0 ? $effectiveQuota - $effectiveUsage : null;
+
+        foreach ($this->_getTreeNodeBackend()->getAllFolderNodes(new Tinebase_Record_RecordSet(
+                'Tinebase_Model_Tree_Node', array($node))) as $parentNode) {
+            if ($quotaConfig->{Tinebase_Config::QUOTA_INCLUDE_REVISION}) {
+                $size = $parentNode->size;
+            } else {
+                $size = $parentNode->revision_size;
+            }
+
+            // folder quota
+            if (null !== $parentNode->quota && 0 !== (int)$parentNode->quota) {
+                if (null === $localQuota) {
+                    $localQuota = $parentNode->quota;
+                    $localSize = $size;
+                }
+                $free = $parentNode->quota - $size;
+                if (null === $effectiveFree || $free < $effectiveFree) {
+                    $effectiveFree = $free;
+                    $effectiveQuota = $parentNode->quota;
+                    $effectiveUsage = $size;
+                }
+            }
+
+            //personal quota
+            if (null !== $personalNode && $parentNode->parent_id === $personalNode->getId()) {
+                $user = Tinebase_User::getInstance()->getFullUserById($parentNode->name);
+                $quota = isset(
+                    $user->xprops('configuration')[Tinebase_Model_FullUser::CONFIGURATION_PERSONAL_QUOTA]) ?
+                    $user->xprops('configuration')[Tinebase_Model_FullUser::CONFIGURATION_PERSONAL_QUOTA] :
+                    $totalByUser;
+                if ($quota > 0) {
+                    if (null === $localQuota) {
+                        $localQuota = $quota;
+                        $localSize = $size;
+                    }
+                    $free = $quota - $size;
+                    if (null === $effectiveFree || $free < $effectiveFree) {
+                        $effectiveFree = $free;
+                        $effectiveQuota = $quota;
+                        $effectiveUsage = $size;
+                    }
+                }
+            }
+        }
+
+        if (null === $localQuota) {
+            $localQuota = $total;
+            $localSize = $totalUsage;
+        }
+
+        return array(
+            'localQuota'        => $localQuota,
+            'localUsage'        => $localSize,
+            'localFree'         => $localQuota > $localSize ? $localQuota - $localSize : 0,
+            'effectiveQuota'    => $effectiveQuota,
+            'effectiveUsage'    => $effectiveUsage,
+            'effectiveFree'     => $effectiveFree,
+        );
+    }
+
     /**
      * @param Tinebase_Record_RecordSet $_nodes
      * @param int $_sizeDiff