|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | +/** |
| 5 | + * SPDX-FileCopyrightText: 2025 LibreCode coop and contributors |
| 6 | + * SPDX-License-Identifier: AGPL-3.0-or-later |
| 7 | + */ |
| 8 | + |
| 9 | +namespace OCA\Libresign\Service\Envelope; |
| 10 | + |
| 11 | +use DateTime; |
| 12 | +use OCA\Libresign\AppInfo\Application; |
| 13 | +use OCA\Libresign\Db\File as FileEntity; |
| 14 | +use OCA\Libresign\Db\FileMapper; |
| 15 | +use OCA\Libresign\Enum\NodeType; |
| 16 | +use OCA\Libresign\Exception\LibresignException; |
| 17 | +use OCA\Libresign\Service\FolderService; |
| 18 | +use OCP\AppFramework\Db\DoesNotExistException; |
| 19 | +use OCP\IAppConfig; |
| 20 | +use OCP\IL10N; |
| 21 | +use Sabre\DAV\UUIDUtil; |
| 22 | + |
| 23 | +class EnvelopeService { |
| 24 | + public function __construct( |
| 25 | + protected FileMapper $fileMapper, |
| 26 | + protected IL10N $l10n, |
| 27 | + protected IAppConfig $appConfig, |
| 28 | + protected FolderService $folderService, |
| 29 | + ) { |
| 30 | + } |
| 31 | + |
| 32 | + public function isEnabled(): bool { |
| 33 | + return $this->appConfig->getValueBool(Application::APP_ID, 'envelope_enabled', true); |
| 34 | + } |
| 35 | + |
| 36 | + /** |
| 37 | + * @throws LibresignException |
| 38 | + */ |
| 39 | + public function validateEnvelopeConstraints(int $fileCount): void { |
| 40 | + if (!$this->isEnabled()) { |
| 41 | + throw new LibresignException($this->l10n->t('Envelope feature is disabled')); |
| 42 | + } |
| 43 | + |
| 44 | + $maxFiles = $this->getMaxFilesPerEnvelope(); |
| 45 | + if ($fileCount > $maxFiles) { |
| 46 | + throw new LibresignException( |
| 47 | + $this->l10n->t('Maximum number of files per envelope (%s) exceeded', [$maxFiles]) |
| 48 | + ); |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + public function createEnvelope( |
| 53 | + string $name, |
| 54 | + string $userId, |
| 55 | + int $filesCount = 0, |
| 56 | + ?string $path = null, |
| 57 | + ): FileEntity { |
| 58 | + $this->folderService->setUserId($userId); |
| 59 | + |
| 60 | + $uuid = UUIDUtil::getUUID(); |
| 61 | + if ($path) { |
| 62 | + $envelopeFolder = $this->folderService->getOrCreateFolderByAbsolutePath($path); |
| 63 | + } else { |
| 64 | + $parentFolder = $this->folderService->getFolder(); |
| 65 | + $folderName = $name . '_' . $uuid; |
| 66 | + $envelopeFolder = $parentFolder->newFolder($folderName); |
| 67 | + } |
| 68 | + |
| 69 | + $envelope = new FileEntity(); |
| 70 | + $envelope->setNodeId($envelopeFolder->getId()); |
| 71 | + $envelope->setNodeTypeEnum(NodeType::ENVELOPE); |
| 72 | + $envelope->setName($name); |
| 73 | + $envelope->setUuid($uuid); |
| 74 | + $envelope->setCreatedAt(new DateTime()); |
| 75 | + $envelope->setStatus(FileEntity::STATUS_DRAFT); |
| 76 | + |
| 77 | + $envelope->setMetadata(['filesCount' => $filesCount]); |
| 78 | + |
| 79 | + if ($userId) { |
| 80 | + $envelope->setUserId($userId); |
| 81 | + } |
| 82 | + |
| 83 | + return $this->fileMapper->insert($envelope); |
| 84 | + } |
| 85 | + |
| 86 | + public function addFileToEnvelope(int $envelopeId, FileEntity $file): FileEntity { |
| 87 | + $envelope = $this->fileMapper->getById($envelopeId); |
| 88 | + |
| 89 | + if (!$envelope->isEnvelope()) { |
| 90 | + throw new LibresignException($this->l10n->t('The specified ID is not an envelope')); |
| 91 | + } |
| 92 | + |
| 93 | + if ($envelope->getStatus() > FileEntity::STATUS_DRAFT) { |
| 94 | + throw new LibresignException($this->l10n->t('Cannot add files to an envelope that is already in signing process')); |
| 95 | + } |
| 96 | + |
| 97 | + $maxFiles = $this->getMaxFilesPerEnvelope(); |
| 98 | + $currentCount = $this->fileMapper->countChildrenFiles($envelopeId); |
| 99 | + if ($currentCount >= $maxFiles) { |
| 100 | + throw new LibresignException( |
| 101 | + $this->l10n->t('Maximum number of files per envelope (%s) exceeded', [$maxFiles]) |
| 102 | + ); |
| 103 | + } |
| 104 | + |
| 105 | + $file->setParentFileId($envelopeId); |
| 106 | + $file->setNodeTypeEnum(NodeType::FILE); |
| 107 | + |
| 108 | + return $this->fileMapper->update($file); |
| 109 | + } |
| 110 | + |
| 111 | + public function getEnvelopeByFileId(int $fileId): ?FileEntity { |
| 112 | + try { |
| 113 | + return $this->fileMapper->getParentEnvelope($fileId); |
| 114 | + } catch (DoesNotExistException) { |
| 115 | + return null; |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + public function getEnvelopeFolder(FileEntity $envelope): \OCP\Files\Folder { |
| 120 | + $userId = $envelope->getUserId(); |
| 121 | + if (!$userId) { |
| 122 | + throw new LibresignException('Envelope does not have a user'); |
| 123 | + } |
| 124 | + |
| 125 | + $this->folderService->setUserId($userId); |
| 126 | + $userRootFolder = $this->folderService->getUserRootFolder(); |
| 127 | + |
| 128 | + $envelopeFolderNode = $userRootFolder->getFirstNodeById($envelope->getNodeId()); |
| 129 | + if (!$envelopeFolderNode instanceof \OCP\Files\Folder) { |
| 130 | + throw new LibresignException('Envelope folder not found'); |
| 131 | + } |
| 132 | + |
| 133 | + return $envelopeFolderNode; |
| 134 | + } |
| 135 | + |
| 136 | + private function getMaxFilesPerEnvelope(): int { |
| 137 | + return $this->appConfig->getValueInt(Application::APP_ID, 'envelope_max_files', 50); |
| 138 | + } |
| 139 | +} |
0 commit comments