Passed
Push — master ( 7536ca...401126 )
by
unknown
13:23
created

Plugin::afterGet()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 28
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 18
c 1
b 0
f 0
nc 5
nop 2
dl 0
loc 28
rs 9.3554
1
<?php
2
declare(strict_types=1);
3
/**
4
 * @copyright Copyright (c) 2019, Roeland Jago Douma <[email protected]>
5
 *
6
 * @author Roeland Jago Douma <[email protected]>
7
 *
8
 * @license GNU AGPL version 3 or any later version
9
 *
10
 * This program is free software: you can redistribute it and/or modify
11
 * it under the terms of the GNU Affero General Public License as
12
 * published by the Free Software Foundation, either version 3 of the
13
 * License, or (at your option) any later version.
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
21
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
22
 *
23
 */
24
25
namespace OCA\Files_Versions\Sabre;
26
27
use OC\AppFramework\Http\Request;
28
use OCP\IRequest;
29
use Sabre\DAV\Exception\NotFound;
30
use Sabre\DAV\Server;
31
use Sabre\DAV\ServerPlugin;
32
use Sabre\HTTP\RequestInterface;
33
use Sabre\HTTP\ResponseInterface;
34
35
class Plugin extends ServerPlugin {
36
37
	/** @var Server */
38
	private $server;
39
	/** @var IRequest */
40
	private $request;
41
42
	function __construct(IRequest $request) {
43
		$this->request = $request;
44
	}
45
46
	function initialize(Server $server) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
47
		$this->server = $server;
48
49
		$server->on('afterMethod:GET', [$this, 'afterGet']);
50
	}
51
52
	public function afterGet(RequestInterface $request, ResponseInterface $response) {
53
		$path = $request->getPath();
54
		if (strpos($path, 'versions') !== 0) {
55
			return;
56
		}
57
58
		try {
59
			$node = $this->server->tree->getNodeForPath($path);
60
		} catch (NotFound $e) {
61
			return;
62
		}
63
64
		if (!($node instanceof VersionFile)) {
65
			return;
66
		}
67
68
		$filename = $node->getVersion()->getSourceFileName();
69
70
		if ($this->request->isUserAgent(
71
			[
72
				Request::USER_AGENT_IE,
73
				Request::USER_AGENT_ANDROID_MOBILE_CHROME,
74
				Request::USER_AGENT_FREEBOX,
75
			])) {
76
			$response->addHeader('Content-Disposition', 'attachment; filename="' . rawurlencode($filename) . '"');
77
		} else {
78
			$response->addHeader('Content-Disposition', 'attachment; filename*=UTF-8\'\'' . rawurlencode($filename)
79
				. '; filename="' . rawurlencode($filename) . '"');
80
		}
81
	}
82
83
}
84