@@ -35,208 +35,208 @@ |
||
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 | - if ($rootInfo) { |
|
75 | - return $rootInfo->getSize(true); |
|
76 | - } |
|
77 | - return \OCP\Files\FileInfo::SPACE_NOT_COMPUTED; |
|
78 | - } else { |
|
79 | - if (is_null($storage)) { |
|
80 | - $cache = $this->getCache(); |
|
81 | - } else { |
|
82 | - $cache = $storage->getCache(); |
|
83 | - } |
|
84 | - $data = $cache->get($path); |
|
85 | - if ($data instanceof ICacheEntry and isset($data['size'])) { |
|
86 | - return $data['size']; |
|
87 | - } else { |
|
88 | - return \OCP\Files\FileInfo::SPACE_NOT_COMPUTED; |
|
89 | - } |
|
90 | - } |
|
91 | - } |
|
92 | - |
|
93 | - /** |
|
94 | - * Get free space as limited by the quota |
|
95 | - * |
|
96 | - * @param string $path |
|
97 | - * @return int |
|
98 | - */ |
|
99 | - public function free_space($path) { |
|
100 | - if ($this->quota < 0 || strpos($path, 'cache') === 0 || strpos($path, 'uploads') === 0) { |
|
101 | - return $this->storage->free_space($path); |
|
102 | - } else { |
|
103 | - $used = $this->getSize($this->sizeRoot); |
|
104 | - if ($used < 0) { |
|
105 | - return \OCP\Files\FileInfo::SPACE_NOT_COMPUTED; |
|
106 | - } else { |
|
107 | - $free = $this->storage->free_space($path); |
|
108 | - $quotaFree = max($this->quota - $used, 0); |
|
109 | - // if free space is known |
|
110 | - if ($free >= 0) { |
|
111 | - $free = min($free, $quotaFree); |
|
112 | - } else { |
|
113 | - $free = $quotaFree; |
|
114 | - } |
|
115 | - return $free; |
|
116 | - } |
|
117 | - } |
|
118 | - } |
|
119 | - |
|
120 | - /** |
|
121 | - * see http://php.net/manual/en/function.file_put_contents.php |
|
122 | - * |
|
123 | - * @param string $path |
|
124 | - * @param string $data |
|
125 | - * @return bool |
|
126 | - */ |
|
127 | - public function file_put_contents($path, $data) { |
|
128 | - $free = $this->free_space($path); |
|
129 | - if ($free < 0 or strlen($data) < $free) { |
|
130 | - return $this->storage->file_put_contents($path, $data); |
|
131 | - } else { |
|
132 | - return false; |
|
133 | - } |
|
134 | - } |
|
135 | - |
|
136 | - /** |
|
137 | - * see http://php.net/manual/en/function.copy.php |
|
138 | - * |
|
139 | - * @param string $source |
|
140 | - * @param string $target |
|
141 | - * @return bool |
|
142 | - */ |
|
143 | - public function copy($source, $target) { |
|
144 | - $free = $this->free_space($target); |
|
145 | - if ($free < 0 or $this->getSize($source) < $free) { |
|
146 | - return $this->storage->copy($source, $target); |
|
147 | - } else { |
|
148 | - return false; |
|
149 | - } |
|
150 | - } |
|
151 | - |
|
152 | - /** |
|
153 | - * see http://php.net/manual/en/function.fopen.php |
|
154 | - * |
|
155 | - * @param string $path |
|
156 | - * @param string $mode |
|
157 | - * @return resource |
|
158 | - */ |
|
159 | - public function fopen($path, $mode) { |
|
160 | - $source = $this->storage->fopen($path, $mode); |
|
161 | - |
|
162 | - // don't apply quota for part files |
|
163 | - if (!$this->isPartFile($path)) { |
|
164 | - $free = $this->free_space($path); |
|
165 | - if ($source && $free >= 0 && $mode !== 'r' && $mode !== 'rb') { |
|
166 | - // only apply quota for files, not metadata, trash or others |
|
167 | - if ($this->shouldApplyQuota($path)) { |
|
168 | - return \OC\Files\Stream\Quota::wrap($source, $free); |
|
169 | - } |
|
170 | - } |
|
171 | - } |
|
172 | - return $source; |
|
173 | - } |
|
174 | - |
|
175 | - /** |
|
176 | - * Checks whether the given path is a part file |
|
177 | - * |
|
178 | - * @param string $path Path that may identify a .part file |
|
179 | - * @return string File path without .part extension |
|
180 | - * @note this is needed for reusing keys |
|
181 | - */ |
|
182 | - private function isPartFile($path) { |
|
183 | - $extension = pathinfo($path, PATHINFO_EXTENSION); |
|
184 | - |
|
185 | - return ($extension === 'part'); |
|
186 | - } |
|
187 | - |
|
188 | - /** |
|
189 | - * Only apply quota for files, not metadata, trash or others |
|
190 | - */ |
|
191 | - private function shouldApplyQuota(string $path): bool { |
|
192 | - return strpos(ltrim($path, '/'), 'files/') === 0; |
|
193 | - } |
|
194 | - |
|
195 | - /** |
|
196 | - * @param IStorage $sourceStorage |
|
197 | - * @param string $sourceInternalPath |
|
198 | - * @param string $targetInternalPath |
|
199 | - * @return bool |
|
200 | - */ |
|
201 | - public function copyFromStorage(IStorage $sourceStorage, $sourceInternalPath, $targetInternalPath) { |
|
202 | - $free = $this->free_space($targetInternalPath); |
|
203 | - if ($free < 0 or $this->getSize($sourceInternalPath, $sourceStorage) < $free) { |
|
204 | - return $this->storage->copyFromStorage($sourceStorage, $sourceInternalPath, $targetInternalPath); |
|
205 | - } else { |
|
206 | - return false; |
|
207 | - } |
|
208 | - } |
|
209 | - |
|
210 | - /** |
|
211 | - * @param IStorage $sourceStorage |
|
212 | - * @param string $sourceInternalPath |
|
213 | - * @param string $targetInternalPath |
|
214 | - * @return bool |
|
215 | - */ |
|
216 | - public function moveFromStorage(IStorage $sourceStorage, $sourceInternalPath, $targetInternalPath) { |
|
217 | - $free = $this->free_space($targetInternalPath); |
|
218 | - if ($free < 0 or $this->getSize($sourceInternalPath, $sourceStorage) < $free) { |
|
219 | - return $this->storage->moveFromStorage($sourceStorage, $sourceInternalPath, $targetInternalPath); |
|
220 | - } else { |
|
221 | - return false; |
|
222 | - } |
|
223 | - } |
|
224 | - |
|
225 | - public function mkdir($path) { |
|
226 | - $free = $this->free_space($path); |
|
227 | - if ($this->shouldApplyQuota($path) && $free === 0.0) { |
|
228 | - return false; |
|
229 | - } |
|
230 | - |
|
231 | - return parent::mkdir($path); |
|
232 | - } |
|
233 | - |
|
234 | - public function touch($path, $mtime = null) { |
|
235 | - $free = $this->free_space($path); |
|
236 | - if ($free === 0.0) { |
|
237 | - return false; |
|
238 | - } |
|
239 | - |
|
240 | - return parent::touch($path, $mtime); |
|
241 | - } |
|
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 | + if ($rootInfo) { |
|
75 | + return $rootInfo->getSize(true); |
|
76 | + } |
|
77 | + return \OCP\Files\FileInfo::SPACE_NOT_COMPUTED; |
|
78 | + } else { |
|
79 | + if (is_null($storage)) { |
|
80 | + $cache = $this->getCache(); |
|
81 | + } else { |
|
82 | + $cache = $storage->getCache(); |
|
83 | + } |
|
84 | + $data = $cache->get($path); |
|
85 | + if ($data instanceof ICacheEntry and isset($data['size'])) { |
|
86 | + return $data['size']; |
|
87 | + } else { |
|
88 | + return \OCP\Files\FileInfo::SPACE_NOT_COMPUTED; |
|
89 | + } |
|
90 | + } |
|
91 | + } |
|
92 | + |
|
93 | + /** |
|
94 | + * Get free space as limited by the quota |
|
95 | + * |
|
96 | + * @param string $path |
|
97 | + * @return int |
|
98 | + */ |
|
99 | + public function free_space($path) { |
|
100 | + if ($this->quota < 0 || strpos($path, 'cache') === 0 || strpos($path, 'uploads') === 0) { |
|
101 | + return $this->storage->free_space($path); |
|
102 | + } else { |
|
103 | + $used = $this->getSize($this->sizeRoot); |
|
104 | + if ($used < 0) { |
|
105 | + return \OCP\Files\FileInfo::SPACE_NOT_COMPUTED; |
|
106 | + } else { |
|
107 | + $free = $this->storage->free_space($path); |
|
108 | + $quotaFree = max($this->quota - $used, 0); |
|
109 | + // if free space is known |
|
110 | + if ($free >= 0) { |
|
111 | + $free = min($free, $quotaFree); |
|
112 | + } else { |
|
113 | + $free = $quotaFree; |
|
114 | + } |
|
115 | + return $free; |
|
116 | + } |
|
117 | + } |
|
118 | + } |
|
119 | + |
|
120 | + /** |
|
121 | + * see http://php.net/manual/en/function.file_put_contents.php |
|
122 | + * |
|
123 | + * @param string $path |
|
124 | + * @param string $data |
|
125 | + * @return bool |
|
126 | + */ |
|
127 | + public function file_put_contents($path, $data) { |
|
128 | + $free = $this->free_space($path); |
|
129 | + if ($free < 0 or strlen($data) < $free) { |
|
130 | + return $this->storage->file_put_contents($path, $data); |
|
131 | + } else { |
|
132 | + return false; |
|
133 | + } |
|
134 | + } |
|
135 | + |
|
136 | + /** |
|
137 | + * see http://php.net/manual/en/function.copy.php |
|
138 | + * |
|
139 | + * @param string $source |
|
140 | + * @param string $target |
|
141 | + * @return bool |
|
142 | + */ |
|
143 | + public function copy($source, $target) { |
|
144 | + $free = $this->free_space($target); |
|
145 | + if ($free < 0 or $this->getSize($source) < $free) { |
|
146 | + return $this->storage->copy($source, $target); |
|
147 | + } else { |
|
148 | + return false; |
|
149 | + } |
|
150 | + } |
|
151 | + |
|
152 | + /** |
|
153 | + * see http://php.net/manual/en/function.fopen.php |
|
154 | + * |
|
155 | + * @param string $path |
|
156 | + * @param string $mode |
|
157 | + * @return resource |
|
158 | + */ |
|
159 | + public function fopen($path, $mode) { |
|
160 | + $source = $this->storage->fopen($path, $mode); |
|
161 | + |
|
162 | + // don't apply quota for part files |
|
163 | + if (!$this->isPartFile($path)) { |
|
164 | + $free = $this->free_space($path); |
|
165 | + if ($source && $free >= 0 && $mode !== 'r' && $mode !== 'rb') { |
|
166 | + // only apply quota for files, not metadata, trash or others |
|
167 | + if ($this->shouldApplyQuota($path)) { |
|
168 | + return \OC\Files\Stream\Quota::wrap($source, $free); |
|
169 | + } |
|
170 | + } |
|
171 | + } |
|
172 | + return $source; |
|
173 | + } |
|
174 | + |
|
175 | + /** |
|
176 | + * Checks whether the given path is a part file |
|
177 | + * |
|
178 | + * @param string $path Path that may identify a .part file |
|
179 | + * @return string File path without .part extension |
|
180 | + * @note this is needed for reusing keys |
|
181 | + */ |
|
182 | + private function isPartFile($path) { |
|
183 | + $extension = pathinfo($path, PATHINFO_EXTENSION); |
|
184 | + |
|
185 | + return ($extension === 'part'); |
|
186 | + } |
|
187 | + |
|
188 | + /** |
|
189 | + * Only apply quota for files, not metadata, trash or others |
|
190 | + */ |
|
191 | + private function shouldApplyQuota(string $path): bool { |
|
192 | + return strpos(ltrim($path, '/'), 'files/') === 0; |
|
193 | + } |
|
194 | + |
|
195 | + /** |
|
196 | + * @param IStorage $sourceStorage |
|
197 | + * @param string $sourceInternalPath |
|
198 | + * @param string $targetInternalPath |
|
199 | + * @return bool |
|
200 | + */ |
|
201 | + public function copyFromStorage(IStorage $sourceStorage, $sourceInternalPath, $targetInternalPath) { |
|
202 | + $free = $this->free_space($targetInternalPath); |
|
203 | + if ($free < 0 or $this->getSize($sourceInternalPath, $sourceStorage) < $free) { |
|
204 | + return $this->storage->copyFromStorage($sourceStorage, $sourceInternalPath, $targetInternalPath); |
|
205 | + } else { |
|
206 | + return false; |
|
207 | + } |
|
208 | + } |
|
209 | + |
|
210 | + /** |
|
211 | + * @param IStorage $sourceStorage |
|
212 | + * @param string $sourceInternalPath |
|
213 | + * @param string $targetInternalPath |
|
214 | + * @return bool |
|
215 | + */ |
|
216 | + public function moveFromStorage(IStorage $sourceStorage, $sourceInternalPath, $targetInternalPath) { |
|
217 | + $free = $this->free_space($targetInternalPath); |
|
218 | + if ($free < 0 or $this->getSize($sourceInternalPath, $sourceStorage) < $free) { |
|
219 | + return $this->storage->moveFromStorage($sourceStorage, $sourceInternalPath, $targetInternalPath); |
|
220 | + } else { |
|
221 | + return false; |
|
222 | + } |
|
223 | + } |
|
224 | + |
|
225 | + public function mkdir($path) { |
|
226 | + $free = $this->free_space($path); |
|
227 | + if ($this->shouldApplyQuota($path) && $free === 0.0) { |
|
228 | + return false; |
|
229 | + } |
|
230 | + |
|
231 | + return parent::mkdir($path); |
|
232 | + } |
|
233 | + |
|
234 | + public function touch($path, $mtime = null) { |
|
235 | + $free = $this->free_space($path); |
|
236 | + if ($free === 0.0) { |
|
237 | + return false; |
|
238 | + } |
|
239 | + |
|
240 | + return parent::touch($path, $mtime); |
|
241 | + } |
|
242 | 242 | } |