summaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorRobin Appelman <icewind@owncloud.com>2013-07-02 18:22:49 +0200
committerRobin Appelman <icewind@owncloud.com>2013-07-25 00:31:51 +0200
commitcd8f0c21ced2283a8cf8b48af1c91d665a1dab49 (patch)
treeabf4ccdea85973172ee7af04992d704a3579714a /lib
parenta853968f7dddcbb18e815f0323ed3046e048e4b1 (diff)
downloadnextcloud-server-cd8f0c21ced2283a8cf8b48af1c91d665a1dab49.tar.gz
nextcloud-server-cd8f0c21ced2283a8cf8b48af1c91d665a1dab49.zip
add quota storage wrapper
Diffstat (limited to 'lib')
-rw-r--r--lib/files/storage/wrapper/quota.php104
1 files changed, 104 insertions, 0 deletions
diff --git a/lib/files/storage/wrapper/quota.php b/lib/files/storage/wrapper/quota.php
new file mode 100644
index 00000000000..bc2d8939760
--- /dev/null
+++ b/lib/files/storage/wrapper/quota.php
@@ -0,0 +1,104 @@
+<?php
+/**
+ * Copyright (c) 2013 Robin Appelman <icewind@owncloud.com>
+ * This file is licensed under the Affero General Public License version 3 or
+ * later.
+ * See the COPYING-README file.
+ */
+
+namespace OC\Files\Storage\Wrapper;
+
+class Quota extends Wrapper {
+
+ /**
+ * @var int $quota
+ */
+ protected $quota;
+
+ /**
+ * @param array $parameters
+ */
+ public function __construct($parameters) {
+ $this->storage = $parameters['storage'];
+ $this->quota = $parameters['quota'];
+ }
+
+ protected function getSize($path) {
+ $cache = $this->getCache();
+ $data = $cache->get($path);
+ if (is_array($data) and isset($data['size'])) {
+ return $data['size'];
+ } else {
+ return \OC\Files\SPACE_NOT_COMPUTED;
+ }
+ }
+
+ /**
+ * Get free space as limited by the quota
+ *
+ * @param string $path
+ * @return int
+ */
+ public function free_space($path) {
+ if ($this->quota < 0) {
+ return $this->storage->free_space($path);
+ } else {
+ $used = $this->getSize('');
+ if ($used < 0) {
+ return \OC\Files\SPACE_NOT_COMPUTED;
+ } else {
+ $free = $this->storage->free_space($path);
+ return min($free, ($this->quota - $used));
+ }
+ }
+ }
+
+ /**
+ * see http://php.net/manual/en/function.file_put_contents.php
+ *
+ * @param string $path
+ * @param string $data
+ * @return bool
+ */
+ public function file_put_contents($path, $data) {
+ $free = $this->free_space('');
+ if ($free < 0 or strlen($data) < $free) {
+ return $this->storage->file_put_contents($path, $data);
+ } else {
+ return false;
+ }
+ }
+
+ /**
+ * see http://php.net/manual/en/function.copy.php
+ *
+ * @param string $source
+ * @param string $target
+ * @return bool
+ */
+ public function copy($source, $target) {
+ $free = $this->free_space('');
+ if ($free < 0 or $this->getSize($source) < $free) {
+ return $this->storage->copy($source, $target);
+ } else {
+ return false;
+ }
+ }
+
+ /**
+ * see http://php.net/manual/en/function.fopen.php
+ *
+ * @param string $path
+ * @param string $mode
+ * @return resource
+ */
+ public function fopen($path, $mode) {
+ $source = $this->storage->fopen($path, $mode);
+ $free = $this->free_space('');
+ if ($free >= 0) {
+ return \OC\Files\Stream\Quota::wrap($source, $free);
+ } else {
+ return $source;
+ }
+ }
+}