Completed
Push — master ( e4992c...6d0a35 )
by
unknown
10:42
created

LockPlugin::getLock()   A

Complexity

Conditions 6
Paths 5

Size

Total Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
nc 5
nop 1
dl 0
loc 19
rs 9.0111
c 0
b 0
f 0
1
<?php
2
/**
3
 * @author Robin Appelman <[email protected]>
4
 * @author Roeland Jago Douma <[email protected]>
5
 * @author Stefan Weil <[email protected]>
6
 * @author Thomas Müller <[email protected]>
7
 *
8
 * @copyright Copyright (c) 2018, ownCloud GmbH
9
 * @license AGPL-3.0
10
 *
11
 * This code is free software: you can redistribute it and/or modify
12
 * it under the terms of the GNU Affero General Public License, version 3,
13
 * as published by the Free Software Foundation.
14
 *
15
 * This program is distributed in the hope that it will be useful,
16
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18
 * GNU Affero General Public License for more details.
19
 *
20
 * You should have received a copy of the GNU Affero General Public License, version 3,
21
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
22
 *
23
 */
24
25
namespace OCA\DAV\Connector\Sabre;
26
27
use OCA\DAV\Connector\Sabre\Exception\FileLocked;
28
use OCP\Lock\ILockingProvider;
29
use OCP\Lock\LockedException;
30
use Sabre\DAV\Exception\NotFound;
31
use Sabre\DAV\ServerPlugin;
32
use Sabre\HTTP\RequestInterface;
33
34
class LockPlugin extends ServerPlugin {
35
	/**
36
	 * Reference to main server object
37
	 *
38
	 * @var \Sabre\DAV\Server
39
	 */
40
	private $server;
41
42
	/**
43
	 * {@inheritdoc}
44
	 */
45
	public function initialize(\Sabre\DAV\Server $server) {
46
		$this->server = $server;
47
		$this->server->on('beforeMethod', [$this, 'getLock'], 50);
48
		$this->server->on('afterMethod', [$this, 'releaseLock'], 50);
49
	}
50
51
	public function getLock(RequestInterface $request) {
52
		// we can't listen on 'beforeMethod:PUT' due to order of operations with setting up the tree
53
		// so instead we limit ourselves to the PUT method manually
54
		if ($request->getMethod() !== 'PUT' || \OC_FileChunking::isWebdavChunk()) {
55
			return;
56
		}
57
		try {
58
			$node = $this->server->tree->getNodeForPath($request->getPath());
59
		} catch (NotFound $e) {
60
			return;
61
		}
62
		if ($node instanceof Node) {
63
			try {
64
				$node->acquireLock(ILockingProvider::LOCK_SHARED);
65
			} catch (LockedException $e) {
66
				throw new FileLocked($e->getMessage(), $e->getCode(), $e);
67
			}
68
		}
69
	}
70
71
	public function releaseLock(RequestInterface $request) {
72
		if ($request->getMethod() !== 'PUT' || \OC_FileChunking::isWebdavChunk()) {
73
			return;
74
		}
75
		try {
76
			$node = $this->server->tree->getNodeForPath($request->getPath());
77
		} catch (NotFound $e) {
78
			return;
79
		}
80
		if ($node instanceof Node) {
81
			$node->releaseLock(ILockingProvider::LOCK_SHARED);
82
		}
83
	}
84
}
85