Completed
Push — master ( db56df...bd2a8e )
by Lukas
08:30
created

FilesDropPlugin::beforeMethod()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 17
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 10
c 1
b 0
f 0
nc 3
nop 2
dl 0
loc 17
rs 9.4285
1
<?php
2
/**
3
 * @copyright Copyright (c) 2016, Roeland Jago Douma <[email protected]>
4
 *
5
 * @author Roeland Jago Douma <[email protected]>
6
 *
7
 * @license GNU AGPL version 3 or any later version
8
 *
9
 * This program is free software: you can redistribute it and/or modify
10
 * it under the terms of the GNU Affero General Public License as
11
 * published by the Free Software Foundation, either version 3 of the
12
 * License, or (at your option) any later version.
13
 *
14
 * This program is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 * GNU Affero General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU Affero General Public License
20
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21
 *
22
 */
23
namespace OCA\DAV\Files\Sharing;
24
25
use OC\Files\View;
26
use Sabre\DAV\Exception\MethodNotAllowed;
27
use Sabre\DAV\ServerPlugin;
28
use Sabre\HTTP\RequestInterface;
29
use Sabre\HTTP\ResponseInterface;
30
31
/**
32
 * Make sure that the destination is writable
33
 */
34
class FilesDropPlugin extends ServerPlugin {
35
36
	/** @var View */
37
	private $view;
38
39
	/** @var bool */
40
	private $enabled = false;
41
42
	/**
43
	 * @param View $view
44
	 */
45
	public function setView($view) {
46
		$this->view = $view;
47
	}
48
49
	public function enable() {
50
		$this->enabled = true;
51
	}
52
53
54
	/**
55
	 * This initializes the plugin.
56
	 *
57
	 * @param \Sabre\DAV\Server $server Sabre server
58
	 *
59
	 * @return void
60
	 * @throws MethodNotAllowed
61
	 */
62
	public function initialize(\Sabre\DAV\Server $server) {
63
		$server->on('beforeMethod', [$this, 'beforeMethod'], 999);
64
		$this->enabled = false;
65
	}
66
67
	public function beforeMethod(RequestInterface $request, ResponseInterface $response){
68
69
		if (!$this->enabled) {
70
			return;
71
		}
72
73
		if ($request->getMethod() !== 'PUT') {
74
			throw new MethodNotAllowed('Only PUT is allowed on files drop');
75
		}
76
77
		$path = explode('/', $request->getPath());
78
		$path = array_pop($path);
79
80
		$newName = \OC_Helper::buildNotExistingFileNameForView('/', $path, $this->view);
81
		$url = $request->getBaseUrl() . $newName;
82
		$request->setUrl($url);
83
	}
84
}
85