Passed
Push — master ( 6f31d2...35b910 )
by Julius
34:16 queued 19:13
created

ApiController::getShareTypes()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 26
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 19
nc 3
nop 1
dl 0
loc 26
rs 9.6333
c 0
b 0
f 0
1
<?php
2
/**
3
 * @copyright Copyright (c) 2016, ownCloud, Inc.
4
 *
5
 * @author Christoph Wurst <[email protected]>
6
 * @author Daniel Kesselberg <[email protected]>
7
 * @author Felix Nüsse <[email protected]>
8
 * @author fnuesse <[email protected]>
9
 * @author fnuesse <[email protected]>
10
 * @author Joas Schilling <[email protected]>
11
 * @author John Molakvoæ (skjnldsv) <[email protected]>
12
 * @author Julius Härtl <[email protected]>
13
 * @author Lukas Reschke <[email protected]>
14
 * @author Max Kovalenko <[email protected]>
15
 * @author Morris Jobke <[email protected]>
16
 * @author Richard Steinmetz <[email protected]>
17
 * @author Robin Appelman <[email protected]>
18
 * @author Roeland Jago Douma <[email protected]>
19
 * @author Tobias Kaminsky <[email protected]>
20
 * @author Vincent Petry <[email protected]>
21
 *
22
 * @license AGPL-3.0
23
 *
24
 * This code is free software: you can redistribute it and/or modify
25
 * it under the terms of the GNU Affero General Public License, version 3,
26
 * as published by the Free Software Foundation.
27
 *
28
 * This program is distributed in the hope that it will be useful,
29
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
30
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
31
 * GNU Affero General Public License for more details.
32
 *
33
 * You should have received a copy of the GNU Affero General Public License, version 3,
34
 * along with this program. If not, see <http://www.gnu.org/licenses/>
35
 *
36
 */
37
38
namespace OCA\Files\Controller;
39
40
use OC\Files\Node\Node;
41
use OCA\Files\Service\TagService;
42
use OCP\AppFramework\Controller;
43
use OCP\AppFramework\Http;
44
use OCP\AppFramework\Http\DataResponse;
45
use OCP\AppFramework\Http\FileDisplayResponse;
46
use OCP\AppFramework\Http\JSONResponse;
47
use OCP\AppFramework\Http\Response;
48
use OCP\Files\File;
49
use OCP\Files\Folder;
50
use OCP\Files\NotFoundException;
51
use OCP\IConfig;
52
use OCP\IPreview;
53
use OCP\IRequest;
54
use OCP\IUserSession;
55
use OCP\Share\IManager;
56
use OCP\Share\IShare;
57
58
/**
59
 * Class ApiController
60
 *
61
 * @package OCA\Files\Controller
62
 */
