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