• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

LibreSign / libresign / 19868313373

02 Dec 2025 05:50PM UTC coverage: 41.522%. First build
19868313373

Pull #4464

github

web-flow
Merge f1243a78b into 198609762
Pull Request #4464: refactor: attach document

89 of 246 new or added lines in 14 files covered. (36.18%)

5103 of 12290 relevant lines covered (41.52%)

4.18 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

73.33
/lib/Service/FolderService.php
1
<?php
2

3
declare(strict_types=1);
4
/**
5
 * SPDX-FileCopyrightText: 2020-2024 LibreCode coop and contributors
6
 * SPDX-License-Identifier: AGPL-3.0-or-later
7
 */
8

9
namespace OCA\Libresign\Service;
10

11
use OCA\Libresign\Exception\LibresignException;
12
use OCP\AppFramework\Services\IAppConfig;
13
use OCP\Files\AppData\IAppDataFactory;
14
use OCP\Files\File;
15
use OCP\Files\Folder;
16
use OCP\Files\IAppData;
17
use OCP\Files\IRootFolder;
18
use OCP\Files\Node;
19
use OCP\Files\NotFoundException;
20
use OCP\Files\NotPermittedException;
21
use OCP\IGroupManager;
22
use OCP\IL10N;
23
use OCP\IUser;
24
use OCP\Lock\LockedException;
25

26
class FolderService {
27
        protected IAppData $appData;
28
        public function __construct(
29
                private IRootFolder $root,
30
                protected IAppDataFactory $appDataFactory,
31
                protected IGroupManager $groupManager,
32
                private IAppConfig $appConfig,
33
                private IL10N $l10n,
34
                private ?string $userId,
35
        ) {
36
                $this->userId = $userId;
60✔
37
                $this->appData = $appDataFactory->get('libresign');
60✔
38
        }
39

40
        public function setUserId(string $userId): void {
41
                $this->userId = $userId;
14✔
42
        }
43

44
        public function getUserId(): ?string {
45
                return $this->userId;
17✔
46
        }
47

48
        /**
49
         * Get folder for user and creates it if non-existent
50
         *
51
         * @psalm-suppress MixedReturnStatement
52
         * @throws NotFoundException
53
         * @throws NotPermittedException
54
         * @throws LockedException
55
         */
56
        public function getFolder(): Folder {
57
                $path = $this->getLibreSignDefaultPath();
16✔
58
                $containerFolder = $this->getContainerFolder();
16✔
59
                try {
60
                        /** @var Folder $folder */
61
                        $folder = $containerFolder->get($path);
16✔
62
                } catch (NotFoundException) {
16✔
63
                        /** @var Folder $folder */
64
                        $folder = $containerFolder->newFolder($path);
16✔
65
                }
66
                return $folder;
16✔
67
        }
68

69
        /**
70
         * @throws NotFoundException
71
         */
72
        public function getFileById(?int $nodeId = null): File {
73
                if ($this->getUserId()) {
×
74

75
                        $file = $this->root->getUserFolder($this->getUserId())->getFirstNodeById($nodeId);
×
76
                        if ($file instanceof File) {
×
77
                                return $file;
×
78
                        }
79
                }
80
                $path = $this->getLibreSignDefaultPath();
×
81
                $containerFolder = $this->getContainerFolder();
×
82
                try {
83
                        /** @var Folder $folder */
84
                        $folder = $containerFolder->get($path);
×
85
                } catch (NotFoundException) {
×
86
                        throw new NotFoundException('Invalid node');
×
87
                }
88
                $file = $folder->getFirstNodeById($nodeId);
×
89
                if (!$file instanceof File) {
×
90
                        throw new NotFoundException('Invalid node');
×
91
                }
92
                return $file;
×
93
        }
94

95
        protected function getContainerFolder(): Folder {
96
                if ($this->getUserId() && !$this->groupManager->isInGroup($this->getUserId(), 'guest_app')) {
17✔
97
                        $containerFolder = $this->root->getUserFolder($this->getUserId());
16✔
98
                        if ($containerFolder->isUpdateable()) {
16✔
99
                                return $containerFolder;
16✔
100
                        }
101
                }
102
                $containerFolder = $this->appData->getFolder('/');
1✔
103
                $reflection = new \ReflectionClass($containerFolder);
1✔
104
                $reflectionProperty = $reflection->getProperty('folder');
1✔
105
                return $reflectionProperty->getValue($containerFolder);
1✔
106
        }
107

108
        private function getLibreSignDefaultPath(): string {
109
                if (!$this->userId) {
16✔
110
                        return 'unauthenticated';
×
111
                }
112
                // TODO: retrieve guest group name from app once exposed
113
                if ($this->groupManager->isInGroup($this->getUserId(), 'guest_app')) {
16✔
114
                        return 'guest_app/' . $this->getUserId();
×
115
                }
116
                $path = $this->appConfig->getUserValue($this->userId, 'folder');
16✔
117

118
                if (empty($path)) {
16✔
119
                        $defaultFolder = $this->appConfig->getAppValueString('default_user_folder', 'LibreSign');
16✔
120
                        $path = '/' . $defaultFolder;
16✔
121
                        $this->appConfig->setUserValue($this->userId, 'folder', $path);
16✔
122
                }
123

124
                return $path;
16✔
125
        }
126

127
        /**
128
         * @param array{settings: array, name: string} $data
129
         * @param IUser $owner
130
         */
131
        public function getFolderName(array $data, $identifier): string {
132
                if (isset($data['settings']['folderName'])) {
27✔
133
                        return $data['settings']['folderName'];
1✔
134
                }
135

136
                if (!isset($data['settings']['folderPatterns'])) {
26✔
137
                        $data['settings']['separator'] = '_';
6✔
138
                        $data['settings']['folderPatterns'][] = [
6✔
139
                                'name' => 'date',
6✔
140
                                'setting' => 'Y-m-d\TH-i-s-u'
6✔
141
                        ];
6✔
142
                        $data['settings']['folderPatterns'][] = [
6✔
143
                                'name' => 'name'
6✔
144
                        ];
6✔
145
                        $data['settings']['folderPatterns'][] = [
6✔
146
                                'name' => 'userId'
6✔
147
                        ];
6✔
148
                }
149
                $folderName = [];
26✔
150
                foreach ($data['settings']['folderPatterns'] as $pattern) {
26✔
151
                        switch ($pattern['name']) {
26✔
152
                                case 'date':
26✔
153
                                        $folderName[] = (new \DateTime('now', new \DateTimeZone('UTC')))->format($pattern['setting']);
22✔
154
                                        break;
22✔
155
                                case 'name':
25✔
156
                                        if (!empty($data['name'])) {
24✔
157
                                                $folderName[] = $data['name'];
22✔
158
                                        }
159
                                        break;
24✔
160
                                case 'userId':
24✔
161
                                        if ($identifier instanceof \OCP\IUser) {
23✔
162
                                                $folderName[] = $identifier->getUID();
23✔
163
                                        } else {
NEW
164
                                                $folderName[] = $identifier;
×
165
                                        }
166
                                        break;
23✔
167
                        }
168
                }
169
                return implode($data['settings']['separator'], $folderName);
26✔
170
        }
171

172
        public function getFileByPath(string $path): Node {
173
                $userFolder = $this->root->getUserFolder($this->getUserId());
×
174
                try {
175
                        return $userFolder->get($path);
×
176
                } catch (NotFoundException) {
×
177
                        throw new LibresignException($this->l10n->t('Invalid data to validate file'), 404);
×
178
                }
179
        }
180
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc