Skip to content

Commit eeda34b

Browse files
committed
Implement direct editing API
Signed-off-by: Raul <r.ferreira.fuentes@gmail.com>
1 parent 46d811d commit eeda34b

File tree

7 files changed

+421
-0
lines changed

7 files changed

+421
-0
lines changed

lib/AppInfo/Application.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
use OCA\Richdocuments\Listener\CSPListener;
3131
use OCA\Richdocuments\Listener\LoadViewerListener;
3232
use OCA\Richdocuments\Listener\ShareLinkListener;
33+
use OCA\Richdocuments\Listeners\RegisterDirectEditorEventListener;
3334
use OCA\Richdocuments\Middleware\WOPIMiddleware;
3435
use OCA\Richdocuments\Listener\FileCreatedFromTemplateListener;
3536
use OCA\Richdocuments\Preview\MSExcel;
@@ -45,6 +46,7 @@
4546
use OCP\AppFramework\Bootstrap\IBootContext;
4647
use OCP\AppFramework\Bootstrap\IBootstrap;
4748
use OCP\AppFramework\Bootstrap\IRegistrationContext;
49+
use OCP\DirectEditing\RegisterDirectEditorEvent;
4850
use OCP\Files\Template\FileCreatedFromTemplateEvent;
4951
use OCP\Files\Template\ITemplateManager;
5052
use OCP\Files\Template\TemplateFileCreator;
@@ -69,6 +71,7 @@ public function register(IRegistrationContext $context): void {
6971
$context->registerEventListener(AddContentSecurityPolicyEvent::class, CSPListener::class);
7072
$context->registerEventListener(LoadViewer::class, LoadViewerListener::class);
7173
$context->registerEventListener(ShareLinkAccessedEvent::class, ShareLinkListener::class);
74+
$context->registerEventListener(RegisterDirectEditorEvent::class, RegisterDirectEditorEventListener::class);
7275
}
7376

