Completed
Push — master ( 72889e...5f25dd )
by Morris
25:38 queued 13:04
created

PublicPreviewController::directLink()   C

Complexity

Conditions 8
Paths 14

Size

Total Lines 38
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 8
eloc 21
nc 14
nop 1
dl 0
loc 38
rs 5.3846
c 0
b 0
f 0
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\Files_Sharing\Controller;
24
25
use OCP\AppFramework\Controller;
26
use OCP\AppFramework\Http;
27
use OCP\AppFramework\Http\DataResponse;
28
use OCP\AppFramework\Http\FileDisplayResponse;
29
use OCP\Constants;
30
use OCP\Files\Folder;
31
use OCP\Files\NotFoundException;
32
use OCP\IPreview;
33
use OCP\IRequest;
34
use OCP\Share\Exceptions\ShareNotFound;
35
use OCP\Share\IManager as ShareManager;
36
37
class PublicPreviewController extends Controller {
38
39
	/** @var ShareManager */
40
	private $shareManager;
41
42
	/** @var IPreview */
43
	private $previewManager;
44
45
	public function __construct($appName,
46
								IRequest $request,
47
								ShareManager $shareManger,
48
								IPreview $previewManager) {
49
		parent::__construct($appName, $request);
50
51
		$this->shareManager = $shareManger;
52
		$this->previewManager = $previewManager;
53
	}
54
55
	/**
56
	 * @PublicPage
57
	 * @NoCSRFRequired
58
	 *
59
	 * @param string $file
60
	 * @param int $x
61
	 * @param int $y
62
	 * @param string $t
63
	 * @param bool $a
64
	 * @return DataResponse|FileDisplayResponse
65
	 */
66
	public function getPreview(
67
		$file = '',
68
		$x = 32,
69
		$y = 32,
70
		$t = '',
71
		$a = false
72
	) {
73
74
		if ($t === '' || $x === 0 || $y === 0) {
75
			return new DataResponse([], Http::STATUS_BAD_REQUEST);
76
		}
77
78
		try {
79
			$share = $this->shareManager->getShareByToken($t);
80
		} catch (ShareNotFound $e) {
81
			return new DataResponse([], Http::STATUS_NOT_FOUND);
82
		}
83
84
		if (($share->getPermissions() & Constants::PERMISSION_READ) === 0) {
85
			return new DataResponse([], Http::STATUS_FORBIDDEN);
86
		}
87
88
		try {
89
			$node = $share->getNode();
90
			if ($node instanceof Folder) {
91
				$file = $node->get($file);
92
			} else {
93
				$file = $node;
94
			}
95
96
			$f = $this->previewManager->getPreview($file, $x, $y, !$a);
0 ignored issues
show
Compatibility introduced by
$file of type object<OCP\Files\Node> is not a sub-type of object<OCP\Files\File>. It seems like you assume a child interface of the interface OCP\Files\Node to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
97
			return new FileDisplayResponse($f, Http::STATUS_OK, ['Content-Type' => $f->getMimeType()]);
98
		} catch (NotFoundException $e) {
99
			return new DataResponse([], Http::STATUS_NOT_FOUND);
100
		} catch (\InvalidArgumentException $e) {
101
			return new DataResponse([], Http::STATUS_BAD_REQUEST);
102
		}
103
	}
104
105
	/**
106
	 * @PublicPage
107
	 * @NoCSRFRequired
108
	 * @NoSameSiteCookieRequired
109
	 *
110
	 * @param $token
111
	 * @return DataResponse|FileDisplayResponse
112
	 */
113
	public function directLink($token) {
114
		// No token no image
115
		if ($token === '') {
116
			return new DataResponse([], Http::STATUS_BAD_REQUEST);
117
		}
118
119
		// No share no image
120
		try {
121
			$share = $this->shareManager->getShareByToken($token);
122
		} catch (ShareNotFound $e) {
123
			return new DataResponse([], Http::STATUS_NOT_FOUND);
124
		}
125
126
		// No permissions no image
127
		if (($share->getPermissions() & Constants::PERMISSION_READ) === 0) {
128
			return new DataResponse([], Http::STATUS_FORBIDDEN);
129
		}
130
131
		// Password protected shares have no direct link!
132
		if ($share->getPassword() !== null) {
133
			return new DataResponse([], Http::STATUS_FORBIDDEN);
134
		}
135
136
		try {
137
			$node = $share->getNode();
138
			if ($node instanceof Folder) {
139
				// Direct link only works for single files
140
				return new DataResponse([], Http::STATUS_BAD_REQUEST);
141
			}
142
143
			$f = $this->previewManager->getPreview($node, -1, -1, false);
144
			return new FileDisplayResponse($f, Http::STATUS_OK, ['Content-Type' => $f->getMimeType()]);
145
		} catch (NotFoundException $e) {
146
			return new DataResponse([], Http::STATUS_NOT_FOUND);
147
		} catch (\InvalidArgumentException $e) {
148
			return new DataResponse([], Http::STATUS_BAD_REQUEST);
149
		}
150
	}
151
}
152