QuotaPlugin.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. * @copyright Copyright (C) 2012 entreCables S.L. All rights reserved.
  5. * @copyright Copyright (C) 2012 entreCables S.L. All rights reserved.
  6. *
  7. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  8. * @author Felix Moeller <mail@felixmoeller.de>
  9. * @author Joas Schilling <coding@schilljs.com>
  10. * @author Robin Appelman <robin@icewind.nl>
  11. * @author Roeland Jago Douma <roeland@famdouma.nl>
  12. * @author scambra <sergio@entrecables.com>
  13. * @author Thomas Müller <thomas.mueller@tmit.eu>
  14. * @author Vincent Petry <vincent@nextcloud.com>
  15. *
  16. * @license AGPL-3.0
  17. *
  18. * This code is free software: you can redistribute it and/or modify
  19. * it under the terms of the GNU Affero General Public License, version 3,
  20. * as published by the Free Software Foundation.
  21. *
  22. * This program is distributed in the hope that it will be useful,
  23. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  24. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  25. * GNU Affero General Public License for more details.
  26. *
  27. * You should have received a copy of the GNU Affero General Public License, version 3,
  28. * along with this program. If not, see <http://www.gnu.org/licenses/>
  29. *
  30. */
  31. namespace OCA\DAV\Connector\Sabre;
  32. use OCA\DAV\Upload\FutureFile;
  33. use OCA\DAV\Upload\UploadFolder;
  34. use OCP\Files\StorageNotAvailableException;
  35. use Sabre\DAV\Exception\InsufficientStorage;
  36. use Sabre\DAV\Exception\ServiceUnavailable;
  37. use Sabre\DAV\INode;
  38. /**
  39. * This plugin check user quota and deny creating files when they exceeds the quota.
  40. *
  41. * @author Sergio Cambra
  42. * @copyright Copyright (C) 2012 entreCables S.L. All rights reserved.
  43. * @license http://code.google.com/p/sabredav/wiki/License Modified BSD License
  44. */
  45. class QuotaPlugin extends \Sabre\DAV\ServerPlugin {
  46. /** @var \OC\Files\View */
  47. private $view;
  48. /**
  49. * Reference to main server object
  50. *
  51. * @var \Sabre\DAV\Server
  52. */
  53. private $server;
  54. /**
  55. * @param \OC\Files\View $view
  56. */
  57. public function __construct($view) {
  58. $this->view = $view;
  59. }
  60. /**
  61. * This initializes the plugin.
  62. *
  63. * This function is called by \Sabre\DAV\Server, after
  64. * addPlugin is called.
  65. *
  66. * This method should set up the requires event subscriptions.
  67. *
  68. * @param \Sabre\DAV\Server $server
  69. * @return void
  70. */
  71. public function initialize(\Sabre\DAV\Server $server) {
  72. $this->server = $server;
  73. $server->on('beforeWriteContent', [$this, 'beforeWriteContent'], 10);
  74. $server->on('beforeCreateFile', [$this, 'beforeCreateFile'], 10);
  75. $server->on('beforeMove', [$this, 'beforeMove'], 10);
  76. $server->on('beforeCopy', [$this, 'beforeCopy'], 10);
  77. }
  78. /**
  79. * Check quota before creating file
  80. *
  81. * @param string $uri target file URI
  82. * @param resource $data data
  83. * @param INode $parent Sabre Node
  84. * @param bool $modified modified
  85. */
  86. public function beforeCreateFile($uri, $data, INode $parent, $modified) {
  87. $request = $this->server->httpRequest;
  88. if ($parent instanceof UploadFolder && $request->getHeader('Destination')) {
  89. // If chunked upload and Total-Length header is set, use that
  90. // value for quota check. This allows us to also check quota while
  91. // uploading chunks and not only when the file is assembled.
  92. $length = $request->getHeader('OC-Total-Length');
  93. $destinationPath = $this->server->calculateUri($request->getHeader('Destination'));
  94. $quotaPath = $this->getPathForDestination($destinationPath);
  95. if ($quotaPath && is_numeric($length)) {
  96. return $this->checkQuota($quotaPath, (int)$length);
  97. }
  98. }
  99. if (!$parent instanceof Node) {
  100. return;
  101. }
  102. return $this->checkQuota($parent->getPath() . '/' . basename($uri));
  103. }
  104. /**
  105. * Check quota before writing content
  106. *
  107. * @param string $uri target file URI
  108. * @param INode $node Sabre Node
  109. * @param resource $data data
  110. * @param bool $modified modified
  111. */
  112. public function beforeWriteContent($uri, INode $node, $data, $modified) {
  113. if (!$node instanceof Node) {
  114. return;
  115. }
  116. return $this->checkQuota($node->getPath());
  117. }
  118. /**
  119. * Check if we're moving a FutureFile in which case we need to check
  120. * the quota on the target destination.
  121. */
  122. public function beforeMove(string $sourcePath, string $destinationPath): bool {
  123. $sourceNode = $this->server->tree->getNodeForPath($sourcePath);
  124. if (!$sourceNode instanceof FutureFile) {
  125. return true;
  126. }
  127. try {
  128. // The final path is not known yet, we check the quota on the parent
  129. $path = $this->getPathForDestination($destinationPath);
  130. } catch (\Exception $e) {
  131. return true;
  132. }
  133. return $this->checkQuota($path, $sourceNode->getSize());
  134. }
  135. /**
  136. * Check quota on the target destination before a copy.
  137. */
  138. public function beforeCopy(string $sourcePath, string $destinationPath): bool {
  139. $sourceNode = $this->server->tree->getNodeForPath($sourcePath);
  140. if (!$sourceNode instanceof Node) {
  141. return true;
  142. }
  143. try {
  144. $path = $this->getPathForDestination($destinationPath);
  145. } catch (\Exception $e) {
  146. return true;
  147. }
  148. return $this->checkQuota($path, $sourceNode->getSize());
  149. }
  150. private function getPathForDestination(string $destinationPath): string {
  151. // get target node for proper path conversion
  152. if ($this->server->tree->nodeExists($destinationPath)) {
  153. $destinationNode = $this->server->tree->getNodeForPath($destinationPath);
  154. if (!$destinationNode instanceof Node) {
  155. throw new \Exception('Invalid destination node');
  156. }
  157. return $destinationNode->getPath();
  158. }
  159. $parent = dirname($destinationPath);
  160. if ($parent === '.') {
  161. $parent = '';
  162. }
  163. $parentNode = $this->server->tree->getNodeForPath($parent);
  164. if (!$parentNode instanceof Node) {
  165. throw new \Exception('Invalid destination node');
  166. }
  167. return $parentNode->getPath();
  168. }
  169. /**
  170. * This method is called before any HTTP method and validates there is enough free space to store the file
  171. *
  172. * @param string $path relative to the users home
  173. * @param int|float|null $length
  174. * @throws InsufficientStorage
  175. * @return bool
  176. */
  177. public function checkQuota(string $path, $length = null) {
  178. if ($length === null) {
  179. $length = $this->getLength();
  180. }
  181. if ($length) {
  182. [$parentPath, $newName] = \Sabre\Uri\split($path);
  183. if (is_null($parentPath)) {
  184. $parentPath = '';
  185. }
  186. $req = $this->server->httpRequest;
  187. // If LEGACY chunked upload
  188. if ($req->getHeader('OC-Chunked')) {
  189. $info = \OC_FileChunking::decodeName($newName);
  190. $chunkHandler = $this->getFileChunking($info);
  191. // subtract the already uploaded size to see whether
  192. // there is still enough space for the remaining chunks
  193. $length -= $chunkHandler->getCurrentSize();
  194. // use target file name for free space check in case of shared files
  195. $path = rtrim($parentPath, '/') . '/' . $info['name'];
  196. }
  197. // Strip any duplicate slashes
  198. $path = str_replace('//', '/', $path);
  199. $freeSpace = $this->getFreeSpace($path);
  200. if ($freeSpace >= 0 && $length > $freeSpace) {
  201. // If LEGACY chunked upload, clean up
  202. if (isset($chunkHandler)) {
  203. $chunkHandler->cleanup();
  204. }
  205. throw new InsufficientStorage("Insufficient space in $path, $length required, $freeSpace available");
  206. }
  207. }
  208. return true;
  209. }
  210. public function getFileChunking($info) {
  211. // FIXME: need a factory for better mocking support
  212. return new \OC_FileChunking($info);
  213. }
  214. public function getLength() {
  215. $req = $this->server->httpRequest;
  216. $length = $req->getHeader('X-Expected-Entity-Length');
  217. if (!is_numeric($length)) {
  218. $length = $req->getHeader('Content-Length');
  219. $length = is_numeric($length) ? $length : null;
  220. }
  221. $ocLength = $req->getHeader('OC-Total-Length');
  222. if (is_numeric($length) && is_numeric($ocLength)) {
  223. return max($length, $ocLength);
  224. }
  225. return $length;
  226. }
  227. /**
  228. * @param string $uri
  229. * @return mixed
  230. * @throws ServiceUnavailable
  231. */
  232. public function getFreeSpace($uri) {
  233. try {
  234. $freeSpace = $this->view->free_space(ltrim($uri, '/'));
  235. return $freeSpace;
  236. } catch (StorageNotAvailableException $e) {
  237. throw new ServiceUnavailable($e->getMessage());
  238. }
  239. }
  240. }