]> source.dussan.org Git - nextcloud-server.git/commitdiff
calculate trashbin size per user
authorBjörn Schießle <schiessle@owncloud.com>
Thu, 21 Feb 2013 11:37:13 +0000 (12:37 +0100)
committerBjörn Schießle <schiessle@owncloud.com>
Thu, 21 Feb 2013 11:37:13 +0000 (12:37 +0100)
apps/files_trashbin/appinfo/database.xml
apps/files_trashbin/appinfo/version
apps/files_trashbin/lib/trash.php

index 1144a1c9a974a1fd45af02620b5898062d929ccb..6f2a27866fff4a3d7fa2f785462d9694d81f4da5 100644 (file)
 
  </table>
 
+ <table>
+
+  <name>*dbprefix*files_trashsize</name>
+
+  <declaration>
+
+   <field>
+    <name>user</name>
+    <type>text</type>
+    <default></default>
+    <notnull>true</notnull>
+    <length>50</length>
+   </field>
+
+   <field>
+    <name>size</name>
+    <type>text</type>
+    <default></default>
+    <notnull>true</notnull>
+    <length>50</length>
+   </field>
+     
+  </declaration>
+
+ </table>
+
 </database>
index 49d59571fbf6e077eece30f8c418b6aad15e20b0..be586341736ee60d6ca2be0f3762a307e8fe79f9 100644 (file)
@@ -1 +1 @@
-0.1
+0.3
index 8d54a471b422ab719fa317a59cef6365d6e9bd68..898f8972737908714b748540baacb191f2733c34 100644 (file)
@@ -52,8 +52,9 @@ class Trashbin {
                } else {\r
                        $type = 'file';\r
                }
-               \r
-               if (  ($trashbinSize = \OCP\Config::getAppValue('files_trashbin', 'size')) === null ) {\r
+               
+               $trashbinSize = self::getTrashbinSize($user);\r
+               if ( $trashbinSize === false || $trashbinSize < 0 ) {\r
                        $trashbinSize = self::calculateSize(new \OC_FilesystemView('/'. $user.'/files_trashbin'));
                        $trashbinSize += self::calculateSize(new \OC_FilesystemView('/'. $user.'/versions_trashbin'));\r
                }
@@ -89,7 +90,7 @@ class Trashbin {
                        $quota = \OCP\Util::computerFileSize(\OC_Appconfig::getValue('files', 'default_quota'));\r
                }\r
                if ( $quota == null ) {\r
-                       $quota = \OC\Files\Filesystem::free_space('/');\r
+                       $quota = \OC\Files\Filesystem::free_space('/') / count(\OCP\User::getUsers());\r
                }
                
                // calculate available space for trash bin\r
@@ -102,7 +103,8 @@ class Trashbin {
                }
                
                $trashbinSize -= self::expire($availableSpace);
-               \OCP\Config::setAppValue('files_trashbin', 'size', $trashbinSize);\r
+               
+               self::setTrashbinSize($user, $trashbinSize);\r
        }
        
        
@@ -116,7 +118,8 @@ class Trashbin {
                $user = \OCP\User::getUser();
                $view = new \OC_FilesystemView('/'.$user);
                
-               if (  ($trashbinSize = \OCP\Config::getAppValue('files_trashbin', 'size')) === null ) {\r
+               $trashbinSize = self::getTrashbinSize($user);
+               if ( $trashbinSize === false || $trashbinSize < 0 ) {\r
                        $trashbinSize = self::calculateSize(new \OC_FilesystemView('/'. $user.'/files_trashbin'));\r
                        $trashbinSize += self::calculateSize(new \OC_FilesystemView('/'. $user.'/versions_trashbin'));\r
                }
@@ -185,7 +188,8 @@ class Trashbin {
                                $query->execute(array($user,$filename,$timestamp));
                        }
 
-                       \OCP\Config::setAppValue('files_trashbin', 'size', $trashbinSize);
+                       self::setTrashbinSize($user, $trashbinSize);
+                       
                        return true;
                } else {
                        \OC_Log::write('files_trashbin', 'Couldn\'t restore file from trash bin, '.$filename, \OC_log::ERROR);
@@ -205,7 +209,8 @@ class Trashbin {
                $view = new \OC_FilesystemView('/'.$user);
                $size = 0;\r
        
-               if (  ($trashbinSize = \OCP\Config::getAppValue('files_trashbin', 'size')) === null ) {\r
+               $trashbinSize = self::getTrashbinSize($user);
+               if ( $trashbinSize === false || $trashbinSize < 0 ) {\r
                        $trashbinSize = self::calculateSize(new \OC_FilesystemView('/'. $user.'/files_trashbin'));\r
                        $trashbinSize += self::calculateSize(new \OC_FilesystemView('/'. $user.'/versions_trashbin'));\r
                }
@@ -242,7 +247,7 @@ class Trashbin {
                }
                $view->unlink('/files_trashbin/'.$file);
                $trashbinSize -= $size;
-               \OCP\Config::setAppValue('files_trashbin', 'size', $trashbinSize);
+               self::setTrashbinSize($user, $trashbinSize);
                \r
                return $size;\r
        }
@@ -429,5 +434,36 @@ class Trashbin {
                }\r
                return $size;\r
        }
+
+       /**\r
+        * get current size of trash bin from a given user\r
+        *\r
+        * @param $user user who owns the trash bin\r
+        * @return mixed trash bin size or false if no trash bin size is stored\r
+        */\r
+       private static function getTrashbinSize($user) {\r
+               $query = \OC_DB::prepare('SELECT size FROM *PREFIX*files_trashsize WHERE user=?');\r
+               $result = $query->execute(array($user))->fetchAll();\r
+\r
+               if ($result) {\r
+                       return $result[0]['size'];\r
+               }\r
+               return false;\r
+       }\r
+       \r
+       /**\r
+        * write to the database how much space is in use for the trash bin\r
+        *\r
+        * @param $user owner of the trash bin\r
+        * @param $size size of the trash bin\r
+        */\r
+       private static function setTrashbinSize($user, $size) {\r
+               if ( self::getTrashbinSize($user) === false) {\r
+                       $query = \OC_DB::prepare('INSERT INTO *PREFIX*files_trashsize (size, user) VALUES (?, ?)');\r
+               }else {\r
+                       $query = \OC_DB::prepare('UPDATE *PREFIX*files_trashsize SET size=? WHERE user=?');\r
+               }\r
+               $query->execute(array($size, $user));\r
+       }
        
 }\r