Skip to content

Commit

Permalink
Implement direct editing API
Browse files Browse the repository at this point in the history
Signed-off-by: Raul <r.ferreira.fuentes@gmail.com>
  • Loading branch information
Raudius committed Oct 13, 2022
1 parent b909462 commit f8f4ba7
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 13 deletions.
1 change: 1 addition & 0 deletions lib/Db/Wopi.php
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@ public function getDirect() {
return (bool)$this->direct;
}

#[\ReturnTypeWillChange]
public function jsonSerialize() {
$properties = get_object_vars($this);
$reflection = new \ReflectionClass($this);
Expand Down
75 changes: 62 additions & 13 deletions lib/DirectEditing/DirectEditor.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}

/**
Expand Down Expand Up @@ -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();
}
Expand Down

0 comments on commit f8f4ba7

Please sign in to comment.