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

LibreSign / libresign / 3717593689

pending completion
3717593689

Pull #1287

github

GitHub
Merge 42fe701e4 into ff8c1edaf
Pull Request #1287: [stable25] Bump packages

2548 of 4384 relevant lines covered (58.12%)

4.74 hits per line

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

62.65
/lib/Controller/FileController.php
1
<?php
2

3
namespace OCA\Libresign\Controller;
4

5
use OCA\Libresign\AppInfo\Application;
6
use OCA\Libresign\Exception\LibresignException;
7
use OCA\Libresign\Helper\JSActions;
8
use OCA\Libresign\Helper\ValidateHelper;
9
use OCA\Libresign\Service\FileService;
10
use OCP\AppFramework\Controller;
11
use OCP\AppFramework\Http;
12
use OCP\AppFramework\Http\DataDisplayResponse;
13
use OCP\AppFramework\Http\JSONResponse;
14
use OCP\IL10N;
15
use OCP\IRequest;
16
use OCP\IUserSession;
17
use Psr\Log\LoggerInterface;
18

19
class FileController extends Controller {
20
        /** @var IL10N */
21
        private $l10n;
22
        /** @var LoggerInterface */
23
        private $logger;
24
        /** @var IUserSession */
25
        private $userSession;
26
        /** @var FileService */
27
        private $fileService;
28
        /** @var ValidateHelper */
29
        private $validateHelper;
30

31
        public function __construct(
32
                IRequest $request,
33
                IL10N $l10n,
34
                LoggerInterface $logger,
35
                IUserSession $userSession,
36
                FileService $fileService,
37
                ValidateHelper $validateHelper
38
        ) {
39
                parent::__construct(Application::APP_ID, $request);
6✔
40
                $this->l10n = $l10n;
6✔
41
                $this->logger = $logger;
6✔
42
                $this->userSession = $userSession;
6✔
43
                $this->fileService = $fileService;
6✔
44
                $this->validateHelper = $validateHelper;
6✔
45
        }
46

47
        /**
48
         * @NoAdminRequired
49
         * @NoCSRFRequired
50
         * @PublicPage
51
         *
52
         * @return JSONResponse
53
         */
54
        public function validateUuid($uuid): JSONResponse {
55
                return $this->validate('Uuid', $uuid);
3✔
56
        }
57

58
        /**
59
         * @NoAdminRequired
60
         * @NoCSRFRequired
61
         * @PublicPage
62
         *
63
         * @return JSONResponse
64
         */
65
        public function validateFileId($fileId): JSONResponse {
66
                return $this->validate('FileId', $fileId);
1✔
67
        }
68

69
        private function validate(string $type, $identifier): JSONResponse {
70
                try {
71
                        $this->fileService->setFileByType($type, $identifier);
4✔
72
                        $return['success'] = true;
2✔
73
                        $statusCode = Http::STATUS_OK;
2✔
74
                } catch (LibresignException $e) {
2✔
75
                        $message = $this->l10n->t($e->getMessage());
2✔
76
                        $return = [
2✔
77
                                'success' => false,
2✔
78
                                'action' => JSActions::ACTION_DO_NOTHING,
2✔
79
                                'errors' => [$message]
2✔
80
                        ];
2✔
81
                        $statusCode = $e->getCode() ?? Http::STATUS_UNPROCESSABLE_ENTITY;
2✔
82
                } catch (\Throwable $th) {
×
83
                        $message = $this->l10n->t($th->getMessage());
×
84
                        $this->logger->error($message);
×
85
                        $return = [
×
86
                                'success' => false,
×
87
                                'action' => JSActions::ACTION_DO_NOTHING,
×
88
                                'errors' => [$message]
×
89
                        ];
×
90
                        $statusCode = $th->getCode() ?? Http::STATUS_UNPROCESSABLE_ENTITY;
×
91
                }
92

93
                $return = array_merge($return,
4✔
94
                        $this->fileService
4✔
95
                                ->setMe($this->userSession->getUser())
4✔
96
                                ->showVisibleElements()
4✔
97
                                ->showPages()
4✔
98
                                ->showSigners()
4✔
99
                                ->showSettings()
4✔
100
                                ->showMessages()
4✔
101
                                ->formatFile()
4✔
102
                );
4✔
103

104
                return new JSONResponse($return, $statusCode);
4✔
105
        }
106

107
        /**
108
         * @NoAdminRequired
109
         * @NoCSRFRequired
110
         */
111
        public function list($page = null, $length = null): JSONResponse {
112
                $return = $this->fileService->listAssociatedFilesOfSignFlow($this->userSession->getUser(), $page, $length);
1✔
113
                return new JSONResponse($return, Http::STATUS_OK);
1✔
114
        }
115

116
        /**
117
         * @NoAdminRequired
118
         *
119
         * @NoCSRFRequired
120
         *
121
         * @return DataDisplayResponse|JSONResponse
122
         */
123
        public function getPage(string $uuid, int $page) {
124
                try {
125
                        $page = $this->fileService->getPage($uuid, $page, $this->userSession->getUser()->getUID());
×
126
                        return new DataDisplayResponse(
×
127
                                $page,
×
128
                                Http::STATUS_OK,
×
129
                                ['Content-Type' => 'image/png']
×
130
                        );
×
131
                } catch (\Throwable $th) {
×
132
                        $this->logger->error($th->getMessage());
×
133
                        $return = [
×
134
                                'success' => false,
×
135
                                'errors' => [$th->getMessage()]
×
136
                        ];
×
137
                        $statusCode = $th->getCode() > 0 ? $th->getCode() : Http::STATUS_NOT_FOUND;
×
138
                        return new JSONResponse($return, $statusCode);
×
139
                }
140
        }
141

142

143
        /**
144
         * @NoAdminRequired
145
         * @NoCSRFRequired
146
         * @return JSONResponse
147
         */
148
        public function save(string $name, array $file, array $settings = []): JSONResponse {
149
                try {
150
                        if (empty($name)) {
1✔
151
                                throw new \Exception($this->l10n->t('Name is mandatory'));
×
152
                        }
153
                        $this->validateHelper->validateNewFile(['file' => $file]);
1✔
154
                        $this->validateHelper->canRequestSign($this->userSession->getUser());
1✔
155

156
                        $node = $this->fileService->getNodeFromData([
1✔
157
                                'userManager' => $this->userSession->getUser(),
1✔
158
                                'name' => $name,
1✔
159
                                'file' => $file,
1✔
160
                                'settings' => $settings
1✔
161
                        ]);
1✔
162

163
                        return new JSONResponse(
1✔
164
                                [
1✔
165
                                        'message' => $this->l10n->t('Success'),
1✔
166
                                        'name' => $name,
1✔
167
                                        'id' => $node->getId(),
1✔
168
                                        'etag' => $node->getEtag(),
1✔
169
                                        'path' => $node->getPath(),
1✔
170
                                        'type' => $node->getType(),
1✔
171
                                ],
1✔
172
                                Http::STATUS_OK
1✔
173
                        );
1✔
174
                } catch (\Exception $e) {
×
175
                        return new JSONResponse(
×
176
                                [
×
177
                                        'message' => $e->getMessage(),
×
178
                                ],
×
179
                                Http::STATUS_UNPROCESSABLE_ENTITY,
×
180
                        );
×
181
                }
182
        }
183
}
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

© 2025 Coveralls, Inc