QuotaPlugin.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Felix Moeller <mail@felixmoeller.de>
  6. * @author Morris Jobke <hey@morrisjobke.de>
  7. * @author Robin Appelman <robin@icewind.nl>
  8. * @author scambra <sergio@entrecables.com>
  9. * @author Thomas Müller <thomas.mueller@tmit.eu>
  10. * @author Vincent Petry <pvince81@owncloud.com>
  11. *
  12. * @license AGPL-3.0
  13. *
  14. * This code is free software: you can redistribute it and/or modify
  15. * it under the terms of the GNU Affero General Public License, version 3,
  16. * as published by the Free Software Foundation.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU Affero General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU Affero General Public License, version 3,
  24. * along with this program. If not, see <http://www.gnu.org/licenses/>
  25. *
  26. */
  27. namespace OCA\DAV\Connector\Sabre;
  28. use OCP\Files\FileInfo;
  29. use OCP\Files\StorageNotAvailableException;
  30. use Sabre\DAV\Exception\InsufficientStorage;
  31. use Sabre\DAV\Exception\ServiceUnavailable;
  32. use Sabre\HTTP\URLUtil;
  33. /**
  34. * This plugin check user quota and deny creating files when they exceeds the quota.
  35. *
  36. * @author Sergio Cambra
  37. * @copyright Copyright (C) 2012 entreCables S.L. All rights reserved.
  38. * @license http://code.google.com/p/sabredav/wiki/License Modified BSD License
  39. */
  40. class QuotaPlugin extends \Sabre\DAV\ServerPlugin {
  41. /**
  42. * @var \OC\Files\View
  43. */
  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', array($this, 'checkQuota'), 10);
  71. $server->on('beforeCreateFile', array($this, 'checkQuota'), 10);
  72. }
  73. /**
  74. * This method is called before any HTTP method and validates there is enough free space to store the file
  75. *
  76. * @param string $uri
  77. * @throws InsufficientStorage
  78. * @return bool
  79. */
  80. public function checkQuota($uri) {
  81. $length = $this->getLength();
  82. if ($length) {
  83. if (substr($uri, 0, 1) !== '/') {
  84. $uri = '/' . $uri;
  85. }
  86. list($parentUri, $newName) = URLUtil::splitPath($uri);
  87. if(is_null($parentUri)) {
  88. $parentUri = '';
  89. }
  90. $req = $this->server->httpRequest;
  91. if ($req->getHeader('OC-Chunked')) {
  92. $info = \OC_FileChunking::decodeName($newName);
  93. $chunkHandler = $this->getFileChunking($info);
  94. // subtract the already uploaded size to see whether
  95. // there is still enough space for the remaining chunks
  96. $length -= $chunkHandler->getCurrentSize();
  97. // use target file name for free space check in case of shared files
  98. $uri = rtrim($parentUri, '/') . '/' . $info['name'];
  99. }
  100. $freeSpace = $this->getFreeSpace($uri);
  101. if ($freeSpace !== FileInfo::SPACE_UNKNOWN && $length > $freeSpace) {
  102. if (isset($chunkHandler)) {
  103. $chunkHandler->cleanup();
  104. }
  105. throw new InsufficientStorage();
  106. }
  107. }
  108. return true;
  109. }
  110. public function getFileChunking($info) {
  111. // FIXME: need a factory for better mocking support
  112. return new \OC_FileChunking($info);
  113. }
  114. public function getLength() {
  115. $req = $this->server->httpRequest;
  116. $length = $req->getHeader('X-Expected-Entity-Length');
  117. if (!is_numeric($length)) {
  118. $length = $req->getHeader('Content-Length');
  119. $length = is_numeric($length) ? $length : null;
  120. }
  121. $ocLength = $req->getHeader('OC-Total-Length');
  122. if (is_numeric($length) && is_numeric($ocLength)) {
  123. return max($length, $ocLength);
  124. }
  125. return $length;
  126. }
  127. /**
  128. * @param string $uri
  129. * @return mixed
  130. * @throws ServiceUnavailable
  131. */
  132. public function getFreeSpace($uri) {
  133. try {
  134. $freeSpace = $this->view->free_space(ltrim($uri, '/'));
  135. return $freeSpace;
  136. } catch (StorageNotAvailableException $e) {
  137. throw new ServiceUnavailable($e->getMessage());
  138. }
  139. }
  140. }