-
-
Notifications
You must be signed in to change notification settings - Fork 111
Expand file tree
/
Copy pathFolderService.php
More file actions
203 lines (184 loc) · 5.68 KB
/
FolderService.php
File metadata and controls
203 lines (184 loc) · 5.68 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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
<?php
declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2020-2024 LibreCode coop and contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace OCA\Libresign\Service;
use OCA\Libresign\Exception\LibresignException;
use OCP\AppFramework\Services\IAppConfig;
use OCP\Files\AppData\IAppDataFactory;
use OCP\Files\File;
use OCP\Files\Folder;
use OCP\Files\IAppData;
use OCP\Files\IRootFolder;
use OCP\Files\Node;
use OCP\Files\NotFoundException;
use OCP\Files\NotPermittedException;
use OCP\IGroupManager;
use OCP\IL10N;
use OCP\IUser;
use OCP\Lock\LockedException;
class FolderService {
protected IAppData $appData;
public function __construct(
private IRootFolder $root,
protected IAppDataFactory $appDataFactory,
protected IGroupManager $groupManager,
private IAppConfig $appConfig,
private IL10N $l10n,
private ?string $userId,
) {
$this->userId = $userId;
$this->appData = $appDataFactory->get('libresign');
}
public function setUserId(string $userId): void {
$this->userId = $userId;
}
public function getUserId(): ?string {
return $this->userId;
}
/**
* Get folder for user and creates it if non-existent
*
* @psalm-suppress MixedReturnStatement
* @throws NotFoundException
* @throws NotPermittedException
* @throws LockedException
*/
public function getFolder(): Folder {
$path = $this->getLibreSignDefaultPath();
$containerFolder = $this->getContainerFolder();
try {
/** @var Folder $folder */
$folder = $containerFolder->get($path);
} catch (NotFoundException) {
/** @var Folder $folder */
$folder = $containerFolder->newFolder($path);
}
return $folder;
}
/**
* @throws NotFoundException
*/
public function getFileById(?int $nodeId = null): File {
if ($this->getUserId()) {
$file = $this->root->getUserFolder($this->getUserId())->getFirstNodeById($nodeId);
if ($file instanceof File) {
return $file;
}
}
$path = $this->getLibreSignDefaultPath();
$containerFolder = $this->getContainerFolder();
try {
/** @var Folder $folder */
$folder = $containerFolder->get($path);
} catch (NotFoundException) {
throw new NotFoundException('Invalid node');
}
$file = $folder->getFirstNodeById($nodeId);
if (!$file instanceof File) {
throw new NotFoundException('Invalid node');
}
return $file;
}
protected function getContainerFolder(): Folder {
if ($this->getUserId() && !$this->groupManager->isInGroup($this->getUserId(), 'guest_app')) {
$containerFolder = $this->root->getUserFolder($this->getUserId());
if ($containerFolder->isUpdateable()) {
return $containerFolder;
}
}
$containerFolder = $this->appData->getFolder('/');
$reflection = new \ReflectionClass($containerFolder);
$reflectionProperty = $reflection->getProperty('folder');
return $reflectionProperty->getValue($containerFolder);
}
private function getLibreSignDefaultPath(): string {
if (!$this->userId) {
return 'unauthenticated';
}
// TODO: retrieve guest group name from app once exposed
if ($this->groupManager->isInGroup($this->getUserId(), 'guest_app')) {
return 'guest_app/' . $this->getUserId();
}
$path = $this->appConfig->getUserValue($this->userId, 'folder');
if (empty($path)) {
$defaultFolder = $this->appConfig->getAppValueString('default_user_folder', 'LibreSign');
$path = '/' . $defaultFolder;
$this->appConfig->setUserValue($this->userId, 'folder', $path);
}
return $path;
}
/**
* Get or create the folder where a file should be stored
*
* @param array $data Must contain 'settings' and optionally 'name', 'userManager'
* @param mixed $identifier User or string identifier
* @return Folder The folder where files should be created
* @throws LibresignException
*/
public function getFolderForFile(array $data, $identifier): Folder {
$userFolder = $this->getFolder();
if (isset($data['settings']['envelopeFolderId'])) {
$envelopeFolder = $userFolder->getFirstNodeById($data['settings']['envelopeFolderId']);
if ($envelopeFolder === null || !$envelopeFolder instanceof Folder) {
throw new LibresignException($this->l10n->t('Envelope folder not found'));
}
return $envelopeFolder;
}
$folderName = $this->getFolderName($data, $identifier);
return $userFolder->newFolder($folderName);
}
/**
* @param array{settings: array, name: string} $data
* @param IUser $owner
*/
public function getFolderName(array $data, $identifier): string {
if (isset($data['settings']['folderName'])) {
return $data['settings']['folderName'];
}
if (!isset($data['settings']['folderPatterns'])) {
$data['settings']['separator'] = '_';
$data['settings']['folderPatterns'][] = [
'name' => 'date',
'setting' => 'Y-m-d\TH-i-s-u'
];
$data['settings']['folderPatterns'][] = [
'name' => 'name'
];
$data['settings']['folderPatterns'][] = [
'name' => 'userId'
];
}
$folderName = [];
foreach ($data['settings']['folderPatterns'] as $pattern) {
switch ($pattern['name']) {
case 'date':
$folderName[] = (new \DateTime('now', new \DateTimeZone('UTC')))->format($pattern['setting']);
break;
case 'name':
if (!empty($data['name'])) {
$folderName[] = $data['name'];
}
break;
case 'userId':
if ($identifier instanceof \OCP\IUser) {
$folderName[] = $identifier->getUID();
} else {
$folderName[] = $identifier;
}
break;
}
}
return implode($data['settings']['separator'], $folderName);
}
public function getFileByPath(string $path): Node {
$userFolder = $this->root->getUserFolder($this->getUserId());
try {
return $userFolder->get($path);
} catch (NotFoundException) {
throw new LibresignException($this->l10n->t('Invalid data to validate file'), 404);
}
}
}