Completed
Push — master ( 8bf574...c0bbae )
by Lukas
28:01 queued 15:22
created

PublicPreviewController::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 7
nc 1
nop 4
dl 0
loc 9
rs 9.6666
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 View Code Duplication
		if ($t === '' || $x === 0 || $y === 0) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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
		}
101
	}
102
}
103