63
class ApiController extends Controller {
64
	/** @var TagService */
65
	private $tagService;
66
	/** @var IManager * */
67
	private $shareManager;
68
	/** @var IPreview */
69
	private $previewManager;
70
	/** IUserSession */
71
	private $userSession;
72
	/** IConfig */
73
	private $config;
74
	/** @var Folder */
75
	private $userFolder;
76
77
	/**
78
	 * @param string $appName
79
	 * @param IRequest $request
80
	 * @param IUserSession $userSession
81
	 * @param TagService $tagService
82
	 * @param IPreview $previewManager
83
	 * @param IManager $shareManager
84
	 * @param IConfig $config
85
	 * @param Folder $userFolder
86
	 */
87
	public function __construct($appName,
88
								IRequest $request,
89
								IUserSession $userSession,
90
								TagService $tagService,
91
								IPreview $previewManager,
92
								IManager $shareManager,
93
								IConfig $config,
94
								Folder $userFolder) {
95
		parent::__construct($appName, $request);
96
		$this->userSession = $userSession;
97
		$this->tagService = $tagService;
98
		$this->previewManager = $previewManager;
99
		$this->shareManager = $shareManager;
100
		$this->config = $config;
101
		$this->userFolder = $userFolder;
102
	}
103
104
	/**
105
	 * Gets a thumbnail of the specified file
106
	 *
107
	 * @since API version 1.0
108
	 *
109
	 * @NoAdminRequired
110
	 * @NoCSRFRequired
111
	 * @StrictCookieRequired
112
	 *
113
	 * @param int $x
114
	 * @param int $y
115
	 * @param string $file URL-encoded filename
116
	 * @return DataResponse|FileDisplayResponse
117
	 */
118
	public function getThumbnail($x, $y, $file) {
119
		if ($x < 1 || $y < 1) {
120
			return new DataResponse(['message' => 'Requested size must be numeric and a positive value.'], Http::STATUS_BAD_REQUEST);
121
		}
122
123
		try {
124
			$file = $this->userFolder->get($file);
125
			if ($file instanceof Folder) {
126
				throw new NotFoundException();
127
			}
128
129
			/** @var File $file */
130
			$preview = $this->previewManager->getPreview($file, $x, $y, true);
131
132
			return new FileDisplayResponse($preview, Http::STATUS_OK, ['Content-Type' => $preview->getMimeType()]);
133
		} catch (NotFoundException $e) {
134
			return new DataResponse(['message' => 'File not found.'], Http::STATUS_NOT_FOUND);
135
		} catch (\Exception $e) {
136
			return new DataResponse([], Http::STATUS_BAD_REQUEST);
137
		}
138
	}
139
140
	/**
141
	 * Updates the info of the specified file path
142
	 * The passed tags are absolute, which means they will
143
	 * replace the actual tag selection.
144
	 *
145
	 * @NoAdminRequired
146
	 *
147
	 * @param string $path path
148
	 * @param array|string $tags array of tags
149
	 * @return DataResponse
150
	 */
151
	public function updateFileTags($path, $tags = null) {
152
		$result = [];
153
		// if tags specified or empty array, update tags
154
		if (!is_null($tags)) {
155
			try {
156
				$this->tagService->updateFileTags($path, $tags);
157
			} catch (\OCP\Files\NotFoundException $e) {
158
				return new DataResponse([
159
					'message' => $e->getMessage()
160
				], Http::STATUS_NOT_FOUND);
161
			} catch (\OCP\Files\StorageNotAvailableException $e) {
162
				return new DataResponse([
163
					'message' => $e->getMessage()
164
				], Http::STATUS_SERVICE_UNAVAILABLE);
165
			} catch (\Exception $e) {
166
				return new DataResponse([
167
					'message' => $e->getMessage()
168
				], Http::STATUS_NOT_FOUND);
169
			}
170
			$result['tags'] = $tags;
171
		}
172
		return new DataResponse($result);
173
	}
174
175
	/**
176
	 * @param \OCP\Files\Node[] $nodes
177
	 * @return array
178
	 */
179
	private function formatNodes(array $nodes) {
180
		$shareTypesForNodes = $this->getShareTypesForNodes($nodes);
181
		return array_values(array_map(function (Node $node) use ($shareTypesForNodes) {
182
			$shareTypes = $shareTypesForNodes[$node->getId()] ?? [];
183
			$file = \OCA\Files\Helper::formatFileInfo($node->getFileInfo());
184
			$file['hasPreview'] = $this->previewManager->isAvailable($node);
185
			$parts = explode('/', dirname($node->getPath()), 4);
186
			if (isset($parts[3])) {
187
				$file['path'] = '/' . $parts[3];
188
			} else {
189
				$file['path'] = '/';
190
			}
191
			if (!empty($shareTypes)) {
192
				$file['shareTypes'] = $shareTypes;
193
			}
194
			return $file;
195
		}, $nodes));
196
	}
197
198
	/**
199
	 * Get the share types for each node
200
	 *
201
	 * @param \OCP\Files\Node[] $nodes
202
	 * @return array<int, int[]> list of share types for each fileid
203
	 */
204
	private function getShareTypesForNodes(array $nodes): array {
205
		$userId = $this->userSession->getUser()->getUID();
206
		$requestedShareTypes = [
207
			IShare::TYPE_USER,
208
			IShare::TYPE_GROUP,
209
			IShare::TYPE_LINK,
210
			IShare::TYPE_REMOTE,
211
			IShare::TYPE_EMAIL,
212
			IShare::TYPE_ROOM,
213
			IShare::TYPE_DECK,
214
		];
215
		$shareTypes = [];
216
217
		$nodeIds = array_map(function (Node $node) {
218
			return $node->getId();
219
		}, $nodes);
220
221
		foreach ($requestedShareTypes as $shareType) {
222
			$nodesLeft = array_combine($nodeIds, array_fill(0, count($nodeIds), true));
223
			$offset = 0;
224
225
			// fetch shares until we've either found shares for all nodes or there are no more shares left
226
			while (count($nodesLeft) > 0) {
227
				$shares = $this->shareManager->getSharesBy($userId, $shareType, null, false, 100, $offset);
228
				foreach ($shares as $share) {
229
					$fileId = $share->getNodeId();
230
					if (isset($nodesLeft[$fileId])) {
231
						if (!isset($shareTypes[$fileId])) {
232
							$shareTypes[$fileId] = [];
233
						}
234
						$shareTypes[$fileId][] = $shareType;
235
						unset($nodesLeft[$fileId]);
236
					}
237
				}
238
239
				if (count($shares) < 100) {
240
					break;
241
				} else {
242
					$offset += count($shares);
243
				}
244
			}
245
		}
246
		return $shareTypes;
247
	}
248
249
	/**
250
	 * Returns a list of recently modifed files.
251
	 *
252
	 * @NoAdminRequired
253
	 *
254
	 * @return DataResponse
255
	 */
256
	public function getRecentFiles() {
257
		$nodes = $this->userFolder->getRecent(100);
258
		$files = $this->formatNodes($nodes);
259
		return new DataResponse(['files' => $files]);
260
	}
261
262
	/**
263
	 * Change the default sort mode
264
	 *
265
	 * @NoAdminRequired
266
	 *
267
	 * @param string $mode
268
	 * @param string $direction
269
	 * @return Response
270
	 * @throws \OCP\PreConditionNotMetException
271
	 */
272
	public function updateFileSorting($mode, $direction) {
273
		$allowedMode = ['name', 'size', 'mtime'];
274
		$allowedDirection = ['asc', 'desc'];
275
		if (!in_array($mode, $allowedMode) || !in_array($direction, $allowedDirection)) {
276
			$response = new Response();
277
			$response->setStatus(Http::STATUS_UNPROCESSABLE_ENTITY);
278
			return $response;
279
		}
280
		$this->config->setUserValue($this->userSession->getUser()->getUID(), 'files', 'file_sorting', $mode);
281
		$this->config->setUserValue($this->userSession->getUser()->getUID(), 'files', 'file_sorting_direction', $direction);
282
		return new Response();
283
	}
284
285
	/**
286
	 * Toggle default for showing/hiding hidden files
287
	 *
288
	 * @NoAdminRequired
289
	 *
290
	 * @param bool $show
291
	 * @return Response
292
	 * @throws \OCP\PreConditionNotMetException
293
	 */
294
	public function showHiddenFiles($show) {
295
		$this->config->setUserValue($this->userSession->getUser()->getUID(), 'files', 'show_hidden', (int)$show);
296
		return new Response();
297
	}
298
299
	/**
300
	 * Toggle default for cropping preview images
301
	 *
302
	 * @NoAdminRequired
303
	 *
304
	 * @param bool $crop
305
	 * @return Response
306
	 * @throws \OCP\PreConditionNotMetException
307
	 */
308
	public function cropImagePreviews($crop) {
309
		$this->config->setUserValue($this->userSession->getUser()->getUID(), 'files', 'crop_image_previews', (int)$crop);
310
		return new Response();
311
	}
312
313
	/**
314
	 * Toggle default for files grid view
315
	 *
316
	 * @NoAdminRequired
317
	 *
318
	 * @param bool $show
319
	 * @return Response
320
	 * @throws \OCP\PreConditionNotMetException
321
	 */
322
	public function showGridView($show) {
323
		$this->config->setUserValue($this->userSession->getUser()->getUID(), 'files', 'show_grid', (int)$show);
324
		return new Response();
325
	}
326
327
	/**
328
	 * Get default settings for the grid view
329
	 *
330
	 * @NoAdminRequired
331
	 */
332
	public function getGridView() {
333
		$status = $this->config->getUserValue($this->userSession->getUser()->getUID(), 'files', 'show_grid', '0') === '1';
334
		return new JSONResponse(['gridview' => $status]);
335
	}
336
337
	/**
338
	 * Toggle default for showing/hiding xxx folder
339
	 *
340
	 * @NoAdminRequired
341
	 *
342
	 * @param int $show
343
	 * @param string $key the key of the folder
344
	 *
345
	 * @return Response
346
	 * @throws \OCP\PreConditionNotMetException
347
	 */
348
	public function toggleShowFolder(int $show, string $key) {
349
		// ensure the edited key exists
350
		$navItems = \OCA\Files\App::getNavigationManager()->getAll();
351
		foreach ($navItems as $item) {
352
			// check if data is valid
353
			if (($show === 0 || $show === 1) && isset($item['expandedState']) && $key === $item['expandedState']) {
354
				$this->config->setUserValue($this->userSession->getUser()->getUID(), 'files', $key, $show);
355
				return new Response();
356
			}
357
		}
358
		$response = new Response();
359
		$response->setStatus(Http::STATUS_FORBIDDEN);
360
		return $response;
361
	}
362
363
	/**
364
	 * Get sorting-order for custom sorting
365
	 *
366
	 * @NoAdminRequired
367
	 *
368
	 * @param string $folderpath
369
	 * @return string
370
	 * @throws \OCP\Files\NotFoundException
371
	 */
372
	public function getNodeType($folderpath) {
373
		$node = $this->userFolder->get($folderpath);
374
		return $node->getType();
375
	}
376
}
377