From f2292366f26bf58cd5b9947ed316c820c5639bdb Mon Sep 17 00:00:00 2001 From: Raul Date: Wed, 24 Aug 2022 10:02:45 +0200 Subject: [PATCH] Implement direct editing API Signed-off-by: Raul --- lib/Db/Wopi.php | 1 + lib/DirectEditing/DirectEditor.php | 75 +++++++++++++++++++---- lib/DirectEditing/PresentationCreator.php | 14 +++-- lib/DirectEditing/SpreadsheetCreator.php | 14 +++-- lib/DirectEditing/TextCreator.php | 14 +++-- 5 files changed, 90 insertions(+), 28 deletions(-) diff --git a/lib/Db/Wopi.php b/lib/Db/Wopi.php index c90f199436..2fb1514d95 100644 --- a/lib/Db/Wopi.php +++ b/lib/Db/Wopi.php @@ -186,6 +186,7 @@ public function getDirect() { return (bool)$this->direct; } + #[\ReturnTypeWillChange] public function jsonSerialize() { $properties = get_object_vars($this); $reflection = new \ReflectionClass($this); diff --git a/lib/DirectEditing/DirectEditor.php b/lib/DirectEditing/DirectEditor.php index 0839158083..78b1f9bad4 100644 --- a/lib/DirectEditing/DirectEditor.php +++ b/lib/DirectEditing/DirectEditor.php @@ -23,20 +23,28 @@ namespace OCA\Richdocuments\DirectEditing; +use OCA\Richdocuments\AppConfig; use OCA\Richdocuments\AppInfo\Application; use OCA\Richdocuments\Capabilities; +use OCA\Richdocuments\Controller\DocumentTrait; +use OCA\Richdocuments\Service\InitialStateService; +use OCA\Richdocuments\TokenManager; 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\IRootFolder; use OCP\Files\NotFoundException; use OCP\Files\NotPermittedException; +use OCP\IConfig; use OCP\IInitialStateService; use OCP\IL10N; +use Psr\Log\LoggerInterface; class DirectEditor implements IEditor { + use DocumentTrait; /** @var IL10N */ private $l10n; @@ -47,11 +55,40 @@ class DirectEditor implements IEditor { /** @var string[] */ private $mimetypes; + /** @var TokenManager */ + private $tokenManager; - public function __construct(IL10N $l10n, IInitialStateService $initialStateService, Capabilities $capabilities) { + /** @var IRootFolder */ + private $rootFolder; + + /** @var IConfig */ + private $config; + + /** @var AppConfig */ + private $appConfig; + + /** @var LoggerInterface */ + private $logger; + + + public function __construct( + IL10N $l10n, + InitialStateService $initialStateService, + Capabilities $capabilities, + TokenManager $tokenManager, + IConfig $config, + AppConfig $appConfig, + IRootFolder $rootFolder, + LoggerInterface $logger + ) { $this->l10n = $l10n; $this->initialStateService = $initialStateService; $this->mimetypes = $capabilities->getCapabilities()[Application::APPNAME]['mimetypes']; + $this->tokenManager = $tokenManager; + $this->config = $config; + $this->appConfig = $appConfig; + $this->rootFolder = $rootFolder; + $this->logger = $logger; } /** @@ -131,18 +168,30 @@ public function isSecure(): bool { public function open(IToken $token): Response { $token->useTokenScope(); try { - $session = $this->apiService->create($token->getFile()->getId()); - $this->initialStateService->provideInitialState('text', 'file', [ - 'fileId' => $token->getFile()->getId(), - 'mimetype' => $token->getFile()->getMimeType(), - 'content' => $token->getFile()->getContent(), - 'session' => \json_encode($session->getData()) - ]); - $this->initialStateService->provideInitialState('text', 'directEditingToken', $token->getToken()); - return new TemplateResponse(Application::APPNAME, 'main', [], 'base'); - } catch (InvalidPathException $e) { - } catch (NotFoundException $e) { - } catch (NotPermittedException $e) { + $folder = $this->rootFolder->getUserFolder($token->getUser()); + $item = $token->getFile(); + + [$urlSrc, $token, $wopi] = $this->tokenManager->getToken($item->getId(), null, $token->getUser(), true); + + $params = [ + 'permissions' => $item->getPermissions(), + 'title' => $item->getName(), + 'fileId' => $wopi->getFileid() . '_' . $this->config->getSystemValue('instanceid'), + 'token' => $wopi->getToken(), + 'token_ttl' => $wopi->getExpiry(), + 'urlsrc' => $urlSrc, + 'path' => $folder->getRelativePath($item->getPath()), + 'instanceId' => $this->config->getSystemValue('instanceid'), + 'canonical_webroot' => $this->appConfig->getAppValue('canonical_webroot'), + 'direct' => true, + ]; + + $this->initialStateService->provideDocument($wopi); + $response = new TemplateResponse('richdocuments', 'documents', $params, 'base'); + $this->applyPolicies($response); + return $response; + } catch (InvalidPathException|NotFoundException|NotPermittedException $e) { + $this->logger->error($e->getMessage()); } return new NotFoundResponse(); } diff --git a/lib/DirectEditing/PresentationCreator.php b/lib/DirectEditing/PresentationCreator.php index cd11887562..3df24ccdec 100644 --- a/lib/DirectEditing/PresentationCreator.php +++ b/lib/DirectEditing/PresentationCreator.php @@ -23,18 +23,21 @@ namespace OCA\Richdocuments\DirectEditing; +use OCA\Richdocuments\AppInfo\Application; use OCP\DirectEditing\ACreateEmpty; +use OCP\IConfig; use OCP\IL10N; class PresentationCreator extends ACreateEmpty { - /** - * @var IL10N - */ + /** @var IL10N */ private $l10n; + /** @var IConfig */ + private $config; - public function __construct(IL10N $l10n) { + public function __construct(IL10N $l10n, IConfig $config) { $this->l10n = $l10n; + $this->config = $config; } public function getId(): string { @@ -46,7 +49,8 @@ public function getName(): string { } public function getExtension(): string { - return 'odp'; + $useOoxml = $this->config->getAppValue(Application::APPNAME, 'doc_format', '') === 'ooxml'; + return $useOoxml ? 'pptx' : 'odp'; } public function getMimetype(): string { diff --git a/lib/DirectEditing/SpreadsheetCreator.php b/lib/DirectEditing/SpreadsheetCreator.php index 8684cd50a8..9a3daefeac 100644 --- a/lib/DirectEditing/SpreadsheetCreator.php +++ b/lib/DirectEditing/SpreadsheetCreator.php @@ -23,18 +23,21 @@ namespace OCA\Richdocuments\DirectEditing; +use OCA\Richdocuments\AppInfo\Application; use OCP\DirectEditing\ACreateEmpty; +use OCP\IConfig; use OCP\IL10N; class SpreadsheetCreator extends ACreateEmpty { - /** - * @var IL10N - */ + /** @var IL10N */ private $l10n; + /** @var IConfig */ + private $config; - public function __construct(IL10N $l10n) { + public function __construct(IL10N $l10n, IConfig $config) { $this->l10n = $l10n; + $this->config = $config; } public function getId(): string { @@ -46,7 +49,8 @@ public function getName(): string { } public function getExtension(): string { - return 'ods'; + $useOoxml = $this->config->getAppValue(Application::APPNAME, 'doc_format', '') === 'ooxml'; + return $useOoxml ? 'xlsx' : 'ods'; } public function getMimetype(): string { diff --git a/lib/DirectEditing/TextCreator.php b/lib/DirectEditing/TextCreator.php index e569af494a..1764df0eef 100644 --- a/lib/DirectEditing/TextCreator.php +++ b/lib/DirectEditing/TextCreator.php @@ -24,18 +24,21 @@ namespace OCA\Richdocuments\DirectEditing; +use OCA\Richdocuments\AppInfo\Application; use OCP\DirectEditing\ACreateEmpty; +use OCP\IConfig; use OCP\IL10N; class TextCreator extends ACreateEmpty { - /** - * @var IL10N - */ + /** @var IL10N */ private $l10n; + /** @var IConfig */ + private $config; - public function __construct(IL10N $l10n) { + public function __construct(IL10N $l10n, IConfig $config) { $this->l10n = $l10n; + $this->config = $config; } public function getId(): string { @@ -47,7 +50,8 @@ public function getName(): string { } public function getExtension(): string { - return 'odt'; + $useOoxml = $this->config->getAppValue(Application::APPNAME, 'doc_format', '') === 'ooxml'; + return $useOoxml ? 'docx' : 'odt'; } public function getMimetype(): string {