Completed
Push — master ( 81153d...2a6722 )
by Thomas
52s
created

PreviewPlugin::httpGet()   B

Complexity

Conditions 9
Paths 11

Size

Total Lines 56
Code Lines 31

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 9
eloc 31
nc 11
nop 2
dl 0
loc 56
rs 7.1584
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * @author Thomas Müller <[email protected]>
4
 *
5
 * @copyright Copyright (c) 2017, ownCloud GmbH
6
 * @license AGPL-3.0
7
 *
8
 * This code is free software: you can redistribute it and/or modify
9
 * it under the terms of the GNU Affero General Public License, version 3,
10
 * as published by the Free Software Foundation.
11
 *
12
 * This program is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
 * GNU Affero General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Affero General Public License, version 3,
18
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
19
 *
20
 */
21
22
namespace OCA\DAV\Files;
23
24
use OCP\Files\IPreviewNode;
25
use OCP\ILogger;
26
use Sabre\DAV\Exception\NotFound;
27
use Sabre\DAV\Server;
28
use Sabre\DAV\ServerPlugin;
29
use Sabre\HTTP\RequestInterface;
30
use Sabre\HTTP\ResponseInterface;
31
32
class PreviewPlugin extends ServerPlugin {
33
34
	/** @var Server */
35
	protected $server;
36
	/** @var ILogger */
37
	private $logger;
38
39
	public function __construct(ILogger $logger) {
40
		$this->logger = $logger;
41
	}
42
43
	/**
44
	 * Initializes the plugin and registers event handlers
45
	 *
46
	 * @param Server $server
47
	 * @return void
48
	 */
49
	function initialize(Server $server) {
50
51
		$this->server = $server;
52
		$this->server->on('method:GET', [$this, 'httpGet'], 90);
53
	}
54
55
	/**
56
	 * Intercepts GET requests on node urls ending with ?preview.
57
	 * The node has to implement IPreviewNode
58
	 *
59
	 * @param RequestInterface $request
60
	 * @param ResponseInterface $response
61
	 * @return bool
62
	 * @throws NotFound
63
	 * @throws \Sabre\DAVACL\Exception\NeedPrivileges
64
	 * @throws \Sabre\DAV\Exception\NotAuthenticated
65
	 */
66
	function httpGet(RequestInterface $request, ResponseInterface $response) {
67
68
		$queryParams = $request->getQueryParameters();
69
		if (!array_key_exists('preview', $queryParams)) {
70
			return true;
71
		}
72
73
		$path = $request->getPath();
74
		$node = $this->server->tree->getNodeForPath($path);
75
76
		if (!$node instanceof IFileNode) {
77
			return false;
78
		}
79
		$fileNode = $node->getNode();
80
		if (!$fileNode instanceof IPreviewNode) {
81
			return false;
82
		}
83
84
		// Checking ACL, if available.
85
		if ($aclPlugin = $this->server->getPlugin('acl')) {
86
			/** @var \Sabre\DAVACL\Plugin $aclPlugin */
87
			$aclPlugin->checkPrivileges($path, '{DAV:}read');
88
		}
89
90
		if ($image = $fileNode->getThumbnail($queryParams)) {
91
			if ($image === null || !$image->valid()) {
92
				throw new NotFound();
93
			}
94
			$type = $image->mimeType();
95
			if (!in_array($type, ['image/png', 'image/jpeg', 'image/gif'])) {
96
				$type = 'application/octet-stream';
97
			}
98
99
			// Enable output buffering
100
			ob_start();
101
			// Capture the output
102
			$image->show();
103
			$imageData = ob_get_contents();
104
			// Clear the output buffer
105
			ob_end_clean();
106
107
			$response->setHeader('Content-Type', $type);
108
			$response->setHeader('Content-Disposition', 'attachment');
109
			// cache 24h
110
			$response->setHeader('Cache-Control', 'max-age=86400, must-revalidate');
111
			$response->setHeader('Expires', gmdate ("D, d M Y H:i:s", time() + 86400) . " GMT");
112
113
			$response->setStatus(200);
114
			$response->setBody($imageData);
115
116
			// Returning false to break the event chain
117
			return false;
118
		}
119
		// TODO: add forceIcon handling .... if still needed
120
		throw new NotFound();
121
	}
122
}
123