-
Notifications
You must be signed in to change notification settings - Fork 120
Expand file tree
/
Copy pathWorkspacePlugin.php
More file actions
143 lines (123 loc) Β· 4.19 KB
/
WorkspacePlugin.php
File metadata and controls
143 lines (123 loc) Β· 4.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
<?php
declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2019 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace OCA\Text\DAV;
use Exception;
use OC\Files\Node\File;
use OCA\DAV\Connector\Sabre\Directory;
use OCA\DAV\Files\FilesHome;
use OCA\Text\AppInfo\Application;
use OCA\Text\Service\WorkspaceService;
use OCP\Files\GenericFileException;
use OCP\Files\IRootFolder;
use OCP\Files\NotPermittedException;
use OCP\ICacheFactory;
use OCP\IConfig;
use OCP\Lock\LockedException;
use Psr\Log\LoggerInterface;
use Sabre\DAV\INode;
use Sabre\DAV\PropFind;
use Sabre\DAV\Server;
use Sabre\DAV\ServerPlugin;
class WorkspacePlugin extends ServerPlugin {
public const WORKSPACE_PROPERTY = '{http://nextcloud.org/ns}rich-workspace';
public const WORKSPACE_FILE_PROPERTY = '{http://nextcloud.org/ns}rich-workspace-file';
public const WORKSPACE_PROPERTY_FLAT = '{http://nextcloud.org/ns}rich-workspace-flat';
public const WORKSPACE_FILE_PROPERTY_FLAT = '{http://nextcloud.org/ns}rich-workspace-file-flat';
private Server $server;
public function __construct(
private WorkspaceService $workspaceService,
private IRootFolder $rootFolder,
private ICacheFactory $cacheFactory,
private IConfig $config,
private LoggerInterface $logger,
private ?string $userId,
) {
}
/**
* This initializes the plugin.
*
* This function is called by Sabre\DAV\Server, after
* addPlugin is called.
*
* This method should set up the required event subscriptions.
*
* @param Server $server
* @return void
*/
public function initialize(Server $server) {
$this->server = $server;
$this->server->on('propFind', [$this, 'propFind']);
}
public function propFind(PropFind $propFind, INode $node) {
if (!array_intersect([
self::WORKSPACE_PROPERTY,
self::WORKSPACE_FILE_PROPERTY,
self::WORKSPACE_PROPERTY_FLAT,
self::WORKSPACE_FILE_PROPERTY_FLAT
], $propFind->getRequestedProperties())) {
return;
}
if (!$node instanceof Directory && !$node instanceof FilesHome) {
return;
}
$workspaceAvailable = $this->config->getAppValue(Application::APP_NAME, 'workspace_available', '1') === '1';
$workspaceEnabled = $this->config->getUserValue($this->userId, Application::APP_NAME, 'workspace_enabled', '1') === '1';
if (!$workspaceAvailable || !$workspaceEnabled) {
return;
}
$shouldFetchChildren = array_intersect([
self::WORKSPACE_PROPERTY,
self::WORKSPACE_FILE_PROPERTY,
], $propFind->getRequestedProperties());
// In most cases we only need the workspace property for the root node
// So we can skip the propFind for further nodes for performance reasons
// Fetching the workspace property for all children is still required for mobile apps
if ($propFind->getDepth() !== $this->server->getHTTPDepth() && !$shouldFetchChildren) {
return;
}
$node = $node->getNode();
try {
$file = $this->workspaceService->getFile($node);
} catch (\Exception $e) {
$file = null;
}
$workspaceContentCallback = function () use ($file) {
$cachedContent = '';
if ($file instanceof File) {
$cache = $this->cacheFactory->createDistributed('text_workspace');
$cacheKey = $file->getFileInfo()->getId() . '_' . $file->getFileInfo()->getEtag();
if (($cachedContent = $cache->get($cacheKey)) !== null) {
return $cachedContent;
}
try {
$cachedContent = $file->getContent();
$cache->set($cacheKey, $cachedContent, 3600);
} catch (GenericFileException|NotPermittedException|LockedException $e) {
// Ignore but log when debugging
$this->logger->debug($e->getMessage(), [
'exception' => $e,
]);
} catch (Exception $e) {
$this->logger->error($e->getMessage(), [
'exception' => $e,
]);
}
}
return $cachedContent;
};
$workspaceFileCallback = function () use ($file) {
if ($file instanceof File) {
return $file->getFileInfo()->getId();
}
return '';
};
$propFind->handle(self::WORKSPACE_PROPERTY, $workspaceContentCallback);
$propFind->handle(self::WORKSPACE_PROPERTY_FLAT, $workspaceContentCallback);
$propFind->handle(self::WORKSPACE_FILE_PROPERTY, $workspaceFileCallback);
$propFind->handle(self::WORKSPACE_FILE_PROPERTY_FLAT, $workspaceFileCallback);
}
}