Passed
Push — master ( a16703...c6645c )
by John
17:09 queued 13s
created

TrashbinPlugin   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 89
Duplicated Lines 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
eloc 39
dl 0
loc 89
rs 10
c 1
b 1
f 0
wmc 6

4 Methods

Rating   Name   Duplication   Size   Complexity  
A propFind() 0 49 2
A initialize() 0 5 1
A httpGet() 0 5 2
A __construct() 0 4 1
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * @copyright 2018, Roeland Jago Douma <[email protected]>
7
 *
8
 * @author Christoph Wurst <[email protected]>
9
 * @author Robin Appelman <[email protected]>
10
 * @author Roeland Jago Douma <[email protected]>
11
 *
12
 * @license GNU AGPL version 3 or any later version
13
 *
14
 * This program is free software: you can redistribute it and/or modify
15
 * it under the terms of the GNU Affero General Public License as
16
 * published by the Free Software Foundation, either version 3 of the
17
 * License, or (at your option) any later version.
18
 *
19
 * This program is distributed in the hope that it will be useful,
20
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22
 * GNU Affero General Public License for more details.
23
 *
24
 * You should have received a copy of the GNU Affero General Public License
25
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
26
 *
27
 */
28
namespace OCA\Files_Trashbin\Sabre;
29
30
use OCP\IPreview;
31
use Sabre\DAV\INode;
32
use Sabre\DAV\Server;
33
use Sabre\DAV\PropFind;
34
use Sabre\DAV\ServerPlugin;
35
use Sabre\HTTP\RequestInterface;
36
use Sabre\HTTP\ResponseInterface;
37
use OCA\DAV\Connector\Sabre\FilesPlugin;
38
39
class TrashbinPlugin extends ServerPlugin {
40
	public const TRASHBIN_FILENAME = '{http://nextcloud.org/ns}trashbin-filename';
41
	public const TRASHBIN_ORIGINAL_LOCATION = '{http://nextcloud.org/ns}trashbin-original-location';
42
	public const TRASHBIN_DELETION_TIME = '{http://nextcloud.org/ns}trashbin-deletion-time';
43
	public const TRASHBIN_TITLE = '{http://nextcloud.org/ns}trashbin-title';
44
45
	/** @var Server */
46
	private $server;
47
48
	/** @var IPreview */
49
	private $previewManager;
50
51
	public function __construct(
52
		IPreview $previewManager
53
	) {
54
		$this->previewManager = $previewManager;
55
	}
56
57
	public function initialize(Server $server) {
58
		$this->server = $server;
59
60
		$this->server->on('propFind', [$this, 'propFind']);
61
		$this->server->on('afterMethod:GET', [$this,'httpGet']);
62
	}
63
64
65
	public function propFind(PropFind $propFind, INode $node) {
66
		if (!($node instanceof ITrash)) {
67
			return;
68
		}
69
70
		$propFind->handle(self::TRASHBIN_FILENAME, function () use ($node) {
71
			return $node->getFilename();
72
		});
73
74
		$propFind->handle(self::TRASHBIN_ORIGINAL_LOCATION, function () use ($node) {
75
			return $node->getOriginalLocation();
76
		});
77
78
		$propFind->handle(self::TRASHBIN_TITLE, function () use ($node) {
79
			return $node->getTitle();
80
		});
81
82
		$propFind->handle(self::TRASHBIN_DELETION_TIME, function () use ($node) {
83
			return $node->getDeletionTime();
84
		});
85
86
		$propFind->handle(FilesPlugin::SIZE_PROPERTYNAME, function () use ($node) {
87
			return $node->getSize();
88
		});
89
90
		$propFind->handle(FilesPlugin::FILEID_PROPERTYNAME, function () use ($node) {
91
			return $node->getFileId();
92
		});
93
94
		$propFind->handle(FilesPlugin::PERMISSIONS_PROPERTYNAME, function () {
95
			return 'GD'; // read + delete
96
		});
97
98
		$propFind->handle(FilesPlugin::GETETAG_PROPERTYNAME, function () use ($node) {
99
			// add fake etag, it is only needed to identify the preview image
100
			return $node->getLastModified();
101
		});
102
103
		$propFind->handle(FilesPlugin::INTERNAL_FILEID_PROPERTYNAME, function () use ($node) {
104
			// add fake etag, it is only needed to identify the preview image
105
			return $node->getFileId();
106
		});
107
108
		$propFind->handle(FilesPlugin::HAS_PREVIEW_PROPERTYNAME, function () use ($node) {
109
			return $this->previewManager->isAvailable($node->getFileInfo());
110
		});
111
112
		$propFind->handle(FilesPlugin::MOUNT_TYPE_PROPERTYNAME, function () {
113
			return '';
114
		});
115
	}
116
117
	/**
118
	 * Set real filename on trashbin download
119
	 *
120
	 * @param RequestInterface $request
121
	 * @param ResponseInterface $response
122
	 */
123
	public function httpGet(RequestInterface $request, ResponseInterface $response): void {
124
		$path = $request->getPath();
125
		$node = $this->server->tree->getNodeForPath($path);
126
		if ($node instanceof ITrash) {
127
			$response->addHeader('Content-Disposition', 'attachment; filename="' . $node->getFilename() . '"');
128
		}
129
	}
130
}
131