7477
public function boot(IBootContext $context): void {

lib/DirectEditing/DirectEditor.php

Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
<?php
2+
/**
3+
* @copyright Copyright (c) 2022 Raul Ferreira Fuentes <raul@nextcloud.com>
4+
*
5+
* @author Raul Ferreira Fuentes <raul@nextcloud.com>
6+
*
7+
* @license GNU AGPL version 3 or any later version
8+
*
9+
* This program is free software: you can redistribute it and/or modify
10+
* it under the terms of the GNU Affero General Public License as
11+
* published by the Free Software Foundation, either version 3 of the
12+
* License, or (at your option) any later version.
13+
*
14+
* This program is distributed in the hope that it will be useful,
15+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
16+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17+
* GNU Affero General Public License for more details.
18+
*
19+
* You should have received a copy of the GNU Affero General Public License
20+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
21+
*
22+
*/
23+
24+
namespace OCA\Richdocuments\DirectEditing;
25+
26+
use OCA\Richdocuments\AppInfo\Application;
27+
use OCA\Richdocuments\Capabilities;
28+
use OCP\AppFramework\Http\NotFoundResponse;
29+
use OCP\AppFramework\Http\Response;
30+
use OCP\AppFramework\Http\TemplateResponse;
31+
use OCP\DirectEditing\IEditor;
32+
use OCP\DirectEditing\IToken;
33+
use OCP\Files\InvalidPathException;
34+
use OCP\Files\NotFoundException;
35+
use OCP\Files\NotPermittedException;
36+
use OCP\IInitialStateService;
37+
use OCP\IL10N;
38+
39+
class DirectEditor implements IEditor {
40+
41+
/** @var IL10N */
42+
private $l10n;
43+
44+
/** @var IInitialStateService */
45+
private $initialStateService;
46+
47+
/** @var string[] */
48+
private $mimetypes;
49+
50+
51+
public function __construct(IL10N $l10n, IInitialStateService $initialStateService, Capabilities $capabilities) {
52+
$this->l10n = $l10n;
53+
$this->initialStateService = $initialStateService;
54+
$this->mimetypes = $capabilities->getCapabilities()[Application::APPNAME]['mimetypes'];
55+
}
56+
57+
/**
58+
* Return a unique identifier for the editor
59+
*
60+
* e.g. richdocuments
61+
*
62+
* @return string
63+
*/
64+
public function getId(): string {
65+
return Application::APPNAME;
66+
}
67+
68+
/**
69+
* Return a readable name for the editor
70+
*
71+
* e.g. Collabora Online
72+
*
73+
* @return string
74+
*/
75+
public function getName(): string {
76+
return $this->l10n->t('Nextcloud Office');
77+
}
78+
79+
/**
80+
* A list of mimetypes that should open the editor by default
81+
*
82+
* @return array
83+
*/
84+
public function getMimetypes(): array {
85+
return $this->mimetypes;
86+
}
87+
88+
/**
89+
* A list of mimetypes that can be opened in the editor optionally
90+
*
91+
* @return array
92+
*/
93+
public function getMimetypesOptional(): array {
94+
return [];
95+
}
96+
97+
/**
98+
* Return a list of file creation options to be presented to the user
99+
*
100+
* @return array of ACreateFromTemplate|ACreateEmpty
101+
*/
102+
public function getCreators(): array {
103+
return [
104+
new GraphicsCreator($this->l10n),
105+
new PresentationCreator($this->l10n),
106+
new SpreadsheetCreator($this->l10n),
107+
new TextCreator($this->l10n),
108+
];
109+
}
110+
111+
/**
112+
* Return if the view is able to securely view a file without downloading it to the browser
113+
*
114+
* @return bool
115+
*/
116+
public function isSecure(): bool {
117+
return true;
118+
}
119+
120+
/**
121+
* Return a template response for displaying the editor
122+
*
123+
* open can only be called once when the client requests the editor with a one-time-use token
124+
* For handling editing and later requests, editors need to impelement their own token handling and take care of invalidation
125+
*
126+
* 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
127+
*
128+
* @param IToken $token
129+
* @return Response
130+
*/
131+
public function open(IToken $token): Response {
132+
$token->useTokenScope();
133+
try {
134+
$session = $this->apiService->create($token->getFile()->getId());
135+
$this->initialStateService->provideInitialState('text', 'file', [
136+
'fileId' => $token->getFile()->getId(),
137+
'mimetype' => $token->getFile()->getMimeType(),
138+
'content' => $token->getFile()->getContent(),
139+
'session' => \json_encode($session->getData())
140+
]);
141+
$this->initialStateService->provideInitialState('text', 'directEditingToken', $token->getToken());
142+
return new TemplateResponse(Application::APPNAME, 'main', [], 'base');
143+
} catch (InvalidPathException $e) {
144+
} catch (NotFoundException $e) {
145+
} catch (NotPermittedException $e) {
146+
}
147+
return new NotFoundResponse();
148+
}
149+
}

lib/DirectEditing/GraphicsCreator.php

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
/**
3+
* @copyright Copyright (c) 2022 Raul Ferreira Fuentes <raul@nextcloud.com>
4+
*
5+
* @author Raul Ferreira Fuentes <raul@nextcloud.com>
6+
*
7+
* @license GNU AGPL version 3 or any later version
8+
*
9+
* This program is free software: you can redistribute it and/or modify
10+
* it under the terms of the GNU Affero General Public License as
11+
* published by the Free Software Foundation, either version 3 of the
12+
* License, or (at your option) any later version.
13+
*
14+
* This program is distributed in the hope that it will be useful,
15+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
16+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17+
* GNU Affero General Public License for more details.
18+
*
19+
* You should have received a copy of the GNU Affero General Public License
20+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
21+
*
22+
*/
23+
24+
namespace OCA\Richdocuments\DirectEditing;
25+
26+
use OCP\DirectEditing\ACreateEmpty;
27+
use OCP\IL10N;
28+
29+
class GraphicsCreator extends ACreateEmpty {
30+
31+
/**
32+
* @var IL10N
33+
*/
34+
private $l10n;
35+
36+
public function __construct(IL10N $l10n) {
37+
$this->l10n = $l10n;
38+
}
39+
40+
public function getId(): string {
41+
return 'richdocuments_graphics';
42+
}
43+
44+
public function getName(): string {
45+
return $this->l10n->t('diagram');
46+
}
47+
48+
public function getExtension(): string {
49+
return 'odg';
50+
}
51+
52+
public function getMimetype(): string {
53+
return 'application/vnd.oasis.opendocument.graphics';
54+
}
55+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
/**
3+
* @copyright Copyright (c) 2022 Raul Ferreira Fuentes <raul@nextcloud.com>
4+
*
5+
* @author Raul Ferreira Fuentes <raul@nextcloud.com>
6+
*
7+
* @license GNU AGPL version 3 or any later version
8+
*
9+
* This program is free software: you can redistribute it and/or modify
10+
* it under the terms of the GNU Affero General Public License as
11+
* published by the Free Software Foundation, either version 3 of the
12+
* License, or (at your option) any later version.
13+
*
14+
* This program is distributed in the hope that it will be useful,
15+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
16+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17+
* GNU Affero General Public License for more details.
18+
*
19+
* You should have received a copy of the GNU Affero General Public License
20+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
21+
*
22+
*/
23+
24+
namespace OCA\Richdocuments\DirectEditing;
25+
26+
use OCP\DirectEditing\ACreateEmpty;
27+
use OCP\IL10N;
28+
29+
class PresentationCreator extends ACreateEmpty {
30+
31+
/**
32+
* @var IL10N
33+
*/
34+
private $l10n;
35+
36+
public function __construct(IL10N $l10n) {
37+
$this->l10n = $l10n;
38+
}
39+
40+
public function getId(): string {
41+
return 'richdocuments_presentation';
42+
}
43+
44+
public function getName(): string {
45+
return $this->l10n->t('presentation');
46+
}
47+
48+
public function getExtension(): string {
49+
return 'odp';
50+
}
51+
52+
public function getMimetype(): string {
53+
return 'application/vnd.oasis.opendocument.presentation';
54+
}
55+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
/**
3+
* @copyright Copyright (c) 2022 Raul Ferreira Fuentes <raul@nextcloud.com>
4+
*
5+
* @author Raul Ferreira Fuentes <raul@nextcloud.com>
6+
*
7+
* @license GNU AGPL version 3 or any later version
8+
*
9+
* This program is free software: you can redistribute it and/or modify
10+
* it under the terms of the GNU Affero General Public License as
11+
* published by the Free Software Foundation, either version 3 of the
12+
* License, or (at your option) any later version.
13+
*
14+
* This program is distributed in the hope that it will be useful,
15+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
16+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17+
* GNU Affero General Public License for more details.
18+
*
19+
* You should have received a copy of the GNU Affero General Public License
20+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
21+
*
22+
*/
23+
24+
namespace OCA\Richdocuments\DirectEditing;
25+
26+
use OCP\DirectEditing\ACreateEmpty;
27+
use OCP\IL10N;
28+
29+
class SpreadsheetCreator extends ACreateEmpty {
30+
31+
/**
32+
* @var IL10N
33+
*/
34+
private $l10n;
35+
36+
public function __construct(IL10N $l10n) {
37+
$this->l10n = $l10n;
38+
}
39+
40+
public function getId(): string {
41+
return 'richdocuments_spreadsheet';
42+
}
43+
44+
public function getName(): string {
45+
return $this->l10n->t('spreadsheet');
46+
}
47+
48+
public function getExtension(): string {
49+
return 'ods';
50+
}
51+
52+
public function getMimetype(): string {
53+
return 'application/vnd.oasis.opendocument.spreadsheet';
54+
}
55+
}

lib/DirectEditing/TextCreator.php

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
/**
3+
* @copyright Copyright (c) 2022 Raul Ferreira Fuentes <raul@nextcloud.com>
4+
*
5+
* @author Raul Ferreira Fuentes <raul@nextcloud.com>
6+
*
7+
* @license GNU AGPL version 3 or any later version
8+
*
9+
* This program is free software: you can redistribute it and/or modify
10+
* it under the terms of the GNU Affero General Public License as
11+
* published by the Free Software Foundation, either version 3 of the
12+
* License, or (at your option) any later version.
13+
*
14+
* This program is distributed in the hope that it will be useful,
15+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
16+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17+
* GNU Affero General Public License for more details.
18+
*
19+
* You should have received a copy of the GNU Affero General Public License
20+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
21+
*
22+
*/
23+
24+
25+
namespace OCA\Richdocuments\DirectEditing;
26+
27+
use OCP\DirectEditing\ACreateEmpty;
28+
use OCP\IL10N;
29+
30+
class TextCreator extends ACreateEmpty {
31+
32+
/**
33+
* @var IL10N
34+
*/
35+
private $l10n;
36+
37+
public function __construct(IL10N $l10n) {
38+
$this->l10n = $l10n;
39+
}
40+
41+
public function getId(): string {
42+
return 'richdocuments_text';
43+
}
44+
45+
public function getName(): string {
46+
return $this->l10n->t('document');
47+
}
48+
49+
public function getExtension(): string {
50+
return 'odt';
51+
}
52+
53+
public function getMimetype(): string {
54+
return 'application/vnd.oasis.opendocument.text';
55+
}
56+
}

0 commit comments

Comments
 (0)