diff options
author | Julius Härtl <jus@bitgrid.net> | 2019-11-25 17:02:57 +0300 |
---|---|---|
committer | Julius Härtl <jus@bitgrid.net> | 2019-12-02 14:28:30 +0300 |
commit | a6bd99213f36b55b35e08e3fd609f631ba5e970d (patch) | |
tree | 15769d7e543c424723f8443bcfc6936f160bd578 /lib | |
parent | 5f7f70c75e7469309de671d732300f17ec69dde4 (diff) |
Add direct editing endpoint for workspaces
Signed-off-by: Julius Härtl <jus@bitgrid.net>
Diffstat (limited to 'lib')
-rw-r--r-- | lib/Controller/WorkspaceController.php | 57 |
1 files changed, 55 insertions, 2 deletions
diff --git a/lib/Controller/WorkspaceController.php b/lib/Controller/WorkspaceController.php index adeb0a76f..565cabe57 100644 --- a/lib/Controller/WorkspaceController.php +++ b/lib/Controller/WorkspaceController.php @@ -54,10 +54,13 @@ use OCP\AppFramework\Http; use OCP\AppFramework\Http\DataResponse; use OCP\AppFramework\Http\TemplateResponse; use OCP\AppFramework\OCSController; +use OCP\DirectEditing\IManager as IDirectEditingManager; +use OCP\DirectEditing\RegisterDirectEditorEvent; use OCP\Files\Folder; use OCP\Files\IRootFolder; use OCP\Files\NotFoundException; use OCP\IRequest; +use OCP\IURLGenerator; use OCP\Share\Exceptions\ShareNotFound; use OCP\Share\IManager; @@ -72,16 +75,29 @@ class WorkspaceController extends OCSController { /** @var WorkspaceService */ private $workspaceService; - /** @var string */ + /** @var string|null */ private $userId; + /** @var IDirectEditingManager */ + private $directEditingManager; - public function __construct($appName, IRequest $request, IRootFolder $rootFolder, IManager $shareManager, WorkspaceService $workspaceService, $userId) { + /** @var IURLGenerator */ + private $urlGenerator; + + private const SUPPORTED_FILENAMES = [ + 'README.md', + 'Readme.md', + 'readme.md' + ]; + + public function __construct($appName, IRequest $request, IRootFolder $rootFolder, IManager $shareManager, IDirectEditingManager $directEditingManager, IURLGenerator $urlGenerator, WorkspaceService $workspaceService, $userId) { parent::__construct($appName, $request); $this->rootFolder = $rootFolder; $this->shareManager = $shareManager; $this->workspaceService = $workspaceService; $this->userId = $userId; + $this->directEditingManager = $directEditingManager; + $this->urlGenerator = $urlGenerator; } /** @@ -145,4 +161,41 @@ class WorkspaceController extends OCSController { } } + /** + * @NoAdminRequired + */ + public function direct(string $path): DataResponse { + try { + $folder = $this->rootFolder->getUserFolder($this->userId)->get($path); + if ($folder instanceof Folder) { + $file = $this->getFile($folder); + if ($file === null) { + $token = $this->directEditingManager->open($file->getId(), Application::APP_NAME); + return new DataResponse([ + 'url' => $this->urlGenerator->linkToRouteAbsolute('files.DirectEditingView.edit', ['token' => $token]) + ]); + } + $token = $this->directEditingManager->create($path . '/'. self::SUPPORTED_FILENAMES[0], Application::APP_NAME, 'textdocument'); + return new DataResponse([ + 'url' => $this->urlGenerator->linkToRouteAbsolute('files.DirectEditingView.edit', ['token' => $token]) + ]); + } + + } catch (Exception $e) { + $this->logger->logException($e, ['message' => 'Exception when creating a new file through direct editing']); + return new DataResponse('Failed to create file', Http::STATUS_FORBIDDEN); + } + } + + private function getFile(Folder $folder) { + $file = null; + foreach (self::SUPPORTED_FILENAMES as $filename) { + if ($folder->nodeExists($filename)) { + $file = $folder->get($filename); + continue; + } + } + return $file; + } + } |