Skip to content

Commit f229236

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

File tree

5 files changed

+90
-28
lines changed

5 files changed

+90
-28
lines changed

lib/Db/Wopi.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,7 @@ public function getDirect() {
186186
return (bool)$this->direct;
187187
}
188188

189+
#[\ReturnTypeWillChange]
189190
public function jsonSerialize() {
190191
$properties = get_object_vars($this);
191192
$reflection = new \ReflectionClass($this);

lib/DirectEditing/DirectEditor.php

Lines changed: 62 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,20 +23,28 @@
2323

2424
namespace OCA\Richdocuments\DirectEditing;
2525

26+
use OCA\Richdocuments\AppConfig;
2627
use OCA\Richdocuments\AppInfo\Application;
2728
use OCA\Richdocuments\Capabilities;
29+
use OCA\Richdocuments\Controller\DocumentTrait;
30+
use OCA\Richdocuments\Service\InitialStateService;
31+
use OCA\Richdocuments\TokenManager;
2832
use OCP\AppFramework\Http\NotFoundResponse;
2933
use OCP\AppFramework\Http\Response;
3034
use OCP\AppFramework\Http\TemplateResponse;
3135
use OCP\DirectEditing\IEditor;
3236
use OCP\DirectEditing\IToken;
3337
use OCP\Files\InvalidPathException;
38+
use OCP\Files\IRootFolder;
3439
use OCP\Files\NotFoundException;
3540
use OCP\Files\NotPermittedException;
41+
use OCP\IConfig;
3642
use OCP\IInitialStateService;
3743
use OCP\IL10N;
44+
use Psr\Log\LoggerInterface;
3845

3946
class DirectEditor implements IEditor {
47+
use DocumentTrait;
4048

4149
/** @var IL10N */
4250
private $l10n;
@@ -47,11 +55,40 @@ class DirectEditor implements IEditor {
4755
/** @var string[] */
4856
private $mimetypes;
4957

58+
/** @var TokenManager */
59+
private $tokenManager;
5060

51-
public function __construct(IL10N $l10n, IInitialStateService $initialStateService, Capabilities $capabilities) {
61+
/** @var IRootFolder */
62+
private $rootFolder;
63+
64+
/** @var IConfig */
65+
private $config;
66+
67+
/** @var AppConfig */
68+
private $appConfig;
69+
70+
/** @var LoggerInterface */
71+
private $logger;
72+
73+
74+
public function __construct(
75+
IL10N $l10n,
76+
InitialStateService $initialStateService,
77+
Capabilities $capabilities,
78+
TokenManager $tokenManager,
79+
IConfig $config,
80+
AppConfig $appConfig,
81+
IRootFolder $rootFolder,
82+
LoggerInterface $logger
83+
) {
5284
$this->l10n = $l10n;
5385
$this->initialStateService = $initialStateService;
5486
$this->mimetypes = $capabilities->getCapabilities()[Application::APPNAME]['mimetypes'];
87+
$this->tokenManager = $tokenManager;
88+
$this->config = $config;
89+
$this->appConfig = $appConfig;
90+
$this->rootFolder = $rootFolder;
91+
$this->logger = $logger;
5592
}
5693

5794
/**
@@ -131,18 +168,30 @@ public function isSecure(): bool {
131168
public function open(IToken $token): Response {
132169
$token->useTokenScope();
133170
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) {
171+
$folder = $this->rootFolder->getUserFolder($token->getUser());
172+
$item = $token->getFile();
173+
174+
[$urlSrc, $token, $wopi] = $this->tokenManager->getToken($item->getId(), null, $token->getUser(), true);
175+
176+
$params = [
177+
'permissions' => $item->getPermissions(),
178+
'title' => $item->getName(),
179+
'fileId' => $wopi->getFileid() . '_' . $this->config->getSystemValue('instanceid'),
180+
'token' => $wopi->getToken(),
181+
'token_ttl' => $wopi->getExpiry(),
182+
'urlsrc' => $urlSrc,
183+
'path' => $folder->getRelativePath($item->getPath()),
184+
'instanceId' => $this->config->getSystemValue('instanceid'),
185+
'canonical_webroot' => $this->appConfig->getAppValue('canonical_webroot'),
186+
'direct' => true,
187+
];
188+
189+
$this->initialStateService->provideDocument($wopi);
190+
$response = new TemplateResponse('richdocuments', 'documents', $params, 'base');
191+
$this->applyPolicies($response);
192+
return $response;
193+
} catch (InvalidPathException|NotFoundException|NotPermittedException $e) {
194+
$this->logger->error($e->getMessage());
146195
}
147196
return new NotFoundResponse();
148197
}

lib/DirectEditing/PresentationCreator.php

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,18 +23,21 @@
2323

2424
namespace OCA\Richdocuments\DirectEditing;
2525

26+
use OCA\Richdocuments\AppInfo\Application;
2627
use OCP\DirectEditing\ACreateEmpty;
28+
use OCP\IConfig;
2729
use OCP\IL10N;
2830

2931
class PresentationCreator extends ACreateEmpty {
3032

31-
/**
32-
* @var IL10N
33-
*/
33+
/** @var IL10N */
3434
private $l10n;
35+
/** @var IConfig */
36+
private $config;
3537

36-
public function __construct(IL10N $l10n) {
38+
public function __construct(IL10N $l10n, IConfig $config) {
3739
$this->l10n = $l10n;
40+
$this->config = $config;
3841
}
3942

4043
public function getId(): string {
@@ -46,7 +49,8 @@ public function getName(): string {
4649
}
4750

4851
public function getExtension(): string {
49-
return 'odp';
52+
$useOoxml = $this->config->getAppValue(Application::APPNAME, 'doc_format', '') === 'ooxml';
53+
return $useOoxml ? 'pptx' : 'odp';
5054
}
5155

5256
public function getMimetype(): string {

lib/DirectEditing/SpreadsheetCreator.php

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,18 +23,21 @@
2323

2424
namespace OCA\Richdocuments\DirectEditing;
2525

26+
use OCA\Richdocuments\AppInfo\Application;
2627
use OCP\DirectEditing\ACreateEmpty;
28+
use OCP\IConfig;
2729
use OCP\IL10N;
2830

2931
class SpreadsheetCreator extends ACreateEmpty {
3032

31-
/**
32-
* @var IL10N
33-
*/
33+
/** @var IL10N */
3434
private $l10n;
35+
/** @var IConfig */
36+
private $config;
3537

36-
public function __construct(IL10N $l10n) {
38+
public function __construct(IL10N $l10n, IConfig $config) {
3739
$this->l10n = $l10n;
40+
$this->config = $config;
3841
}
3942

4043
public function getId(): string {
@@ -46,7 +49,8 @@ public function getName(): string {
4649
}
4750

4851
public function getExtension(): string {
49-
return 'ods';
52+
$useOoxml = $this->config->getAppValue(Application::APPNAME, 'doc_format', '') === 'ooxml';
53+
return $useOoxml ? 'xlsx' : 'ods';
5054
}
5155

5256
public function getMimetype(): string {

lib/DirectEditing/TextCreator.php

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,18 +24,21 @@
2424

2525
namespace OCA\Richdocuments\DirectEditing;
2626

27+
use OCA\Richdocuments\AppInfo\Application;
2728
use OCP\DirectEditing\ACreateEmpty;
29+
use OCP\IConfig;
2830
use OCP\IL10N;
2931

3032
class TextCreator extends ACreateEmpty {
3133

32-
/**
33-
* @var IL10N
34-
*/
34+
/** @var IL10N */
3535
private $l10n;
36+
/** @var IConfig */
37+
private $config;
3638

37-
public function __construct(IL10N $l10n) {
39+
public function __construct(IL10N $l10n, IConfig $config) {
3840
$this->l10n = $l10n;
41+
$this->config = $config;
3942
}
4043

4144
public function getId(): string {
@@ -47,7 +50,8 @@ public function getName(): string {
4750
}
4851

4952
public function getExtension(): string {
50-
return 'odt';
53+
$useOoxml = $this->config->getAppValue(Application::APPNAME, 'doc_format', '') === 'ooxml';
54+
return $useOoxml ? 'docx' : 'odt';
5155
}
5256

5357
public function getMimetype(): string {

0 commit comments

Comments
 (0)