Completed
Pull Request — master (#6500)
by Morris
30:52 queued 13:58
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 Morris Jobke <[email protected]>
7
 * @author Robin Appelman <[email protected]>
8
 * @author scambra <[email protected]>
9
 * @author Thomas Müller <[email protected]>
10
 * @author Vincent Petry <[email protected]>
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
/**
35
 * This plugin check user quota and deny creating files when they exceeds the quota.
36
 *
37
 * @author Sergio Cambra
38
 * @copyright Copyright (C) 2012 entreCables S.L. All rights reserved.
39
 * @license http://code.google.com/p/sabredav/wiki/License Modified BSD License
40
 */
41
class QuotaPlugin extends \Sabre\DAV\ServerPlugin {
42
43
	/**
44
	 * @var \OC\Files\View
45
	 */
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', array($this, 'checkQuota'), 10);
78
		$server->on('beforeCreateFile', array($this, 'checkQuota'), 10);
79
	}
80
81
	/**
82
	 * This method is called before any HTTP method and validates there is enough free space to store the file
83
	 *
84
	 * @param string $uri
85
	 * @throws InsufficientStorage
86
	 * @return bool
87
	 */
88
	public function checkQuota($uri) {
89
		$length = $this->getLength();
90
		if ($length) {
91
			if (substr($uri, 0, 1) !== '/') {
92
				$uri = '/' . $uri;
93
			}
94
			list($parentUri, $newName) = \Sabre\Uri\split($uri);
95
			if(is_null($parentUri)) {
96
				$parentUri = '';
97
			}
98
			$req = $this->server->httpRequest;
99
			if ($req->getHeader('OC-Chunked')) {
100
				$info = \OC_FileChunking::decodeName($newName);
101
				$chunkHandler = $this->getFileChunking($info);
102
				// subtract the already uploaded size to see whether
103
				// there is still enough space for the remaining chunks
104
				$length -= $chunkHandler->getCurrentSize();
105
				// use target file name for free space check in case of shared files
106
				$uri = rtrim($parentUri, '/') . '/' . $info['name'];
107
			}
108
			$freeSpace = $this->getFreeSpace($uri);
109
			if ($freeSpace !== FileInfo::SPACE_UNKNOWN && $freeSpace !== FileInfo::SPACE_UNLIMITED && $length > $freeSpace) {
110
				if (isset($chunkHandler)) {
111
					$chunkHandler->cleanup();
112
				}
113
				throw new InsufficientStorage();
114
			}
115
		}
116
		return true;
117
	}
118
119
	public function getFileChunking($info) {
120
		// FIXME: need a factory for better mocking support
121
		return new \OC_FileChunking($info);
122
	}
123
124
	public function getLength() {
125
		$req = $this->server->httpRequest;
126
		$length = $req->getHeader('X-Expected-Entity-Length');
127
		if (!is_numeric($length)) {
128
			$length = $req->getHeader('Content-Length');
129
			$length = is_numeric($length) ? $length : null;
130
		}
131
132
		$ocLength = $req->getHeader('OC-Total-Length');
133
		if (is_numeric($length) && is_numeric($ocLength)) {
134
			return max($length, $ocLength);
135
		}
136
137
		return $length;
138
	}
139
140
	/**
141
	 * @param string $uri
142
	 * @return mixed
143
	 * @throws ServiceUnavailable
144
	 */
145
	public function getFreeSpace($uri) {
146
		try {
147
			$freeSpace = $this->view->free_space(ltrim($uri, '/'));
148
			return $freeSpace;
149
		} catch (StorageNotAvailableException $e) {
150
			throw new ServiceUnavailable($e->getMessage());
151
		}
152
	}
153
}
154