Skip to content

Commit 2a6652d

Browse files
committed
Add dav plugin for rich workspaces
Signed-off-by: Julius Härtl <jus@bitgrid.net>
1 parent 35bdf79 commit 2a6652d

File tree

4 files changed

+147
-20
lines changed

4 files changed

+147
-20
lines changed

appinfo/info.xml

+9-1
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,14 @@
1111
- **💾 Open format:** Files are saved as [Markdown](https://en.wikipedia.org/wiki/Markdown), so you can edit them from any other text app too.
1212
- **✊ Strong foundation:** We use [🐈 tiptap](https://tiptap.scrumpy.io) which is based on [🦉 ProseMirror](https://prosemirror.net) – huge thanks to them!
1313
]]></description>
14-
<version>1.2.0</version>
14+
<version>1.2.3</version>
1515
<licence>agpl</licence>
1616
<author mail="jus@bitgrid.net">Julius Härtl</author>
1717
<namespace>Text</namespace>
1818
<default_enable/>
19+
<types>
20+
<dav />
21+
</types>
1922
<category>office</category>
2023
<website>https://github.com/nextcloud/text</website>
2124
<bugs>https://github.com/nextcloud/text/issues</bugs>
@@ -29,4 +32,9 @@
2932
<background-jobs>
3033
<job>OCA\Text\Cron\Cleanup</job>
3134
</background-jobs>
35+
<sabre>
36+
<plugins>
37+
<plugin>OCA\Text\DAV\WorkspacePlugin</plugin>
38+
</plugins>
39+
</sabre>
3240
</info>

lib/Controller/WorkspaceController.php

+10-19
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848

