-
Notifications
You must be signed in to change notification settings - Fork 133
Expand file tree
/
Copy pathWorkspaceController.php
More file actions
191 lines (175 loc) Β· 6.45 KB
/
Copy pathWorkspaceController.php
File metadata and controls
191 lines (175 loc) Β· 6.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
<?php
declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2019 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace OCA\Text\Controller;
use Exception;
use OCA\Text\AppInfo\Application;
use OCA\Text\DirectEditing\TextDocumentCreator;
use OCA\Text\Service\WorkspaceService;
use OCP\AppFramework\Http;
use OCP\AppFramework\Http\Attribute\NoAdminRequired;
use OCP\AppFramework\Http\Attribute\PublicPage;
use OCP\AppFramework\Http\DataResponse;
use OCP\AppFramework\OCSController;
use OCP\Constants;
use OCP\DirectEditing\IManager as IDirectEditingManager;
use OCP\DirectEditing\RegisterDirectEditorEvent;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\Files\File;
use OCP\Files\Folder;
use OCP\Files\IRootFolder;
use OCP\Files\NotFoundException;
use OCP\Files\NotPermittedException;
use OCP\IRequest;
use OCP\ISession;
use OCP\IURLGenerator;
use OCP\Share\Exceptions\ShareNotFound;
use OCP\Share\IManager;
use Psr\Log\LoggerInterface;
class WorkspaceController extends OCSController {
public function __construct(
string $appName,
IRequest $request,
private IRootFolder $rootFolder,
private IManager $shareManager,
private IDirectEditingManager $directEditingManager,
private IURLGenerator $urlGenerator,
private WorkspaceService $workspaceService,
private IEventDispatcher $eventDispatcher,
private LoggerInterface $logger,
private ISession $session,
private ?string $userId,
) {
parent::__construct($appName, $request);
}
/**
* Checks for available files in the current folder and returns required
* details to present the rich workspace.
*
* Returns 200 with file metadata and folder permissions if a README is found,
* or 404 with folder permissions if not (so the client can still offer to create one).
*
* @param string $path Path relative to the user's root folder
*/
#[NoAdminRequired]
public function folder(string $path = '/'): DataResponse {
try {
/** @psalm-suppress PossiblyNullArgument */
$userFolder = $this->rootFolder->getUserFolder($this->userId);
$folder = $userFolder->get($path);
if ($folder instanceof Folder) {
$file = $this->workspaceService->getFile($folder);
if ($file === null) {
return new DataResponse([
'message' => 'No workspace file found',
'folder' => [
'permissions' => $folder->getPermissions()
]
], Http::STATUS_NOT_FOUND);
}
return new DataResponse([
'file' => [
'id' => $file->getId(),
'mimetype' => $file->getMimetype(),
'name' => $file->getName(),
'path' => $userFolder->getRelativePath($file->getPath())
],
'folder' => [
'permissions' => $folder->getPermissions()
]
]);
}
} catch (NotFoundException|NotPermittedException) {
return new DataResponse(['message' => 'No workspace file found'], Http::STATUS_NOT_FOUND);
} catch (Exception $e) {
$this->logger->error('Failed to get workspace file', ['exception' => $e]);
return new DataResponse(['message' => 'No workspace file found'], Http::STATUS_NOT_FOUND);
}
return new DataResponse(['message' => 'No workspace file found'], Http::STATUS_NOT_FOUND);
}
/**
* Checks for available files in a publicly shared folder and returns required
* details to present the rich workspace.
*
* @param string $shareToken Public share token
* @param string $path Path relative to the share root
*/
#[NoAdminRequired]
#[PublicPage]
public function publicFolder(string $shareToken, string $path = '/'): DataResponse {
try {
$share = $this->shareManager->getShareByToken($shareToken);
if (!($share->getPermissions() & Constants::PERMISSION_READ)) {
throw new ShareNotFound();
}
/** @psalm-suppress RedundantConditionGivenDocblockType */
if ($share->getPassword() !== null) {
$shareIds = $this->session->get('public_link_authenticated');
$shareIds = is_array($shareIds) ? $shareIds : [$shareIds];
if (!in_array($share->getId(), $shareIds, true)) {
throw new ShareNotFound();
}
}
$shareNode = $share->getNode();
$node = $shareNode instanceof File ? $shareNode : $shareNode->get($path);
if ($node instanceof Folder) {
$file = $this->workspaceService->getFile($node);
if ($file === null) {
return new DataResponse(['message' => 'No workspace file found'], Http::STATUS_NOT_FOUND);
}
return new DataResponse([
'file' => [
'id' => $file->getId(),
'mimetype' => $file->getMimetype(),
'name' => $file->getName(),
'path' => $path . '/' . $file->getName()
]
]);
}
} catch (NotFoundException|ShareNotFound) {
return new DataResponse(['message' => 'No workspace file found'], Http::STATUS_NOT_FOUND);
} catch (Exception $e) {
$this->logger->error('Failed to get public workspace file', ['exception' => $e]);
return new DataResponse(['message' => 'No workspace file found'], Http::STATUS_NOT_FOUND);
}
return new DataResponse(['message' => 'No workspace file found'], Http::STATUS_NOT_FOUND);
}
/**
* Returns a direct editing URL for the workspace README in the given folder.
*
* If a README file already exists, opens it for editing. If not, creates a new
* file using the first entry of getSupportedFilenames() as the default name.
*
* @param string $path Path to the folder, relative to the user's root folder
*/
#[NoAdminRequired]
public function direct(string $path): DataResponse {
$this->eventDispatcher->dispatchTyped(new RegisterDirectEditorEvent($this->directEditingManager));
try {
/** @psalm-suppress PossiblyNullArgument */
$folder = $this->rootFolder->getUserFolder($this->userId)->get($path);
if ($folder instanceof Folder) {
$file = $this->workspaceService->getFile($folder);
if ($file === null) {
$token = $this->directEditingManager->create(
$path . '/' . $this->workspaceService->getSupportedFilenames()[0],
Application::APP_NAME,
TextDocumentCreator::CREATOR_ID
);
} else {
$token = $this->directEditingManager->open($path . '/' . $file->getName(), Application::APP_NAME);
}
return new DataResponse([
'url' => $this->urlGenerator->linkToRouteAbsolute('files.DirectEditingView.edit', ['token' => $token])
]);
}
} catch (Exception $e) {
$this->logger->error('Exception when creating a new file through direct editing', ['exception' => $e]);
return new DataResponse('Failed to create file', Http::STATUS_FORBIDDEN);
}
return new DataResponse(['message' => 'No workspace file found'], Http::STATUS_NOT_FOUND);
}
}