TemplatesController::fetchPreview()   B
last analyzed

Complexity

Conditions 7
Paths 9

Size

Total Lines 27

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 56

Importance

Changes 0
Metric Value
dl 0
loc 27
ccs 0
cts 24
cp 0
rs 8.5546
c 0
b 0
f 0
cc 7
nc 9
nop 6
crap 56
1
<?php
2
declare(strict_types=1);
3
/**
4
 * @copyright Copyright (c) 2018 John Molakvoæ <[email protected]>
5
 *
6
 * @author John Molakvoæ <[email protected]>
7
 *
8
 * @license GNU AGPL version 3 or any later version
9
 *
10
 * This program is free software: you can redistribute it and/or modify
11
 * it under the terms of the GNU Affero General Public License as
12
 * published by the Free Software Foundation, either version 3 of the
13
 * License, or (at your option) any later version.
14
 *
15
 * This program is distributed in the hope that it will be useful,
16
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
 * GNU Affero General Public License for more details.
19
 *
20
 * You should have received a copy of the GNU Affero General Public License
21
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
22
 *
23
 */
24
25
namespace OCA\Richdocuments\Controller;
26
27
use OCA\Richdocuments\TemplateManager;
28
use OCP\AppFramework\Controller;
29
use OCP\AppFramework\Http;
30
use OCP\AppFramework\Http\DataResponse;
31
use OCP\AppFramework\Http\FileDisplayResponse;
32
use OCP\AppFramework\Http\JSONResponse;
33
use OCP\AppFramework\Http\NotFoundResponse;
34
use OCP\Files\Node;
35
use OCP\Files\NotFoundException;
36
use OCP\Files\SimpleFS\ISimpleFile;
37
use OCP\IL10N;
38
use OCP\IPreview;
39
use OCP\IRequest;
40
use OC\Files\Filesystem;
41
42
class TemplatesController extends Controller {
43
	/** @var IL10N */
44
	private $l10n;
45
46
	/** @var TemplateManager */
47
	private $manager;
48
49
	/** @var IPreview */
50
	private $preview;
51
52
	/** @var int Max template size */
53
	private $maxSize = 20 * 1024 * 1024;
54
55
	/**
56
	 * Templates controller
57
	 *
58
	 * @param string $appName
59
	 * @param IRequest $request
60
	 * @param L10N $l10n
61
	 * @param TemplateManager $manager
62
	 * @param IPreview $preview
63
	 */
64
	public function __construct($appName,
65
								IRequest $request,
66
								IL10N $l10n,
67
								TemplateManager $manager,
68
								IPreview $preview) {
69
		parent::__construct($appName, $request);
70
71
		$this->appName = $appName;
72
		$this->request = $request;
73
		$this->l10n    = $l10n;
74
		$this->manager = $manager;
75
		$this->preview = $preview;
76
	}
77
78
	/**
79
	 * @NoAdminRequired
80
	 * @NoCSRFRequired
81
	 *
82
	 * Get preview for a specific template
83
	 *
84
	 * @param int $fileId The template id
85
	 * @param int $x
86
	 * @param int $y
87
	 * @param bool $a
88
	 * @param bool $forceIcon
89
	 * @param string $mode
90
	 * @return DataResponse
91
	 * @throws NotFoundResponse
92
	 */
93
	public function getPreview($fileId,
94
		$x = 150,
95
		$y = 150,
96
		$a = false,
97
		$forceIcon = true,
98
		$mode = 'fill') {
99
100
		if ($fileId === '' || $x === 0 || $y === 0) {
0 ignored issues
show
Unused Code Bug introduced by
The strict comparison === seems to always evaluate to false as the types of $fileId (integer) and '' (string) can never be identical. Maybe you want to use a loose comparison == instead?
Loading history...
101
			return new DataResponse([], Http::STATUS_BAD_REQUEST);
102
		}
103
104
		try {
105
			$template = $this->manager->get($fileId);
106
		} catch (NotFoundException $e) {
107
			return new DataResponse([], Http::STATUS_NOT_FOUND);
108
		}
109
110
		if ($template instanceof ISimpleFile) {
111
			return new DataResponse([], Http::STATUS_NOT_FOUND);
112
		}
113
114
		return $this->fetchPreview($template, $x, $y, $a, $forceIcon, $mode);
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->fetchPreview($tem...$a, $forceIcon, $mode); of type OCP\AppFramework\Http\Da...ttp\FileDisplayResponse adds the type OCP\AppFramework\Http\FileDisplayResponse to the return on line 114 which is incompatible with the return type documented by OCA\Richdocuments\Contro...sController::getPreview of type OCP\AppFramework\Http\DataResponse.
Loading history...
115
	}
116
117
	/**
118
	 * Add a global template
119
	 *
120
	 * @return JSONResponse
121
	 */
122
	public function add() {
123
		$files = $this->request->getUploadedFile('files');
124
125
		if (!is_null($files)) {
126
			if ($files['error'][0] === 0
127
				&& is_uploaded_file($files['tmp_name'][0])
128
				&& !Filesystem::isFileBlacklisted($files['tmp_name'][0])) {
129
130
				// TODO: ensure the size limit is decent for preview
131 View Code Duplication
				if ($files['size'][0] > $this->maxSize) {
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...
132
					return new JSONResponse(
133
						['data' => ['message' => $this->l10n->t('File is too big')]],
134
						Http::STATUS_BAD_REQUEST
135
					);
136
				}
137
138 View Code Duplication
				if (!$this->manager->isValidTemplateMime($files['type'][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...
139
					return new JSONResponse(
140
						['data' => ['message' => $this->l10n->t('Only template files can be uploaded')]],
141
						Http::STATUS_BAD_REQUEST
142
					);
143
				}
144
145
				$templateName = $files['name'][0];
146
				$templateFile = file_get_contents($files['tmp_name'][0]);
147
148
				unlink($files['tmp_name'][0]);
149
150
				$template = $this->manager->add($templateName, $templateFile);
151
152
				return new JSONResponse(
153
					['data' => $template],
154
					Http::STATUS_CREATED
155
				);
156
			}
157
		}
158
159
		return new JSONResponse(
160
			['data' => ['message' => $this->l10n->t('Invalid file provided')]],
161
			Http::STATUS_BAD_REQUEST
162
		);
163
	}
164
165
	/**
166
	 * Delete a global template
167
	 *
168
	 * @param int $fileId
169
	 * @return JSONResponse
170
	 */
171
	public function delete($fileId) {
172
		try {
173
			$this->manager->delete($fileId);
174
175
			return new JSONResponse(
176
				['data' => ['status' => 'success']],
177
				Http::STATUS_NO_CONTENT
178
			);
179
		} catch (NotFoundException $e) {
180
			return new JSONResponse(
181
				['data' => ['message' => $this->l10n->t('Template not found')]],
182
				Http::STATUS_NOT_FOUND
183
			);
184
		}
185
	}
186
187
	/**
188
	 * @param Node $node
189
	 * @param int $x
190
	 * @param int $y
191
	 * @param bool $a
192
	 * @param bool $forceIcon
193
	 * @param string $mode
194
	 * @return DataResponse|FileDisplayResponse
195
	 */
196
	private function fetchPreview(
197
		Node $node,
198
		$x,
199
		$y,
200
		$a = false,
201
		$forceIcon = true,
202
		string $mode): Http\Response {
203
204
		if (!($node instanceof Node) || (!$forceIcon && !$this->preview->isAvailable($node))) {
205
			return new DataResponse([], Http::STATUS_NOT_FOUND);
206
		}
207
		if (!$node->isReadable()) {
208
			return new DataResponse([], Http::STATUS_FORBIDDEN);
209
		}
210
211
		try {
212
			$f        = $this->preview->getPreview($node, $x, $y, !$a, $mode);
0 ignored issues
show
Compatibility introduced by
$node 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...
213
			$response = new FileDisplayResponse($f, Http::STATUS_OK, ['Content-Type' => $f->getMimeType()]);
214
			$response->cacheFor(3600 * 24);
215
216
			return $response;
217
		} catch (NotFoundException $e) {
218
			return new DataResponse([], Http::STATUS_NOT_FOUND);
219
		} catch (\InvalidArgumentException $e) {
220
			return new DataResponse([], Http::STATUS_BAD_REQUEST);
221
		}
222
	}
223
}
224