4949
use OCA\Text\AppInfo\Application;
5050
use OCA\Text\Service\SessionService;
51+
use OCA\Text\Service\WorkspaceService;
5152
use OCP\AppFramework\Controller;
5253
use OCP\AppFramework\Http;
5354
use OCP\AppFramework\Http\DataResponse;
@@ -68,17 +69,18 @@ class WorkspaceController extends OCSController {
6869
/** @var IManager */
6970
private $shareManager;
7071

71-
private const SUPPORTED_FILENAMES = [
72-
'README.md',
73-
'Readme.md',
74-
'readme.md'
75-
];
72+
/** @var WorkspaceService */
73+
private $workspaceService;
7674

75+
/** @var string */
76+
private $userId;
7777

78-
public function __construct($appName, IRequest $request, IRootFolder $rootFolder, IManager $shareManager, $userId) {
78+
79+
public function __construct($appName, IRequest $request, IRootFolder $rootFolder, IManager $shareManager, WorkspaceService $workspaceService, $userId) {
7980
parent::__construct($appName, $request);
8081
$this->rootFolder = $rootFolder;
8182
$this->shareManager = $shareManager;
83+
$this->workspaceService = $workspaceService;
8284
$this->userId = $userId;
8385
}
8486

@@ -92,7 +94,7 @@ public function folder(string $path = '/'): DataResponse {
9294
try {
9395
$folder = $this->rootFolder->getUserFolder($this->userId)->get($path);
9496
if ($folder instanceof Folder) {
95-
$file = $this->getFile($folder);
97+
$file = $this->workspaceService->getFile($folder);
9698
if ($file === null) {
9799
return new DataResponse(['message' => 'No workspace file found'], Http::STATUS_NOT_FOUND);
98100
}
@@ -122,7 +124,7 @@ public function publicFolder(string $shareToken, string $path = '/'): DataRespon
122124
$share = $this->shareManager->getShareByToken($shareToken);
123125
$folder = $share->getNode()->get($path);
124126
if ($folder instanceof Folder) {
125-
$file = $this->getFile($folder);
127+
$file = $this->workspaceService->getFile($folder);
126128
if ($file === null) {
127129
return new DataResponse(['message' => 'No workspace file found'], Http::STATUS_NOT_FOUND);
128130
}
@@ -143,15 +145,4 @@ public function publicFolder(string $shareToken, string $path = '/'): DataRespon
143145
}
144146
}
145147

146-
private function getFile(Folder $folder) {
147-
$file = null;
148-
foreach (self::SUPPORTED_FILENAMES as $filename) {
149-
if ($folder->nodeExists($filename)) {
150-
$file = $folder->get($filename);
151-
continue;
152-
}
153-
}
154-
return $file;
155-
}
156-
157148
}

lib/DAV/WorkspacePlugin.php

+97
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
<?php
2+
declare(strict_types=1);
3+
/**
4+
* @copyright Copyright (c) 2019 Julius Härtl <jus@bitgrid.net>
5+
*
6+
* @author Julius Härtl <jus@bitgrid.net>
7+
*
8+
* @license GNU AGPL version 3 or any later version
9+
*
10+
* This program is free software: you can redistribute it and/or modify
11+
* it under the terms of the GNU Affero General Public License as
12+
* published by the Free Software Foundation, either version 3 of the
13+
* License, or (at your option) any later version.
14+
*
15+
* This program is distributed in the hope that it will be useful,
16+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
17+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18+
* GNU Affero General Public License for more details.
19+
*
20+
* You should have received a copy of the GNU Affero General Public License
21+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
22+
*
23+
*/
24+
25+
namespace OCA\Text\DAV;
26+
27+
use OC\Files\Node\File;
28+
use OC\Files\Node\Folder;
29+
use OCA\DAV\Connector\Sabre\Directory;
30+
use OCA\DAV\Files\FilesHome;
31+
use OCA\Text\Service\WorkspaceService;
32+
use OCP\Files\IRootFolder;
33+
use Sabre\DAV\INode;
34+
use Sabre\DAV\PropFind;
35+
use Sabre\DAV\Server;
36+
use Sabre\DAV\ServerPlugin;
37+
38+
class WorkspacePlugin extends ServerPlugin {
39+
40+
public const WORKSPACE = '{http://nextcloud.org/ns}rich-workspace';
41+
42+
/** @var Server */
43+
private $server;
44+
45+
/** @var WorkspaceService */
46+
private $workspaceService;
47+
48+
/** @var IRootFolder */
49+
private $rootFolder;
50+
51+
/** @var string|null */
52+
private $userId;
53+
54+
public function __construct(WorkspaceService $workspaceService, IRootFolder $rootFolder, $userId) {
55+
$this->workspaceService = $workspaceService;
56+
$this->rootFolder = $rootFolder;
57+
$this->userId = $userId;
58+
}
59+
60+
/**
61+
* This initializes the plugin.
62+
*
63+
* This function is called by Sabre\DAV\Server, after
64+
* addPlugin is called.
65+
*
66+
* This method should set up the required event subscriptions.
67+
*
68+
* @param Server $server
69+
* @return void
70+
*/
71+
function initialize(Server $server) {
72+
$this->server = $server;
73+
74+
$this->server->on('propFind', [$this, 'propFind']);
75+
}
76+
77+
78+
public function propFind(PropFind $propFind, INode $node) {
79+
if (!$node instanceof Directory && !$node instanceof FilesHome) {
80+
return;
81+
}
82+
83+
$propFind->handle(self::WORKSPACE, function () use ($node) {
84+
/** @var Folder[] $nodes */
85+
$nodes = $this->rootFolder->getUserFolder($this->userId)->getById($node->getId());
86+
if (count($nodes) > 0) {
87+
/** @var File $file */
88+
$file = $this->workspaceService->getFile($nodes[0]);
89+
if ($file instanceof File) {
90+
return $file->getContent();
91+
}
92+
}
93+
});
94+
95+
}
96+
97+
}

lib/Service/WorkspaceService.php

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
4+
namespace OCA\Text\Service;
5+
6+
7+
use OCP\Files\Folder;
8+
use OCP\Files\NotFoundException;
9+
10+
class WorkspaceService {
11+
12+
private const SUPPORTED_FILENAMES = [
13+
'README.md',
14+
'Readme.md',
15+
'readme.md'
16+
];
17+
18+
public function getFile(Folder $folder) {
19+
$file = null;
20+
foreach (self::SUPPORTED_FILENAMES as $filename) {
21+
if ($folder->nodeExists($filename)) {
22+
try {
23+
$file = $folder->get($filename);
24+
} catch (NotFoundException $e) {
25+
}
26+
continue;
27+
}
28+
}
29+
return $file;
30+
}
31+
}

0 commit comments

Comments
 (0)