Completed
Push — master ( ec6e3a...a32d5d )
by Thomas
10:38
created

ZsyncPlugin::handleGetProperties()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 9
nc 4
nop 2
dl 0
loc 13
rs 9.2
c 0
b 0
f 0
1
<?php
2
/*
3
 * Copyright (C) by Ahmed Ammar <[email protected]>
4
 *
5
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
6
 * documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
7
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
8
 * permit persons to whom the Software is furnished to do so, subject to the following conditions:
9
 *
10
 * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
11
 * Software.
12
 *
13
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
14
 * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
15
 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
16
 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
17
 *
18
 */
19
namespace OCA\DAV\Files;
20
21
use OC\AppFramework\Http;
22
use OCP\Files\NotFoundException;
23
use OCP\Files\NotPermittedException;
24
use Sabre\DAV\Server;
25
use Sabre\DAV\ServerPlugin;
26
use Sabre\DAV\PropFind;
27
use Sabre\HTTP\RequestInterface;
28
use Sabre\HTTP\ResponseInterface;
29
use OC\Files\View;
30
use \Exception;
31
32
class ZsyncPlugin extends ServerPlugin {
33
34
	// namespace
35
	const ZSYNC_PROPERTYNAME = '{http://owncloud.org/ns}zsync';
36
37
	/** @var OC\Files\View */
38
	private $view;
39
40
	public function __construct(View $view) {
41
		$this->view = $view;
0 ignored issues
show
Documentation Bug introduced by
It seems like $view of type object<OC\Files\View> is incompatible with the declared type object<OCA\DAV\Files\OC\Files\View> of property $view.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
42
		$this->view->mkdir('files_zsync');
43
	}
44
45
	/**
46
	 * Initializes the plugin and registers event handlers
47
	 *
48
	 * @param Server $server
49
	 * @return void
50
	 */
51
	function initialize(Server $server) {
52
		$server->on('method:GET', [$this, 'httpGet'], 90);
53
		$server->on('method:DELETE', [$this, 'httpDelete'], 90);
54
		$server->on('propFind', [$this, 'handleGetProperties']);
55
	}
56
57
	/**
58
	 * Intercepts GET requests on file urls ending with ?zsync.
59
	 *
60
	 * @param RequestInterface $request
61
	 * @param ResponseInterface $response
62
	 */
63
	function httpGet(RequestInterface $request, ResponseInterface $response) {
64
65
		$queryParams = $request->getQueryParameters();
66
		if (!array_key_exists('zsync', $queryParams)) {
67
			return true;
68
		}
69
70
		$path = ltrim($request->getPath(), '/');
71
		/* remove files/$user */
72
		$path = implode('/', array_slice(explode('/', $path), 2));
73
		/* If basefile not found this is an error */
74
		if (!$this->view->file_exists('files/'.$path)) {
75
			$response->setStatus(Http::STATUS_NOT_FOUND);
76
			return false;
77
		}
78
79
		$info = $this->view->getFileInfo('files/'.$path);
80
		$zsyncMetadataFile = 'files_zsync/'.$info->getId();
81
		if ($this->view->file_exists($zsyncMetadataFile)) {
82
			$content = $this->view->file_get_contents($zsyncMetadataFile);
83
			$response->setHeader('OC-ETag', $info->getEtag());
84
			$response->setStatus(Http::STATUS_OK);
85
			$response->setBody($content);
86
		} else {
87
			$response->setStatus(Http::STATUS_NOT_FOUND);
88
		}
89
90
		return false;
91
	}
92
93
	/**
94
	 * Intercepts DELETE requests on file urls ending with ?zsync.
95
	 *
96
	 * @param RequestInterface $request
97
	 * @param ResponseInterface $response
98
	 */
99
	function httpDelete(RequestInterface $request, ResponseInterface $response) {
100
101
		$queryParams = $request->getQueryParameters();
102
		if (!array_key_exists('zsync', $queryParams)) {
103
			return true;
104
		}
105
106
		$path = ltrim($request->getPath(), '/');
107
		/* remove files/$user */
108
		$path = implode('/', array_slice(explode('/', $path), 2));
109
		/* If basefile not found this is an error */
110
		if (!$this->view->file_exists('files/'.$path)) {
111
			$response->setStatus(Http::STATUS_NOT_FOUND);
112
			return false;
113
		}
114
115
		$info = $this->view->getFileInfo('files/'.$path);
116
		$zsyncMetadataFile = 'files_zsync/'.$info->getId();
117
		if ($this->view->file_exists($zsyncMetadataFile)) {
118
			$this->view->unlink($zsyncMetadataFile);
119
			$response->setStatus(Http::STATUS_OK);
120
		} else {
121
			$response->setStatus(Http::STATUS_NOT_FOUND);
122
		}
123
124
		return false;
125
	}
126
127
	/**
128
	 * Adds zsync property if metadata exists
129
	 *
130
	 * @param PropFind $propFind
131
	 * @param \Sabre\DAV\INode $node
132
	 * @return void
133
	 */
134
	public function handleGetProperties(PropFind $propFind, \Sabre\DAV\INode $node) {
135
		if ($node instanceof \OCA\DAV\Connector\Sabre\File) {
136
			if (!$this->view->is_file('files/'.$node->getPath()))
137
				return;
138
			$info = $this->view->getFileInfo('files/'.$node->getPath());
139
			$zsyncMetadataFile = 'files_zsync/'.$info->getId();
140
			if ($this->view->file_exists($zsyncMetadataFile)) {
141
				$propFind->handle(self::ZSYNC_PROPERTYNAME, function() use ($node) {
142
					return 'true';
143
				});
144
			}
145
		}
146
	}
147
}
148