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(); }