QuotaPlugin.php 6.0 KB

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