quota.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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 management
  24. */
  25. class OC_FileProxy_Quota extends OC_FileProxy{
  26. static $rootView;
  27. private $userQuota=array();
  28. /**
  29. * get the quota for the user
  30. * @param user
  31. * @return int
  32. */
  33. private function getQuota($user) {
  34. if(in_array($user, $this->userQuota)) {
  35. return $this->userQuota[$user];
  36. }
  37. $userQuota=OC_Preferences::getValue($user, 'files', 'quota', 'default');
  38. if($userQuota=='default') {
  39. $userQuota=OC_AppConfig::getValue('files', 'default_quota', 'none');
  40. }
  41. if($userQuota=='none') {
  42. $this->userQuota[$user]=-1;
  43. }else{
  44. $this->userQuota[$user]=OC_Helper::computerFileSize($userQuota);
  45. }
  46. return $this->userQuota[$user];
  47. }
  48. /**
  49. * get the free space in the path's owner home folder
  50. * @param path
  51. * @return int
  52. */
  53. private function getFreeSpace($path) {
  54. /**
  55. * @var \OC\Files\Storage\Storage $storage
  56. * @var string $internalPath
  57. */
  58. list($storage, $internalPath) = \OC\Files\Filesystem::resolvePath($path);
  59. $owner = $storage->getOwner($internalPath);
  60. if (!$owner) {
  61. return -1;
  62. }
  63. $totalSpace = $this->getQuota($owner);
  64. if($totalSpace == -1) {
  65. return -1;
  66. }
  67. $view = new \OC\Files\View("/".$owner."/files");
  68. $rootInfo = $view->getFileInfo('/');
  69. $usedSpace = isset($rootInfo['size'])?$rootInfo['size']:0;
  70. return $totalSpace - $usedSpace;
  71. }
  72. public function postFree_space($path, $space) {
  73. $free=$this->getFreeSpace($path);
  74. if($free==-1) {
  75. return $space;
  76. }
  77. if ($space < 0){
  78. return $free;
  79. }
  80. return min($free, $space);
  81. }
  82. public function preFile_put_contents($path, $data) {
  83. if (is_resource($data)) {
  84. $data = '';//TODO: find a way to get the length of the stream without emptying it
  85. }
  86. return (strlen($data)<$this->getFreeSpace($path) or $this->getFreeSpace($path)==-1);
  87. }
  88. public function preCopy($path1, $path2) {
  89. if(!self::$rootView) {
  90. self::$rootView = new \OC\Files\View('');
  91. }
  92. return (self::$rootView->filesize($path1)<$this->getFreeSpace($path2) or $this->getFreeSpace($path2)==-1);
  93. }
  94. public function preFromTmpFile($tmpfile, $path) {
  95. return (filesize($tmpfile)<$this->getFreeSpace($path) or $this->getFreeSpace($path)==-1);
  96. }
  97. public function preFromUploadedFile($tmpfile, $path) {
  98. return (filesize($tmpfile)<$this->getFreeSpace($path) or $this->getFreeSpace($path)==-1);
  99. }
  100. }