@@ -35,199 +35,199 @@ |
||
| 35 | 35 | |
| 36 | 36 | class Quota extends Wrapper { |
| 37 | 37 | |
| 38 | - /** |
|
| 39 | - * @var int $quota |
|
| 40 | - */ |
|
| 41 | - protected $quota; |
|
| 42 | - |
|
| 43 | - /** |
|
| 44 | - * @var string $sizeRoot |
|
| 45 | - */ |
|
| 46 | - protected $sizeRoot; |
|
| 47 | - |
|
| 48 | - private $config; |
|
| 49 | - |
|
| 50 | - /** |
|
| 51 | - * @param array $parameters |
|
| 52 | - */ |
|
| 53 | - public function __construct($parameters) { |
|
| 54 | - parent::__construct($parameters); |
|
| 55 | - $this->quota = $parameters['quota']; |
|
| 56 | - $this->sizeRoot = isset($parameters['root']) ? $parameters['root'] : ''; |
|
| 57 | - $this->config = \OC::$server->getSystemConfig(); |
|
| 58 | - } |
|
| 59 | - |
|
| 60 | - /** |
|
| 61 | - * @return int quota value |
|
| 62 | - */ |
|
| 63 | - public function getQuota() { |
|
| 64 | - return $this->quota; |
|
| 65 | - } |
|
| 66 | - |
|
| 67 | - /** |
|
| 68 | - * @param string $path |
|
| 69 | - * @param \OC\Files\Storage\Storage $storage |
|
| 70 | - */ |
|
| 71 | - protected function getSize($path, $storage = null) { |
|
| 72 | - if ($this->config->getValue('quota_include_external_storage', false)) { |
|
| 73 | - $rootInfo = Filesystem::getFileInfo('', 'ext'); |
|
| 74 | - return $rootInfo->getSize(true); |
|
| 75 | - } else { |
|
| 76 | - if (is_null($storage)) { |
|
| 77 | - $cache = $this->getCache(); |
|
| 78 | - } else { |
|
| 79 | - $cache = $storage->getCache(); |
|
| 80 | - } |
|
| 81 | - $data = $cache->get($path); |
|
| 82 | - if ($data instanceof ICacheEntry and isset($data['size'])) { |
|
| 83 | - return $data['size']; |
|
| 84 | - } else { |
|
| 85 | - return \OCP\Files\FileInfo::SPACE_NOT_COMPUTED; |
|
| 86 | - } |
|
| 87 | - } |
|
| 88 | - } |
|
| 89 | - |
|
| 90 | - /** |
|
| 91 | - * Get free space as limited by the quota |
|
| 92 | - * |
|
| 93 | - * @param string $path |
|
| 94 | - * @return int |
|
| 95 | - */ |
|
| 96 | - public function free_space($path) { |
|
| 97 | - if ($this->quota < 0 || strpos($path, 'cache') === 0 || strpos($path, 'uploads') === 0) { |
|
| 98 | - return $this->storage->free_space($path); |
|
| 99 | - } else { |
|
| 100 | - $used = $this->getSize($this->sizeRoot); |
|
| 101 | - if ($used < 0) { |
|
| 102 | - return \OCP\Files\FileInfo::SPACE_NOT_COMPUTED; |
|
| 103 | - } else { |
|
| 104 | - $free = $this->storage->free_space($path); |
|
| 105 | - $quotaFree = max($this->quota - $used, 0); |
|
| 106 | - // if free space is known |
|
| 107 | - if ($free >= 0) { |
|
| 108 | - $free = min($free, $quotaFree); |
|
| 109 | - } else { |
|
| 110 | - $free = $quotaFree; |
|
| 111 | - } |
|
| 112 | - return $free; |
|
| 113 | - } |
|
| 114 | - } |
|
| 115 | - } |
|
| 116 | - |
|
| 117 | - /** |
|
| 118 | - * see http://php.net/manual/en/function.file_put_contents.php |
|
| 119 | - * |
|
| 120 | - * @param string $path |
|
| 121 | - * @param string $data |
|
| 122 | - * @return bool |
|
| 123 | - */ |
|
| 124 | - public function file_put_contents($path, $data) { |
|
| 125 | - $free = $this->free_space($path); |
|
| 126 | - if ($free < 0 or strlen($data) < $free) { |
|
| 127 | - return $this->storage->file_put_contents($path, $data); |
|
| 128 | - } else { |
|
| 129 | - return false; |
|
| 130 | - } |
|
| 131 | - } |
|
| 132 | - |
|
| 133 | - /** |
|
| 134 | - * see http://php.net/manual/en/function.copy.php |
|
| 135 | - * |
|
| 136 | - * @param string $source |
|
| 137 | - * @param string $target |
|
| 138 | - * @return bool |
|
| 139 | - */ |
|
| 140 | - public function copy($source, $target) { |
|
| 141 | - $free = $this->free_space($target); |
|
| 142 | - if ($free < 0 or $this->getSize($source) < $free) { |
|
| 143 | - return $this->storage->copy($source, $target); |
|
| 144 | - } else { |
|
| 145 | - return false; |
|
| 146 | - } |
|
| 147 | - } |
|
| 148 | - |
|
| 149 | - /** |
|
| 150 | - * see http://php.net/manual/en/function.fopen.php |
|
| 151 | - * |
|
| 152 | - * @param string $path |
|
| 153 | - * @param string $mode |
|
| 154 | - * @return resource |
|
| 155 | - */ |
|
| 156 | - public function fopen($path, $mode) { |
|
| 157 | - $source = $this->storage->fopen($path, $mode); |
|
| 158 | - |
|
| 159 | - // don't apply quota for part files |
|
| 160 | - if (!$this->isPartFile($path)) { |
|
| 161 | - $free = $this->free_space($path); |
|
| 162 | - if ($source && $free >= 0 && $mode !== 'r' && $mode !== 'rb') { |
|
| 163 | - // only apply quota for files, not metadata, trash or others |
|
| 164 | - if (strpos(ltrim($path, '/'), 'files/') === 0) { |
|
| 165 | - return \OC\Files\Stream\Quota::wrap($source, $free); |
|
| 166 | - } |
|
| 167 | - } |
|
| 168 | - } |
|
| 169 | - return $source; |
|
| 170 | - } |
|
| 171 | - |
|
| 172 | - /** |
|
| 173 | - * Checks whether the given path is a part file |
|
| 174 | - * |
|
| 175 | - * @param string $path Path that may identify a .part file |
|
| 176 | - * @return string File path without .part extension |
|
| 177 | - * @note this is needed for reusing keys |
|
| 178 | - */ |
|
| 179 | - private function isPartFile($path) { |
|
| 180 | - $extension = pathinfo($path, PATHINFO_EXTENSION); |
|
| 181 | - |
|
| 182 | - return ($extension === 'part'); |
|
| 183 | - } |
|
| 184 | - |
|
| 185 | - /** |
|
| 186 | - * @param IStorage $sourceStorage |
|
| 187 | - * @param string $sourceInternalPath |
|
| 188 | - * @param string $targetInternalPath |
|
| 189 | - * @return bool |
|
| 190 | - */ |
|
| 191 | - public function copyFromStorage(IStorage $sourceStorage, $sourceInternalPath, $targetInternalPath) { |
|
| 192 | - $free = $this->free_space($targetInternalPath); |
|
| 193 | - if ($free < 0 or $this->getSize($sourceInternalPath, $sourceStorage) < $free) { |
|
| 194 | - return $this->storage->copyFromStorage($sourceStorage, $sourceInternalPath, $targetInternalPath); |
|
| 195 | - } else { |
|
| 196 | - return false; |
|
| 197 | - } |
|
| 198 | - } |
|
| 199 | - |
|
| 200 | - /** |
|
| 201 | - * @param IStorage $sourceStorage |
|
| 202 | - * @param string $sourceInternalPath |
|
| 203 | - * @param string $targetInternalPath |
|
| 204 | - * @return bool |
|
| 205 | - */ |
|
| 206 | - public function moveFromStorage(IStorage $sourceStorage, $sourceInternalPath, $targetInternalPath) { |
|
| 207 | - $free = $this->free_space($targetInternalPath); |
|
| 208 | - if ($free < 0 or $this->getSize($sourceInternalPath, $sourceStorage) < $free) { |
|
| 209 | - return $this->storage->moveFromStorage($sourceStorage, $sourceInternalPath, $targetInternalPath); |
|
| 210 | - } else { |
|
| 211 | - return false; |
|
| 212 | - } |
|
| 213 | - } |
|
| 214 | - |
|
| 215 | - public function mkdir($path) { |
|
| 216 | - $free = $this->free_space($path); |
|
| 217 | - if ($free === 0.0) { |
|
| 218 | - return false; |
|
| 219 | - } |
|
| 220 | - |
|
| 221 | - return parent::mkdir($path); |
|
| 222 | - } |
|
| 223 | - |
|
| 224 | - public function touch($path, $mtime = null) { |
|
| 225 | - $free = $this->free_space($path); |
|
| 226 | - if ($free === 0.0) { |
|
| 227 | - return false; |
|
| 228 | - } |
|
| 229 | - |
|
| 230 | - return parent::touch($path, $mtime); |
|
| 231 | - } |
|
| 38 | + /** |
|
| 39 | + * @var int $quota |
|
| 40 | + */ |
|
| 41 | + protected $quota; |
|
| 42 | + |
|
| 43 | + /** |
|
| 44 | + * @var string $sizeRoot |
|
| 45 | + */ |
|
| 46 | + protected $sizeRoot; |
|
| 47 | + |
|
| 48 | + private $config; |
|
| 49 | + |
|
| 50 | + /** |
|
| 51 | + * @param array $parameters |
|
| 52 | + */ |
|
| 53 | + public function __construct($parameters) { |
|
| 54 | + parent::__construct($parameters); |
|
| 55 | + $this->quota = $parameters['quota']; |
|
| 56 | + $this->sizeRoot = isset($parameters['root']) ? $parameters['root'] : ''; |
|
| 57 | + $this->config = \OC::$server->getSystemConfig(); |
|
| 58 | + } |
|
| 59 | + |
|
| 60 | + /** |
|
| 61 | + * @return int quota value |
|
| 62 | + */ |
|
| 63 | + public function getQuota() { |
|
| 64 | + return $this->quota; |
|
| 65 | + } |
|
| 66 | + |
|
| 67 | + /** |
|
| 68 | + * @param string $path |
|
| 69 | + * @param \OC\Files\Storage\Storage $storage |
|
| 70 | + */ |
|
| 71 | + protected function getSize($path, $storage = null) { |
|
| 72 | + if ($this->config->getValue('quota_include_external_storage', false)) { |
|
| 73 | + $rootInfo = Filesystem::getFileInfo('', 'ext'); |
|
| 74 | + return $rootInfo->getSize(true); |
|
| 75 | + } else { |
|
| 76 | + if (is_null($storage)) { |
|
| 77 | + $cache = $this->getCache(); |
|
| 78 | + } else { |
|
| 79 | + $cache = $storage->getCache(); |
|
| 80 | + } |
|
| 81 | + $data = $cache->get($path); |
|
| 82 | + if ($data instanceof ICacheEntry and isset($data['size'])) { |
|
| 83 | + return $data['size']; |
|
| 84 | + } else { |
|
| 85 | + return \OCP\Files\FileInfo::SPACE_NOT_COMPUTED; |
|
| 86 | + } |
|
| 87 | + } |
|
| 88 | + } |
|
| 89 | + |
|
| 90 | + /** |
|
| 91 | + * Get free space as limited by the quota |
|
| 92 | + * |
|
| 93 | + * @param string $path |
|
| 94 | + * @return int |
|
| 95 | + */ |
|
| 96 | + public function free_space($path) { |
|
| 97 | + if ($this->quota < 0 || strpos($path, 'cache') === 0 || strpos($path, 'uploads') === 0) { |
|
| 98 | + return $this->storage->free_space($path); |
|
| 99 | + } else { |
|
| 100 | + $used = $this->getSize($this->sizeRoot); |
|
| 101 | + if ($used < 0) { |
|
| 102 | + return \OCP\Files\FileInfo::SPACE_NOT_COMPUTED; |
|
| 103 | + } else { |
|
| 104 | + $free = $this->storage->free_space($path); |
|
| 105 | + $quotaFree = max($this->quota - $used, 0); |
|
| 106 | + // if free space is known |
|
| 107 | + if ($free >= 0) { |
|
| 108 | + $free = min($free, $quotaFree); |
|
| 109 | + } else { |
|
| 110 | + $free = $quotaFree; |
|
| 111 | + } |
|
| 112 | + return $free; |
|
| 113 | + } |
|
| 114 | + } |
|
| 115 | + } |
|
| 116 | + |
|
| 117 | + /** |
|
| 118 | + * see http://php.net/manual/en/function.file_put_contents.php |
|
| 119 | + * |
|
| 120 | + * @param string $path |
|
| 121 | + * @param string $data |
|
| 122 | + * @return bool |
|
| 123 | + */ |
|
| 124 | + public function file_put_contents($path, $data) { |
|
| 125 | + $free = $this->free_space($path); |
|
| 126 | + if ($free < 0 or strlen($data) < $free) { |
|
| 127 | + return $this->storage->file_put_contents($path, $data); |
|
| 128 | + } else { |
|
| 129 | + return false; |
|
| 130 | + } |
|
| 131 | + } |
|
| 132 | + |
|
| 133 | + /** |
|
| 134 | + * see http://php.net/manual/en/function.copy.php |
|
| 135 | + * |
|
| 136 | + * @param string $source |
|
| 137 | + * @param string $target |
|
| 138 | + * @return bool |
|
| 139 | + */ |
|
| 140 | + public function copy($source, $target) { |
|
| 141 | + $free = $this->free_space($target); |
|
| 142 | + if ($free < 0 or $this->getSize($source) < $free) { |
|
| 143 | + return $this->storage->copy($source, $target); |
|
| 144 | + } else { |
|
| 145 | + return false; |
|
| 146 | + } |
|
| 147 | + } |
|
| 148 | + |
|
| 149 | + /** |
|
| 150 | + * see http://php.net/manual/en/function.fopen.php |
|
| 151 | + * |
|
| 152 | + * @param string $path |
|
| 153 | + * @param string $mode |
|
| 154 | + * @return resource |
|
| 155 | + */ |
|
| 156 | + public function fopen($path, $mode) { |
|
| 157 | + $source = $this->storage->fopen($path, $mode); |
|
| 158 | + |
|
| 159 | + // don't apply quota for part files |
|
| 160 | + if (!$this->isPartFile($path)) { |
|
| 161 | + $free = $this->free_space($path); |
|
| 162 | + if ($source && $free >= 0 && $mode !== 'r' && $mode !== 'rb') { |
|
| 163 | + // only apply quota for files, not metadata, trash or others |
|
| 164 | + if (strpos(ltrim($path, '/'), 'files/') === 0) { |
|
| 165 | + return \OC\Files\Stream\Quota::wrap($source, $free); |
|
| 166 | + } |
|
| 167 | + } |
|
| 168 | + } |
|
| 169 | + return $source; |
|
| 170 | + } |
|
| 171 | + |
|
| 172 | + /** |
|
| 173 | + * Checks whether the given path is a part file |
|
| 174 | + * |
|
| 175 | + * @param string $path Path that may identify a .part file |
|
| 176 | + * @return string File path without .part extension |
|
| 177 | + * @note this is needed for reusing keys |
|
| 178 | + */ |
|
| 179 | + private function isPartFile($path) { |
|
| 180 | + $extension = pathinfo($path, PATHINFO_EXTENSION); |
|
| 181 | + |
|
| 182 | + return ($extension === 'part'); |
|
| 183 | + } |
|
| 184 | + |
|
| 185 | + /** |
|
| 186 | + * @param IStorage $sourceStorage |
|
| 187 | + * @param string $sourceInternalPath |
|
| 188 | + * @param string $targetInternalPath |
|
| 189 | + * @return bool |
|
| 190 | + */ |
|
| 191 | + public function copyFromStorage(IStorage $sourceStorage, $sourceInternalPath, $targetInternalPath) { |
|
| 192 | + $free = $this->free_space($targetInternalPath); |
|
| 193 | + if ($free < 0 or $this->getSize($sourceInternalPath, $sourceStorage) < $free) { |
|
| 194 | + return $this->storage->copyFromStorage($sourceStorage, $sourceInternalPath, $targetInternalPath); |
|
| 195 | + } else { |
|
| 196 | + return false; |
|
| 197 | + } |
|
| 198 | + } |
|
| 199 | + |
|
| 200 | + /** |
|
| 201 | + * @param IStorage $sourceStorage |
|
| 202 | + * @param string $sourceInternalPath |
|
| 203 | + * @param string $targetInternalPath |
|
| 204 | + * @return bool |
|
| 205 | + */ |
|
| 206 | + public function moveFromStorage(IStorage $sourceStorage, $sourceInternalPath, $targetInternalPath) { |
|
| 207 | + $free = $this->free_space($targetInternalPath); |
|
| 208 | + if ($free < 0 or $this->getSize($sourceInternalPath, $sourceStorage) < $free) { |
|
| 209 | + return $this->storage->moveFromStorage($sourceStorage, $sourceInternalPath, $targetInternalPath); |
|
| 210 | + } else { |
|
| 211 | + return false; |
|
| 212 | + } |
|
| 213 | + } |
|
| 214 | + |
|
| 215 | + public function mkdir($path) { |
|
| 216 | + $free = $this->free_space($path); |
|
| 217 | + if ($free === 0.0) { |
|
| 218 | + return false; |
|
| 219 | + } |
|
| 220 | + |
|
| 221 | + return parent::mkdir($path); |
|
| 222 | + } |
|
| 223 | + |
|
| 224 | + public function touch($path, $mtime = null) { |
|
| 225 | + $free = $this->free_space($path); |
|
| 226 | + if ($free === 0.0) { |
|
| 227 | + return false; |
|
| 228 | + } |
|
| 229 | + |
|
| 230 | + return parent::touch($path, $mtime); |
|
| 231 | + } |
|
| 232 | 232 | |
| 233 | 233 | } |