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

ApiController   A

Complexity

Total Complexity 23

Size/Duplication

Total Lines 215
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 15

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 215
rs 9.1666
wmc 23
lcom 2
cbo 15

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 16 1
B getThumbnail() 0 21 6
B updateFileTags() 0 23 5
A formatNodes() 0 17 3
A getRecentFiles() 0 5 1
B getShareTypes() 0 25 3
A updateFileSorting() 0 12 3
A showHiddenFiles() 0 4 1
1
<?php
2
/**
3
 * @copyright Copyright (c) 2016, ownCloud, Inc.
4
 *
5
 * @author Christoph Wurst <[email protected]>
6
 * @author Joas Schilling <[email protected]>
7
 * @author Lukas Reschke <[email protected]>
8
 * @author Morris Jobke <[email protected]>
9
 * @author Roeland Jago Douma <[email protected]>
10
 * @author Thomas Müller <[email protected]>
11
 * @author Tobias Kaminsky <[email protected]>
12
 * @author Vincent Petry <[email protected]>
13
 *
14
 * @license AGPL-3.0
15
 *
16
 * This code is free software: you can redistribute it and/or modify
17
 * it under the terms of the GNU Affero General Public License, version 3,
18
 * as published by the Free Software Foundation.
19
 *
20
 * This program is distributed in the hope that it will be useful,
21
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23
 * GNU Affero General Public License for more details.
24
 *
25
 * You should have received a copy of the GNU Affero General Public License, version 3,
26
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
27
 *
28
 */
29
30
namespace OCA\Files\Controller;
31
32
use OCP\AppFramework\Http;
33
use OCP\AppFramework\Controller;
34
use OCP\Files\File;
35
use OCP\Files\Folder;
36
use OCP\Files\NotFoundException;
37
use OCP\IConfig;
38
use OCP\IRequest;
39
use OCP\AppFramework\Http\DataResponse;
40
use OCP\AppFramework\Http\FileDisplayResponse;
41
use OCP\AppFramework\Http\Response;
42
use OCA\Files\Service\TagService;
43
use OCP\IPreview;
44
use OCP\Share\IManager;
45
use OC\Files\Node\Node;
46
use OCP\IUserSession;
47
48
/**
49
 * Class ApiController
50
 *
51
 * @package OCA\Files\Controller
52
 */
