-
-
Notifications
You must be signed in to change notification settings - Fork 111
Expand file tree
/
Copy pathEnvelopeService.php
More file actions
130 lines (104 loc) · 3.68 KB
/
EnvelopeService.php
File metadata and controls
130 lines (104 loc) · 3.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
<?php
declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2025 LibreCode coop and contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace OCA\Libresign\Service;
use DateTime;
use OCA\Libresign\AppInfo\Application;
use OCA\Libresign\Db\File as FileEntity;
use OCA\Libresign\Db\FileMapper;
use OCA\Libresign\Enum\NodeType;
use OCA\Libresign\Exception\LibresignException;
use OCP\AppFramework\Db\DoesNotExistException;
use OCP\IAppConfig;
use OCP\IL10N;
use Sabre\DAV\UUIDUtil;
class EnvelopeService {
public function __construct(
protected FileMapper $fileMapper,
protected IL10N $l10n,
protected IAppConfig $appConfig,
protected FolderService $folderService,
) {
}
public function isEnabled(): bool {
return $this->appConfig->getValueBool(Application::APP_ID, 'envelope_enabled', true);
}
/**
* @throws LibresignException
*/
public function validateEnvelopeConstraints(int $fileCount): void {
if (!$this->isEnabled()) {
throw new LibresignException($this->l10n->t('Envelope feature is disabled'));
}
$maxFiles = $this->getMaxFilesPerEnvelope();
if ($fileCount > $maxFiles) {
throw new LibresignException(
$this->l10n->t('Maximum number of files per envelope (%s) exceeded', [$maxFiles])
);
}
}
public function createEnvelope(string $name, string $userId, int $filesCount = 0): FileEntity {
$this->folderService->setUserId($userId);
$parentFolder = $this->folderService->getFolder();
$uuid = UUIDUtil::getUUID();
$folderName = $name . '_' . $uuid;
$envelopeFolder = $parentFolder->newFolder($folderName);
$envelope = new FileEntity();
$envelope->setNodeId($envelopeFolder->getId());
$envelope->setNodeTypeEnum(NodeType::ENVELOPE);
$envelope->setName($name);
$envelope->setUuid($uuid);
$envelope->setCreatedAt(new DateTime());
$envelope->setStatus(FileEntity::STATUS_DRAFT);
$envelope->setMetadata(['filesCount' => $filesCount]);
if ($userId) {
$envelope->setUserId($userId);
}
return $this->fileMapper->insert($envelope);
}
public function addFileToEnvelope(int $envelopeId, FileEntity $file): FileEntity {
$envelope = $this->fileMapper->getById($envelopeId);
if (!$envelope->isEnvelope()) {
throw new LibresignException($this->l10n->t('The specified ID is not an envelope'));
}
if ($envelope->getStatus() > FileEntity::STATUS_DRAFT) {
throw new LibresignException($this->l10n->t('Cannot add files to an envelope that is already in signing process'));
}
$maxFiles = $this->getMaxFilesPerEnvelope();
$currentCount = $this->fileMapper->countChildrenFiles($envelopeId);
if ($currentCount >= $maxFiles) {
throw new LibresignException(
$this->l10n->t('Maximum number of files per envelope (%s) exceeded', [$maxFiles])
);
}
$file->setParentFileId($envelopeId);
$file->setNodeTypeEnum(NodeType::FILE);
return $this->fileMapper->update($file);
}
public function getEnvelopeByFileId(int $fileId): ?FileEntity {
try {
return $this->fileMapper->getParentEnvelope($fileId);
} catch (DoesNotExistException) {
return null;
}
}
public function getEnvelopeFolder(FileEntity $envelope): \OCP\Files\Folder {
$userId = $envelope->getUserId();
if (!$userId) {
throw new LibresignException('Envelope does not have a user');
}
$this->folderService->setUserId($userId);
$userFolder = $this->folderService->getFolder();
$envelopeFolderNode = $userFolder->getFirstNodeById($envelope->getNodeId());
if (!$envelopeFolderNode instanceof \OCP\Files\Folder) {
throw new LibresignException('Envelope folder not found');
}
return $envelopeFolderNode;
}
private function getMaxFilesPerEnvelope(): int {
return $this->appConfig->getValueInt(Application::APP_ID, 'envelope_max_files', 50);
}
}