Quota.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  6. * @author J0WI <J0WI@users.noreply.github.com>
  7. * @author John Molakvoæ <skjnldsv@protonmail.com>
  8. * @author Jörn Friedrich Dreyer <jfd@butonic.de>
  9. * @author Julius Härtl <jus@bitgrid.net>
  10. * @author Lukas Reschke <lukas@statuscode.ch>
  11. * @author Morris Jobke <hey@morrisjobke.de>
  12. * @author Robin Appelman <robin@icewind.nl>
  13. * @author Robin McCorkell <robin@mccorkell.me.uk>
  14. * @author Roeland Jago Douma <roeland@famdouma.nl>
  15. * @author Tigran Mkrtchyan <tigran.mkrtchyan@desy.de>
  16. * @author Vincent Petry <vincent@nextcloud.com>
  17. *
  18. * @license AGPL-3.0
  19. *
  20. * This code is free software: you can redistribute it and/or modify
  21. * it under the terms of the GNU Affero General Public License, version 3,
  22. * as published by the Free Software Foundation.
  23. *
  24. * This program is distributed in the hope that it will be useful,
  25. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  26. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  27. * GNU Affero General Public License for more details.
  28. *
  29. * You should have received a copy of the GNU Affero General Public License, version 3,
  30. * along with this program. If not, see <http://www.gnu.org/licenses/>
  31. *
  32. */
  33. namespace OC\Files\Storage\Wrapper;
  34. use OC\Files\Filesystem;
  35. use OC\SystemConfig;
  36. use OCP\Files\Cache\ICacheEntry;
  37. use OCP\Files\FileInfo;
  38. use OCP\Files\Storage\IStorage;
  39. class Quota extends Wrapper {
  40. /** @var callable|null */
  41. protected $quotaCallback;
  42. protected ?int $quota;
  43. protected string $sizeRoot;
  44. private SystemConfig $config;
  45. /**
  46. * @param array $parameters
  47. */
  48. public function __construct($parameters) {
  49. parent::__construct($parameters);
  50. $this->quota = $parameters['quota'] ?? null;
  51. $this->quotaCallback = $parameters['quotaCallback'] ?? null;
  52. $this->sizeRoot = $parameters['root'] ?? '';
  53. $this->config = \OC::$server->get(SystemConfig::class);
  54. }
  55. /**
  56. * @return int quota value
  57. */
  58. public function getQuota(): int {
  59. if ($this->quota === null) {
  60. $quotaCallback = $this->quotaCallback;
  61. if ($quotaCallback === null) {
  62. throw new \Exception("No quota or quota callback provider");
  63. }
  64. $this->quota = $quotaCallback();
  65. }
  66. return $this->quota;
  67. }
  68. private function hasQuota(): bool {
  69. return $this->getQuota() !== FileInfo::SPACE_UNLIMITED;
  70. }
  71. /**
  72. * @param string $path
  73. * @param \OC\Files\Storage\Storage $storage
  74. */
  75. protected function getSize($path, $storage = null) {
  76. if ($this->config->getValue('quota_include_external_storage', false)) {
  77. $rootInfo = Filesystem::getFileInfo('', 'ext');
  78. if ($rootInfo) {
  79. return $rootInfo->getSize(true);
  80. }
  81. return FileInfo::SPACE_NOT_COMPUTED;
  82. } else {
  83. $cache = is_null($storage) ? $this->getCache() : $storage->getCache();
  84. $data = $cache->get($path);
  85. if ($data instanceof ICacheEntry && isset($data['size'])) {
  86. return $data['size'];
  87. } else {
  88. return FileInfo::SPACE_NOT_COMPUTED;
  89. }
  90. }
  91. }
  92. /**
  93. * Get free space as limited by the quota
  94. *
  95. * @param string $path
  96. * @return int|bool
  97. */
  98. public function free_space($path) {
  99. if (!$this->hasQuota()) {
  100. return $this->storage->free_space($path);
  101. }
  102. if ($this->getQuota() < 0 || strpos($path, 'cache') === 0 || strpos($path, 'uploads') === 0) {
  103. return $this->storage->free_space($path);
  104. } else {
  105. $used = $this->getSize($this->sizeRoot);
  106. if ($used < 0) {
  107. return FileInfo::SPACE_NOT_COMPUTED;
  108. } else {
  109. $free = $this->storage->free_space($path);
  110. $quotaFree = max($this->getQuota() - $used, 0);
  111. // if free space is known
  112. $free = $free >= 0 ? min($free, $quotaFree) : $quotaFree;
  113. return $free;
  114. }
  115. }
  116. }
  117. /**
  118. * see https://www.php.net/manual/en/function.file_put_contents.php
  119. *
  120. * @param string $path
  121. * @param mixed $data
  122. * @return int|false
  123. */
  124. public function file_put_contents($path, $data) {
  125. if (!$this->hasQuota()) {
  126. return $this->storage->file_put_contents($path, $data);
  127. }
  128. $free = $this->free_space($path);
  129. if ($free < 0 || strlen($data) < $free) {
  130. return $this->storage->file_put_contents($path, $data);
  131. } else {
  132. return false;
  133. }
  134. }
  135. /**
  136. * see https://www.php.net/manual/en/function.copy.php
  137. *
  138. * @param string $source
  139. * @param string $target
  140. * @return bool
  141. */
  142. public function copy($source, $target) {
  143. if (!$this->hasQuota()) {
  144. return $this->storage->copy($source, $target);
  145. }
  146. $free = $this->free_space($target);
  147. if ($free < 0 || $this->getSize($source) < $free) {
  148. return $this->storage->copy($source, $target);
  149. } else {
  150. return false;
  151. }
  152. }
  153. /**
  154. * see https://www.php.net/manual/en/function.fopen.php
  155. *
  156. * @param string $path
  157. * @param string $mode
  158. * @return resource|bool
  159. */
  160. public function fopen($path, $mode) {
  161. if (!$this->hasQuota()) {
  162. return $this->storage->fopen($path, $mode);
  163. }
  164. $source = $this->storage->fopen($path, $mode);
  165. // don't apply quota for part files
  166. if (!$this->isPartFile($path)) {
  167. $free = $this->free_space($path);
  168. if ($source && is_int($free) && $free >= 0 && $mode !== 'r' && $mode !== 'rb') {
  169. // only apply quota for files, not metadata, trash or others
  170. if ($this->shouldApplyQuota($path)) {
  171. return \OC\Files\Stream\Quota::wrap($source, $free);
  172. }
  173. }
  174. }
  175. return $source;
  176. }
  177. /**
  178. * Checks whether the given path is a part file
  179. *
  180. * @param string $path Path that may identify a .part file
  181. * @return string File path without .part extension
  182. * @note this is needed for reusing keys
  183. */
  184. private function isPartFile($path) {
  185. $extension = pathinfo($path, PATHINFO_EXTENSION);
  186. return ($extension === 'part');
  187. }
  188. /**
  189. * Only apply quota for files, not metadata, trash or others
  190. */
  191. private function shouldApplyQuota(string $path): bool {
  192. return strpos(ltrim($path, '/'), 'files/') === 0;
  193. }
  194. /**
  195. * @param IStorage $sourceStorage
  196. * @param string $sourceInternalPath
  197. * @param string $targetInternalPath
  198. * @return bool
  199. */
  200. public function copyFromStorage(IStorage $sourceStorage, $sourceInternalPath, $targetInternalPath) {
  201. if (!$this->hasQuota()) {
  202. return $this->storage->copyFromStorage($sourceStorage, $sourceInternalPath, $targetInternalPath);
  203. }
  204. $free = $this->free_space($targetInternalPath);
  205. if ($free < 0 || $this->getSize($sourceInternalPath, $sourceStorage) < $free) {
  206. return $this->storage->copyFromStorage($sourceStorage, $sourceInternalPath, $targetInternalPath);
  207. } else {
  208. return false;
  209. }
  210. }
  211. /**
  212. * @param IStorage $sourceStorage
  213. * @param string $sourceInternalPath
  214. * @param string $targetInternalPath
  215. * @return bool
  216. */
  217. public function moveFromStorage(IStorage $sourceStorage, $sourceInternalPath, $targetInternalPath) {
  218. if (!$this->hasQuota()) {
  219. return $this->storage->moveFromStorage($sourceStorage, $sourceInternalPath, $targetInternalPath);
  220. }
  221. $free = $this->free_space($targetInternalPath);
  222. if ($free < 0 || $this->getSize($sourceInternalPath, $sourceStorage) < $free) {
  223. return $this->storage->moveFromStorage($sourceStorage, $sourceInternalPath, $targetInternalPath);
  224. } else {
  225. return false;
  226. }
  227. }
  228. public function mkdir($path) {
  229. if (!$this->hasQuota()) {
  230. return $this->storage->mkdir($path);
  231. }
  232. $free = $this->free_space($path);
  233. if ($this->shouldApplyQuota($path) && $free == 0) {
  234. return false;
  235. }
  236. return parent::mkdir($path);
  237. }
  238. public function touch($path, $mtime = null) {
  239. if (!$this->hasQuota()) {
  240. return $this->storage->touch($path, $mtime);
  241. }
  242. $free = $this->free_space($path);
  243. if ($free == 0) {
  244. return false;
  245. }
  246. return parent::touch($path, $mtime);
  247. }
  248. }