Completed
Push — master ( b7d00f...13d21f )
by Morris
40:15 queued 23:53
created

QuotaPlugin::getFileChunking()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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