-
Notifications
You must be signed in to change notification settings - Fork 119
Expand file tree
/
Copy pathTextDirectEditor.php
More file actions
164 lines (148 loc) Β· 4 KB
/
TextDirectEditor.php
File metadata and controls
164 lines (148 loc) Β· 4 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
<?php
/**
* SPDX-FileCopyrightText: 2019 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace OCA\Text\DirectEditing;
use OCA\Text\AppInfo\Application;
use OCA\Text\Service\ApiService;
use OCA\Text\Service\InitialStateProvider;
use OCP\AppFramework\Http\NotFoundResponse;
use OCP\AppFramework\Http\Response;
use OCP\AppFramework\Http\TemplateResponse;
use OCP\DirectEditing\IEditor;
use OCP\DirectEditing\IToken;
use OCP\Files\InvalidPathException;
use OCP\Files\NotFoundException;
use OCP\Files\NotPermittedException;
use OCP\IAppConfig;
use OCP\IL10N;
use OCP\Util;
class TextDirectEditor implements IEditor {
/** @var IL10N */
private $l10n;
/** @var InitialStateProvider */
private $initialStateProvider;
/** @var ApiService */
private $apiService;
/**
* @var IAppConfig
*/
private $appConfig;
public function __construct(IL10N $l10n, InitialStateProvider $initialStateProvider, ApiService $apiService, IAppConfig $appConfig) {
$this->l10n = $l10n;
$this->initialStateProvider = $initialStateProvider;
$this->apiService = $apiService;
$this->appConfig = $appConfig;
}
/**
* Return a unique identifier for the editor
*
* e.g. richdocuments
*
* @return string
*/
public function getId(): string {
return Application::APP_NAME;
}
/**
* Return a readable name for the editor
*
* e.g. Collabora Online
*
* @return string
*/
public function getName(): string {
return $this->l10n->t('Nextcloud Text');
}
/**
* A list of mimetypes that should open the editor by default
*
* @return string[]
*/
public function getMimetypes(): array {
return [
'text/markdown',
'text/plain',
'application/cmd',
'application/x-empty',
'application/x-msdos-program',
'application/javascript',
'application/json',
'application/x-perl',
'application/x-php',
'application/x-tex',
'application/xml',
'application/yaml',
'text/css',
'text/csv',
'text/html',
'text/org',
'text/x-c',
'text/x-c++src',
'text/x-h',
'text/x-java-source',
'text/x-ldif',
'text/x-nfo',
'text/x-python',
'text/x-shellscript',
];
}
/**
* A list of mimetypes that can be opened in the editor optionally
*
* @return string[]
*/
public function getMimetypesOptional(): array {
return [];
}
/**
* Return a list of file creation options to be presented to the user
*
* @return TextDocumentCreator[]
*/
public function getCreators(): array {
return [
new TextDocumentCreator($this->l10n, $this->appConfig),
];
}
/**
* Return if the view is able to securely view a file without downloading it to the browser
*
* @return bool
*/
public function isSecure(): bool {
return false;
}
/**
* Return a template response for displaying the editor
*
* open can only be called once when the client requests the editor with a one-time-use token
* For handling editing and later requests, editors need to impelement their own token handling and take care of invalidation
*
* This behavior is similar to the current direct editing implementation in collabora where we generate a one-time token and switch over to the regular wopi token for the actual editing/saving process
*
* @param IToken $token
* @return Response
*/
public function open(IToken $token): Response {
$token->useTokenScope();
try {
$session = $this->apiService->create($token->getFile()->getId());
$this->initialStateProvider->provideFile([
'fileId' => $token->getFile()->getId(),
'mimetype' => $token->getFile()->getMimeType(),
'session' => \json_encode($session->getData())
]);
$this->initialStateProvider->provideDirectEditToken($token->getToken());
$this->initialStateProvider->provideState();
Util::addScript('text', 'text-text');
Util::addStyle('text', 'text-text');
return new TemplateResponse('text', 'main', [], 'base');
} catch (InvalidPathException $e) {
} catch (NotFoundException $e) {
} catch (NotPermittedException $e) {
}
return new NotFoundResponse();
}
}