Quota.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Jörn Friedrich Dreyer <jfd@butonic.de>
  6. * @author Lukas Reschke <lukas@statuscode.ch>
  7. * @author Morris Jobke <hey@morrisjobke.de>
  8. * @author Robin Appelman <robin@icewind.nl>
  9. * @author Robin McCorkell <robin@mccorkell.me.uk>
  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 OC\Files\Storage\Wrapper;
  28. use OCP\Files\Cache\ICacheEntry;
  29. use OCP\Files\Storage\IStorage;
  30. class Quota extends Wrapper {
  31. /**
  32. * @var int $quota
  33. */
  34. protected $quota;
  35. /**
  36. * @var string $sizeRoot
  37. */
  38. protected $sizeRoot;
  39. /**
  40. * @param array $parameters
  41. */
  42. public function __construct($parameters) {
  43. parent::__construct($parameters);
  44. $this->quota = $parameters['quota'];
  45. $this->sizeRoot = isset($parameters['root']) ? $parameters['root'] : '';
  46. }
  47. /**
  48. * @return int quota value
  49. */
  50. public function getQuota() {
  51. return $this->quota;
  52. }
  53. /**
  54. * @param string $path
  55. * @param \OC\Files\Storage\Storage $storage
  56. */
  57. protected function getSize($path, $storage = null) {
  58. if (is_null($storage)) {
  59. $cache = $this->getCache();
  60. } else {
  61. $cache = $storage->getCache();
  62. }
  63. $data = $cache->get($path);
  64. if ($data instanceof ICacheEntry and isset($data['size'])) {
  65. return $data['size'];
  66. } else {
  67. return \OCP\Files\FileInfo::SPACE_NOT_COMPUTED;
  68. }
  69. }
  70. /**
  71. * Get free space as limited by the quota
  72. *
  73. * @param string $path
  74. * @return int
  75. */
  76. public function free_space($path) {
  77. if ($this->quota < 0 || strpos($path, 'cache') === 0 || strpos($path, 'uploads') === 0) {
  78. return $this->storage->free_space($path);
  79. } else {
  80. $used = $this->getSize($this->sizeRoot);
  81. if ($used < 0) {
  82. return \OCP\Files\FileInfo::SPACE_NOT_COMPUTED;
  83. } else {
  84. $free = $this->storage->free_space($path);
  85. $quotaFree = max($this->quota - $used, 0);
  86. // if free space is known
  87. if ($free >= 0) {
  88. $free = min($free, $quotaFree);
  89. } else {
  90. $free = $quotaFree;
  91. }
  92. return $free;
  93. }
  94. }
  95. }
  96. /**
  97. * see http://php.net/manual/en/function.file_put_contents.php
  98. *
  99. * @param string $path
  100. * @param string $data
  101. * @return bool
  102. */
  103. public function file_put_contents($path, $data) {
  104. $free = $this->free_space($path);
  105. if ($free < 0 or strlen($data) < $free) {
  106. return $this->storage->file_put_contents($path, $data);
  107. } else {
  108. return false;
  109. }
  110. }
  111. /**
  112. * see http://php.net/manual/en/function.copy.php
  113. *
  114. * @param string $source
  115. * @param string $target
  116. * @return bool
  117. */
  118. public function copy($source, $target) {
  119. $free = $this->free_space($target);
  120. if ($free < 0 or $this->getSize($source) < $free) {
  121. return $this->storage->copy($source, $target);
  122. } else {
  123. return false;
  124. }
  125. }
  126. /**
  127. * see http://php.net/manual/en/function.fopen.php
  128. *
  129. * @param string $path
  130. * @param string $mode
  131. * @return resource
  132. */
  133. public function fopen($path, $mode) {
  134. $source = $this->storage->fopen($path, $mode);
  135. // don't apply quota for part files
  136. if (!$this->isPartFile($path)) {
  137. $free = $this->free_space($path);
  138. if ($source && $free >= 0 && $mode !== 'r' && $mode !== 'rb') {
  139. // only apply quota for files, not metadata, trash or others
  140. if (strpos(ltrim($path, '/'), 'files/') === 0) {
  141. return \OC\Files\Stream\Quota::wrap($source, $free);
  142. }
  143. }
  144. }
  145. return $source;
  146. }
  147. /**
  148. * Checks whether the given path is a part file
  149. *
  150. * @param string $path Path that may identify a .part file
  151. * @return string File path without .part extension
  152. * @note this is needed for reusing keys
  153. */
  154. private function isPartFile($path) {
  155. $extension = pathinfo($path, PATHINFO_EXTENSION);
  156. return ($extension === 'part');
  157. }
  158. /**
  159. * @param IStorage $sourceStorage
  160. * @param string $sourceInternalPath
  161. * @param string $targetInternalPath
  162. * @return bool
  163. */
  164. public function copyFromStorage(IStorage $sourceStorage, $sourceInternalPath, $targetInternalPath) {
  165. $free = $this->free_space($targetInternalPath);
  166. if ($free < 0 or $this->getSize($sourceInternalPath, $sourceStorage) < $free) {
  167. return $this->storage->copyFromStorage($sourceStorage, $sourceInternalPath, $targetInternalPath);
  168. } else {
  169. return false;
  170. }
  171. }
  172. /**
  173. * @param IStorage $sourceStorage
  174. * @param string $sourceInternalPath
  175. * @param string $targetInternalPath
  176. * @return bool
  177. */
  178. public function moveFromStorage(IStorage $sourceStorage, $sourceInternalPath, $targetInternalPath) {
  179. $free = $this->free_space($targetInternalPath);
  180. if ($free < 0 or $this->getSize($sourceInternalPath, $sourceStorage) < $free) {
  181. return $this->storage->moveFromStorage($sourceStorage, $sourceInternalPath, $targetInternalPath);
  182. } else {
  183. return false;
  184. }
  185. }
  186. public function mkdir($path) {
  187. $free = $this->free_space($path);
  188. if ($free === 0.0) {
  189. return false;
  190. }
  191. return parent::mkdir($path);
  192. }
  193. }