53
class ApiController extends Controller {
54
	/** @var TagService */
55
	private $tagService;
56
	/** @var IManager **/
57
	private $shareManager;
58
	/** @var IPreview */
59
	private $previewManager;
60
	/** IUserSession */
61
	private $userSession;
62
	/** IConfig */
63
	private $config;
64
	/** @var Folder */
65
	private $userFolder;
66
67
	/**
68
	 * @param string $appName
69
	 * @param IRequest $request
70
	 * @param IUserSession $userSession
71
	 * @param TagService $tagService
72
	 * @param IPreview $previewManager
73
	 * @param IManager $shareManager
74
	 * @param IConfig $config
75
	 * @param Folder $userFolder
76
	 */
77
	public function __construct($appName,
78
								IRequest $request,
79
								IUserSession $userSession,
80
								TagService $tagService,
81
								IPreview $previewManager,
82
								IManager $shareManager,
83
								IConfig $config,
84
								Folder $userFolder) {
85
		parent::__construct($appName, $request);
86
		$this->userSession = $userSession;
87
		$this->tagService = $tagService;
88
		$this->previewManager = $previewManager;
89
		$this->shareManager = $shareManager;
90
		$this->config = $config;
91
		$this->userFolder = $userFolder;
92
	}
93
94
	/**
95
	 * Gets a thumbnail of the specified file
96
	 *
97
	 * @since API version 1.0
98
	 *
99
	 * @NoAdminRequired
100
	 * @NoCSRFRequired
101
	 * @StrictCookieRequired
102
	 *
103
	 * @param int $x
104
	 * @param int $y
105
	 * @param string $file URL-encoded filename
106
	 * @return DataResponse|FileDisplayResponse
107
	 */
108
	public function getThumbnail($x, $y, $file) {
109
		if($x < 1 || $y < 1) {
110
			return new DataResponse(['message' => 'Requested size must be numeric and a positive value.'], Http::STATUS_BAD_REQUEST);
111
		}
112
113
		try {
114
			$file = $this->userFolder->get($file);
115
			if ($file instanceof Folder) {
116
				throw new NotFoundException();
117
			}
118
119
			/** @var File $file */
120
			$preview = $this->previewManager->getPreview($file, $x, $y, true);
121
122
			return new FileDisplayResponse($preview, Http::STATUS_OK, ['Content-Type' => $preview->getMimeType()]);
123
		} catch (NotFoundException $e) {
124
			return new DataResponse(['message' => 'File not found.'], Http::STATUS_NOT_FOUND);
125
		} catch (\Exception $e) {
126
			return new DataResponse([], Http::STATUS_BAD_REQUEST);
127
		}
128
	}
129
130
	/**
131
	 * Updates the info of the specified file path
132
	 * The passed tags are absolute, which means they will
133
	 * replace the actual tag selection.
134
	 *
135
	 * @NoAdminRequired
136
	 *
137
	 * @param string $path path
138
	 * @param array|string $tags array of tags
139
	 * @return DataResponse
140
	 */
141
	public function updateFileTags($path, $tags = null) {
142
		$result = [];
143
		// if tags specified or empty array, update tags
144
		if (!is_null($tags)) {
145
			try {
146
				$this->tagService->updateFileTags($path, $tags);
0 ignored issues
show
Bug introduced by
It seems like $tags defined by parameter $tags on line 141 can also be of type string; however, OCA\Files\Service\TagService::updateFileTags() does only seem to accept array, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
147
			} catch (\OCP\Files\NotFoundException $e) {
148
				return new DataResponse([
149
					'message' => $e->getMessage()
150
				], Http::STATUS_NOT_FOUND);
151
			} catch (\OCP\Files\StorageNotAvailableException $e) {
152
				return new DataResponse([
153
					'message' => $e->getMessage()
154
				], Http::STATUS_SERVICE_UNAVAILABLE);
155
			} catch (\Exception $e) {
156
				return new DataResponse([
157
					'message' => $e->getMessage()
158
				], Http::STATUS_NOT_FOUND);
159
			}
160
			$result['tags'] = $tags;
161
		}
162
		return new DataResponse($result);
163
	}
164
165
	/**
166
	 * @param \OCP\Files\Node[] $nodes
167
	 * @return array
168
	 */
169
	private function formatNodes(array $nodes) {
170
		return array_values(array_map(function (Node $node) {
171
			/** @var \OC\Files\Node\Node $shareTypes */
172
			$shareTypes = $this->getShareTypes($node);
173
			$file = \OCA\Files\Helper::formatFileInfo($node->getFileInfo());
174
			$parts = explode('/', dirname($node->getPath()), 4);
175
			if (isset($parts[3])) {
176
				$file['path'] = '/' . $parts[3];
177
			} else {
178
				$file['path'] = '/';
179
			}
180
			if (!empty($shareTypes)) {
181
				$file['shareTypes'] = $shareTypes;
182
			}
183
			return $file;
184
		}, $nodes));
185
	}
186
187
	/**
188
	 * Returns a list of recently modifed files.
189
	 *
190
	 * @NoAdminRequired
191
	 *
192
	 * @return DataResponse
193
	 */
194
	public function getRecentFiles() {
195
		$nodes = $this->userFolder->getRecent(100);
196
		$files = $this->formatNodes($nodes);
197
		return new DataResponse(['files' => $files]);
198
	}
199
200
	/**
201
	 * Return a list of share types for outgoing shares
202
	 *
203
	 * @param Node $node file node
204
	 *
205
	 * @return int[] array of share types
206
	 */
207
	private function getShareTypes(Node $node) {
208
		$userId = $this->userSession->getUser()->getUID();
209
		$shareTypes = [];
210
		$requestedShareTypes = [
211
			\OCP\Share::SHARE_TYPE_USER,
212
			\OCP\Share::SHARE_TYPE_GROUP,
213
			\OCP\Share::SHARE_TYPE_LINK,
214
			\OCP\Share::SHARE_TYPE_REMOTE,
215
			\OCP\Share::SHARE_TYPE_EMAIL
216
		];
217
		foreach ($requestedShareTypes as $requestedShareType) {
218
			// one of each type is enough to find out about the types
219
			$shares = $this->shareManager->getSharesBy(
220
				$userId,
221
				$requestedShareType,
222
				$node,
223
				false,
224
				1
225
			);
226
			if (!empty($shares)) {
227
				$shareTypes[] = $requestedShareType;
228
			}
229
		}
230
		return $shareTypes;
231
	}
232
233
	/**
234
	 * Change the default sort mode
235
	 *
236
	 * @NoAdminRequired
237
	 *
238
	 * @param string $mode
239
	 * @param string $direction
240
	 * @return Response
241
	 */
242
	public function updateFileSorting($mode, $direction) {
243
		$allowedMode = ['name', 'size', 'mtime'];
244
		$allowedDirection = ['asc', 'desc'];
245
		if (!in_array($mode, $allowedMode) || !in_array($direction, $allowedDirection)) {
246
			$response = new Response();
247
			$response->setStatus(Http::STATUS_UNPROCESSABLE_ENTITY);
248
			return $response;
249
		}
250
		$this->config->setUserValue($this->userSession->getUser()->getUID(), 'files', 'file_sorting', $mode);
251
		$this->config->setUserValue($this->userSession->getUser()->getUID(), 'files', 'file_sorting_direction', $direction);
252
		return new Response();
253
	}
254
255
	/**
256
	 * Toggle default for showing/hiding hidden files
257
	 *
258
	 * @NoAdminRequired
259
	 *
260
	 * @param bool $show
261
	 */
262
	public function showHiddenFiles($show) {
263
		$this->config->setUserValue($this->userSession->getUser()->getUID(), 'files', 'show_hidden', (int) $show);
264
		return new Response();
265
	}
266
267
}
268