@@ -42,172 +42,172 @@ |
||
42 | 42 | */ |
43 | 43 | class QuotaPlugin extends \Sabre\DAV\ServerPlugin { |
44 | 44 | |
45 | - /** @var \OC\Files\View */ |
|
46 | - private $view; |
|
47 | - |
|
48 | - /** |
|
49 | - * Reference to main server object |
|
50 | - * |
|
51 | - * @var \Sabre\DAV\Server |
|
52 | - */ |
|
53 | - private $server; |
|
54 | - |
|
55 | - /** |
|
56 | - * @param \OC\Files\View $view |
|
57 | - */ |
|
58 | - public function __construct($view) { |
|
59 | - $this->view = $view; |
|
60 | - } |
|
61 | - |
|
62 | - /** |
|
63 | - * This initializes the plugin. |
|
64 | - * |
|
65 | - * This function is called by \Sabre\DAV\Server, after |
|
66 | - * addPlugin is called. |
|
67 | - * |
|
68 | - * This method should set up the requires event subscriptions. |
|
69 | - * |
|
70 | - * @param \Sabre\DAV\Server $server |
|
71 | - * @return void |
|
72 | - */ |
|
73 | - public function initialize(\Sabre\DAV\Server $server) { |
|
74 | - |
|
75 | - $this->server = $server; |
|
76 | - |
|
77 | - $server->on('beforeWriteContent', [$this, 'beforeWriteContent'], 10); |
|
78 | - $server->on('beforeCreateFile', [$this, 'beforeCreateFile'], 10); |
|
79 | - $server->on('beforeMove', [$this, 'beforeMove'], 10); |
|
80 | - } |
|
81 | - |
|
82 | - /** |
|
83 | - * Check quota before creating file |
|
84 | - * |
|
85 | - * @param string $uri target file URI |
|
86 | - * @param resource $data data |
|
87 | - * @param INode $parent Sabre Node |
|
88 | - * @param bool $modified modified |
|
89 | - */ |
|
90 | - public function beforeCreateFile($uri, $data, INode $parent, $modified) { |
|
91 | - if (!$parent instanceof Node) { |
|
92 | - return; |
|
93 | - } |
|
94 | - |
|
95 | - return $this->checkQuota($parent->getPath() . '/' . basename($uri)); |
|
96 | - } |
|
97 | - |
|
98 | - /** |
|
99 | - * Check quota before writing content |
|
100 | - * |
|
101 | - * @param string $uri target file URI |
|
102 | - * @param INode $node Sabre Node |
|
103 | - * @param resource $data data |
|
104 | - * @param bool $modified modified |
|
105 | - */ |
|
106 | - public function beforeWriteContent($uri, INode $node, $data, $modified) { |
|
107 | - if (!$node instanceof Node) { |
|
108 | - return; |
|
109 | - } |
|
110 | - |
|
111 | - return $this->checkQuota($node->getPath()); |
|
112 | - } |
|
113 | - |
|
114 | - /** |
|
115 | - * Check if we're moving a Futurefile in which case we need to check |
|
116 | - * the quota on the target destination. |
|
117 | - * |
|
118 | - * @param string $source source path |
|
119 | - * @param string $destination destination path |
|
120 | - */ |
|
121 | - public function beforeMove($source, $destination) { |
|
122 | - $sourceNode = $this->server->tree->getNodeForPath($source); |
|
123 | - if (!$sourceNode instanceof FutureFile) { |
|
124 | - return; |
|
125 | - } |
|
126 | - |
|
127 | - // get target node for proper path conversion |
|
128 | - if ($this->server->tree->nodeExists($destination)) { |
|
129 | - $destinationNode = $this->server->tree->getNodeForPath($destination); |
|
130 | - $path = $destinationNode->getPath(); |
|
131 | - } else { |
|
132 | - $parentNode = $this->server->tree->getNodeForPath(dirname($destination)); |
|
133 | - $path = $parentNode->getPath(); |
|
134 | - } |
|
135 | - |
|
136 | - return $this->checkQuota($path, $sourceNode->getSize()); |
|
137 | - } |
|
138 | - |
|
139 | - |
|
140 | - /** |
|
141 | - * This method is called before any HTTP method and validates there is enough free space to store the file |
|
142 | - * |
|
143 | - * @param string $path relative to the users home |
|
144 | - * @param int $length |
|
145 | - * @throws InsufficientStorage |
|
146 | - * @return bool |
|
147 | - */ |
|
148 | - public function checkQuota($path, $length = null) { |
|
149 | - if ($length === null) { |
|
150 | - $length = $this->getLength(); |
|
151 | - } |
|
152 | - |
|
153 | - if ($length) { |
|
154 | - list($parentPath, $newName) = \Sabre\Uri\split($path); |
|
155 | - if(is_null($parentPath)) { |
|
156 | - $parentPath = ''; |
|
157 | - } |
|
158 | - $req = $this->server->httpRequest; |
|
159 | - if ($req->getHeader('OC-Chunked')) { |
|
160 | - $info = \OC_FileChunking::decodeName($newName); |
|
161 | - $chunkHandler = $this->getFileChunking($info); |
|
162 | - // subtract the already uploaded size to see whether |
|
163 | - // there is still enough space for the remaining chunks |
|
164 | - $length -= $chunkHandler->getCurrentSize(); |
|
165 | - // use target file name for free space check in case of shared files |
|
166 | - $path = rtrim($parentPath, '/') . '/' . $info['name']; |
|
167 | - } |
|
168 | - $freeSpace = $this->getFreeSpace($path); |
|
169 | - if ($freeSpace >= 0 && $length > $freeSpace) { |
|
170 | - if (isset($chunkHandler)) { |
|
171 | - $chunkHandler->cleanup(); |
|
172 | - } |
|
173 | - throw new InsufficientStorage("Insufficient space in $path, $length required, $freeSpace available"); |
|
174 | - } |
|
175 | - } |
|
176 | - return true; |
|
177 | - } |
|
178 | - |
|
179 | - public function getFileChunking($info) { |
|
180 | - // FIXME: need a factory for better mocking support |
|
181 | - return new \OC_FileChunking($info); |
|
182 | - } |
|
183 | - |
|
184 | - public function getLength() { |
|
185 | - $req = $this->server->httpRequest; |
|
186 | - $length = $req->getHeader('X-Expected-Entity-Length'); |
|
187 | - if (!is_numeric($length)) { |
|
188 | - $length = $req->getHeader('Content-Length'); |
|
189 | - $length = is_numeric($length) ? $length : null; |
|
190 | - } |
|
191 | - |
|
192 | - $ocLength = $req->getHeader('OC-Total-Length'); |
|
193 | - if (is_numeric($length) && is_numeric($ocLength)) { |
|
194 | - return max($length, $ocLength); |
|
195 | - } |
|
196 | - |
|
197 | - return $length; |
|
198 | - } |
|
199 | - |
|
200 | - /** |
|
201 | - * @param string $uri |
|
202 | - * @return mixed |
|
203 | - * @throws ServiceUnavailable |
|
204 | - */ |
|
205 | - public function getFreeSpace($uri) { |
|
206 | - try { |
|
207 | - $freeSpace = $this->view->free_space(ltrim($uri, '/')); |
|
208 | - return $freeSpace; |
|
209 | - } catch (StorageNotAvailableException $e) { |
|
210 | - throw new ServiceUnavailable($e->getMessage()); |
|
211 | - } |
|
212 | - } |
|
45 | + /** @var \OC\Files\View */ |
|
46 | + private $view; |
|
47 | + |
|
48 | + /** |
|
49 | + * Reference to main server object |
|
50 | + * |
|
51 | + * @var \Sabre\DAV\Server |
|
52 | + */ |
|
53 | + private $server; |
|
54 | + |
|
55 | + /** |
|
56 | + * @param \OC\Files\View $view |
|
57 | + */ |
|
58 | + public function __construct($view) { |
|
59 | + $this->view = $view; |
|
60 | + } |
|
61 | + |
|
62 | + /** |
|
63 | + * This initializes the plugin. |
|
64 | + * |
|
65 | + * This function is called by \Sabre\DAV\Server, after |
|
66 | + * addPlugin is called. |
|
67 | + * |
|
68 | + * This method should set up the requires event subscriptions. |
|
69 | + * |
|
70 | + * @param \Sabre\DAV\Server $server |
|
71 | + * @return void |
|
72 | + */ |
|
73 | + public function initialize(\Sabre\DAV\Server $server) { |
|
74 | + |
|
75 | + $this->server = $server; |
|
76 | + |
|
77 | + $server->on('beforeWriteContent', [$this, 'beforeWriteContent'], 10); |
|
78 | + $server->on('beforeCreateFile', [$this, 'beforeCreateFile'], 10); |
|
79 | + $server->on('beforeMove', [$this, 'beforeMove'], 10); |
|
80 | + } |
|
81 | + |
|
82 | + /** |
|
83 | + * Check quota before creating file |
|
84 | + * |
|
85 | + * @param string $uri target file URI |
|
86 | + * @param resource $data data |
|
87 | + * @param INode $parent Sabre Node |
|
88 | + * @param bool $modified modified |
|
89 | + */ |
|
90 | + public function beforeCreateFile($uri, $data, INode $parent, $modified) { |
|
91 | + if (!$parent instanceof Node) { |
|
92 | + return; |
|
93 | + } |
|
94 | + |
|
95 | + return $this->checkQuota($parent->getPath() . '/' . basename($uri)); |
|
96 | + } |
|
97 | + |
|
98 | + /** |
|
99 | + * Check quota before writing content |
|
100 | + * |
|
101 | + * @param string $uri target file URI |
|
102 | + * @param INode $node Sabre Node |
|
103 | + * @param resource $data data |
|
104 | + * @param bool $modified modified |
|
105 | + */ |
|
106 | + public function beforeWriteContent($uri, INode $node, $data, $modified) { |
|
107 | + if (!$node instanceof Node) { |
|
108 | + return; |
|
109 | + } |
|
110 | + |
|
111 | + return $this->checkQuota($node->getPath()); |
|
112 | + } |
|
113 | + |
|
114 | + /** |
|
115 | + * Check if we're moving a Futurefile in which case we need to check |
|
116 | + * the quota on the target destination. |
|
117 | + * |
|
118 | + * @param string $source source path |
|
119 | + * @param string $destination destination path |
|
120 | + */ |
|
121 | + public function beforeMove($source, $destination) { |
|
122 | + $sourceNode = $this->server->tree->getNodeForPath($source); |
|
123 | + if (!$sourceNode instanceof FutureFile) { |
|
124 | + return; |
|
125 | + } |
|
126 | + |
|
127 | + // get target node for proper path conversion |
|
128 | + if ($this->server->tree->nodeExists($destination)) { |
|
129 | + $destinationNode = $this->server->tree->getNodeForPath($destination); |
|
130 | + $path = $destinationNode->getPath(); |
|
131 | + } else { |
|
132 | + $parentNode = $this->server->tree->getNodeForPath(dirname($destination)); |
|
133 | + $path = $parentNode->getPath(); |
|
134 | + } |
|
135 | + |
|
136 | + return $this->checkQuota($path, $sourceNode->getSize()); |
|
137 | + } |
|
138 | + |
|
139 | + |
|
140 | + /** |
|
141 | + * This method is called before any HTTP method and validates there is enough free space to store the file |
|
142 | + * |
|
143 | + * @param string $path relative to the users home |
|
144 | + * @param int $length |
|
145 | + * @throws InsufficientStorage |
|
146 | + * @return bool |
|
147 | + */ |
|
148 | + public function checkQuota($path, $length = null) { |
|
149 | + if ($length === null) { |
|
150 | + $length = $this->getLength(); |
|
151 | + } |
|
152 | + |
|
153 | + if ($length) { |
|
154 | + list($parentPath, $newName) = \Sabre\Uri\split($path); |
|
155 | + if(is_null($parentPath)) { |
|
156 | + $parentPath = ''; |
|
157 | + } |
|
158 | + $req = $this->server->httpRequest; |
|
159 | + if ($req->getHeader('OC-Chunked')) { |
|
160 | + $info = \OC_FileChunking::decodeName($newName); |
|
161 | + $chunkHandler = $this->getFileChunking($info); |
|
162 | + // subtract the already uploaded size to see whether |
|
163 | + // there is still enough space for the remaining chunks |
|
164 | + $length -= $chunkHandler->getCurrentSize(); |
|
165 | + // use target file name for free space check in case of shared files |
|
166 | + $path = rtrim($parentPath, '/') . '/' . $info['name']; |
|
167 | + } |
|
168 | + $freeSpace = $this->getFreeSpace($path); |
|
169 | + if ($freeSpace >= 0 && $length > $freeSpace) { |
|
170 | + if (isset($chunkHandler)) { |
|
171 | + $chunkHandler->cleanup(); |
|
172 | + } |
|
173 | + throw new InsufficientStorage("Insufficient space in $path, $length required, $freeSpace available"); |
|
174 | + } |
|
175 | + } |
|
176 | + return true; |
|
177 | + } |
|
178 | + |
|
179 | + public function getFileChunking($info) { |
|
180 | + // FIXME: need a factory for better mocking support |
|
181 | + return new \OC_FileChunking($info); |
|
182 | + } |
|
183 | + |
|
184 | + public function getLength() { |
|
185 | + $req = $this->server->httpRequest; |
|
186 | + $length = $req->getHeader('X-Expected-Entity-Length'); |
|
187 | + if (!is_numeric($length)) { |
|
188 | + $length = $req->getHeader('Content-Length'); |
|
189 | + $length = is_numeric($length) ? $length : null; |
|
190 | + } |
|
191 | + |
|
192 | + $ocLength = $req->getHeader('OC-Total-Length'); |
|
193 | + if (is_numeric($length) && is_numeric($ocLength)) { |
|
194 | + return max($length, $ocLength); |
|
195 | + } |
|
196 | + |
|
197 | + return $length; |
|
198 | + } |
|
199 | + |
|
200 | + /** |
|
201 | + * @param string $uri |
|
202 | + * @return mixed |
|
203 | + * @throws ServiceUnavailable |
|
204 | + */ |
|
205 | + public function getFreeSpace($uri) { |
|
206 | + try { |
|
207 | + $freeSpace = $this->view->free_space(ltrim($uri, '/')); |
|
208 | + return $freeSpace; |
|
209 | + } catch (StorageNotAvailableException $e) { |
|
210 | + throw new ServiceUnavailable($e->getMessage()); |
|
211 | + } |
|
212 | + } |
|
213 | 213 | } |
@@ -92,7 +92,7 @@ discard block |
||
92 | 92 | return; |
93 | 93 | } |
94 | 94 | |
95 | - return $this->checkQuota($parent->getPath() . '/' . basename($uri)); |
|
95 | + return $this->checkQuota($parent->getPath().'/'.basename($uri)); |
|
96 | 96 | } |
97 | 97 | |
98 | 98 | /** |
@@ -152,7 +152,7 @@ discard block |
||
152 | 152 | |
153 | 153 | if ($length) { |
154 | 154 | list($parentPath, $newName) = \Sabre\Uri\split($path); |
155 | - if(is_null($parentPath)) { |
|
155 | + if (is_null($parentPath)) { |
|
156 | 156 | $parentPath = ''; |
157 | 157 | } |
158 | 158 | $req = $this->server->httpRequest; |
@@ -163,7 +163,7 @@ discard block |
||
163 | 163 | // there is still enough space for the remaining chunks |
164 | 164 | $length -= $chunkHandler->getCurrentSize(); |
165 | 165 | // use target file name for free space check in case of shared files |
166 | - $path = rtrim($parentPath, '/') . '/' . $info['name']; |
|
166 | + $path = rtrim($parentPath, '/').'/'.$info['name']; |
|
167 | 167 | } |
168 | 168 | $freeSpace = $this->getFreeSpace($path); |
169 | 169 | if ($freeSpace >= 0 && $length > $freeSpace) { |