quota.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <?php
  2. /**
  3. * ownCloud
  4. *
  5. * @author Robin Appelman
  6. * @copyright 2011 Robin Appelman icewind1991@gmail.com
  7. *
  8. * This library is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
  10. * License as published by the Free Software Foundation; either
  11. * version 3 of the License, or any later version.
  12. *
  13. * This library is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public
  19. * License along with this library. If not, see <http://www.gnu.org/licenses/>.
  20. *
  21. */
  22. /**
  23. * user quota managment
  24. */
  25. class OC_FileProxy_Quota extends OC_FileProxy{
  26. private $userQuota=-1;
  27. /**
  28. * get the quota for the current user
  29. * @return int
  30. */
  31. private function getQuota(){
  32. if($this->userQuota!=-1){
  33. return $this->userQuota;
  34. }
  35. $userQuota=OC_Preferences::getValue(OC_User::getUser(),'files','quota','default');
  36. if($userQuota=='default'){
  37. $userQuota=OC_AppConfig::getValue('files','default_quota','none');
  38. }
  39. if($userQuota=='none'){
  40. $this->userQuota=0;
  41. }else{
  42. $this->userQuota=OC_Helper::computerFileSize($userQuota);
  43. }
  44. return $this->userQuota;
  45. }
  46. /**
  47. * get the free space in the users home folder
  48. * @return int
  49. */
  50. private function getFreeSpace(){
  51. $rootInfo=OC_FileCache_Cached::get('');
  52. // TODO Remove after merge of share_api
  53. if (OC_FileCache::inCache('/Shared')) {
  54. $sharedInfo=OC_FileCache_Cached::get('/Shared');
  55. } else {
  56. $sharedInfo = null;
  57. }
  58. $usedSpace=isset($rootInfo['size'])?$rootInfo['size']:0;
  59. $usedSpace=isset($sharedInfo['size'])?$usedSpace-$sharedInfo['size']:$usedSpace;
  60. $totalSpace=$this->getQuota();
  61. if($totalSpace==0){
  62. return 0;
  63. }
  64. return $totalSpace-$usedSpace;
  65. }
  66. public function postFree_space($path,$space){
  67. $free=$this->getFreeSpace();
  68. if($free==0){
  69. return $space;
  70. }
  71. return min($free,$space);
  72. }
  73. public function preFile_put_contents($path,$data){
  74. if (is_resource($data)) {
  75. $data = '';//TODO: find a way to get the length of the stream without emptying it
  76. }
  77. return (strlen($data)<$this->getFreeSpace() or $this->getFreeSpace()==0);
  78. }
  79. public function preCopy($path1,$path2){
  80. return (OC_Filesystem::filesize($path1)<$this->getFreeSpace() or $this->getFreeSpace()==0);
  81. }
  82. public function preFromTmpFile($tmpfile,$path){
  83. return (filesize($tmpfile)<$this->getFreeSpace() or $this->getFreeSpace()==0);
  84. }
  85. public function preFromUploadedFile($tmpfile,$path){
  86. return (filesize($tmpfile)<$this->getFreeSpace() or $this->getFreeSpace()==0);
  87. }
  88. }