QuotaPlugin.php 7.1 KB

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