-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathMessageDocumentsController.php
246 lines (211 loc) · 8.61 KB
/
MessageDocumentsController.php
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
<?php
// This file is part of Bileto.
// Copyright 2022-2025 Probesys
// SPDX-License-Identifier: AGPL-3.0-or-later
namespace App\Controller;
use App\Entity\MessageDocument;
use App\Repository\MessageDocumentRepository;
use App\Service\MessageDocumentStorage;
use App\Service\MessageDocumentStorageError;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Contracts\Translation\TranslatorInterface;
/**
* Allow to upload and download MessageDocuments.
*
* @see docs/developers/document-upload.md
*/
class MessageDocumentsController extends BaseController
{
#[Route('/messages/documents/new', name: 'create message document', methods: ['POST'])]
public function create(
Request $request,
MessageDocumentRepository $messageDocumentRepository,
MessageDocumentStorage $messageDocumentStorage,
TranslatorInterface $translator,
): Response {
$this->denyAccessUnlessGranted('orga:create:tickets:messages', 'any');
$file = $request->files->get('document');
/** @var string */
$csrfToken = $request->request->get('_csrf_token', '');
if (!$this->isCsrfTokenValid('create message document', $csrfToken)) {
return new JsonResponse([
'error' => $translator->trans('csrf.invalid', [], 'errors'),
], Response::HTTP_BAD_REQUEST);
}
if (!($file instanceof UploadedFile)) {
return new JsonResponse([
'error' => $translator->trans('message_document.required', [], 'errors'),
], Response::HTTP_BAD_REQUEST);
}
if (
$file->getError() === \UPLOAD_ERR_INI_SIZE ||
$file->getError() === \UPLOAD_ERR_FORM_SIZE
) {
return new JsonResponse([
'error' => $translator->trans('message_document.too_large', [], 'errors'),
], Response::HTTP_BAD_REQUEST);
}
if (!$file->isValid()) {
return new JsonResponse([
'error' => $translator->trans('message_document.server_error', [], 'errors'),
'description' => $file->getErrorMessage(),
], Response::HTTP_BAD_REQUEST);
}
try {
$messageDocument = $messageDocumentStorage->store($file, $file->getClientOriginalName());
} catch (MessageDocumentStorageError $e) {
if ($e->getCode() === MessageDocumentStorageError::REJECTED_MIMETYPE) {
return new JsonResponse([
'error' => $translator->trans('message_document.mimetype.rejected', [], 'errors'),
'description' => $e->getMessage(),
], Response::HTTP_BAD_REQUEST);
} else {
return new JsonResponse([
'error' => $translator->trans('message_document.server_error', [], 'errors'),
'description' => $e->getMessage(),
], Response::HTTP_INTERNAL_SERVER_ERROR);
}
}
$messageDocumentRepository->save($messageDocument, true);
$urlShow = $this->generateUrl(
'message document',
[
'uid' => $messageDocument->getUid(),
'extension' => $messageDocument->getExtension(),
],
UrlGeneratorInterface::ABSOLUTE_URL,
);
$urlDelete = $this->generateUrl(
'delete message document',
[
'uid' => $messageDocument->getUid(),
],
);
return new JsonResponse([
'uid' => $messageDocument->getUid(),
'urlShow' => $urlShow,
]);
}
#[Route(
'/messages/documents/{uid:messageDocument}.{extension}',
name: 'message document',
methods: ['GET', 'HEAD']
)]
public function show(
MessageDocument $messageDocument,
string $extension,
MessageDocumentStorage $messageDocumentStorage,
): Response {
/** @var \App\Entity\User $user */
$user = $this->getUser();
$message = $messageDocument->getMessage();
if (!$messageDocument->isCreatedBy($user)) {
if ($message === null) {
// The message of the document is not posted yet, only its author
// can see it.
throw $this->createAccessDeniedException();
} else {
// The message of the document is posted, check that the user has
// the permissions to see the message.
$ticket = $message->getTicket();
if ($message->isConfidential()) {
$this->denyAccessUnlessGranted('orga:see:tickets:messages:confidential', $ticket);
} else {
$this->denyAccessUnlessGranted('orga:see', $ticket);
}
}
}
// The extension parameter is only decorative, but at least check that
// it corresponds to the real extension!
if ($extension !== $messageDocument->getExtension()) {
throw $this->createNotFoundException('The file does not exist.');
}
try {
$content = $messageDocumentStorage->read($messageDocument);
$contentLength = $messageDocumentStorage->size($messageDocument);
} catch (MessageDocumentStorageError $e) {
throw $this->createNotFoundException('The file does not exist.');
}
$name = rawurlencode($messageDocument->getName());
$mimetype = $messageDocument->getMimetype();
if (str_starts_with($mimetype, 'image/') && $mimetype !== 'image/svg+xml') {
$contentDisposition = "inline; filename=\"{$name}\"";
} else {
$contentDisposition = "attachment; filename=\"{$name}\"";
}
return new Response(
$content,
Response::HTTP_OK,
[
'Content-Disposition' => $contentDisposition,
'Content-Length' => $contentLength,
'Content-Type' => $mimetype,
]
);
}
#[Route('/messages/documents/{uid:messageDocument}/deletion', name: 'delete message document', methods: ['POST'])]
public function delete(
MessageDocument $messageDocument,
Request $request,
MessageDocumentRepository $messageDocumentRepository,
MessageDocumentStorage $messageDocumentStorage,
TranslatorInterface $translator,
): Response {
/** @var \App\Entity\User $user */
$user = $this->getUser();
/** @var string $csrfToken */
$csrfToken = $request->request->get('_csrf_token', '');
if (!$messageDocument->isCreatedBy($user)) {
throw $this->createAccessDeniedException();
}
if (!$this->isCsrfTokenValid('delete message document', $csrfToken)) {
return new JsonResponse([
'error' => $translator->trans('csrf.invalid', [], 'errors'),
], Response::HTTP_BAD_REQUEST);
}
$messageDocumentRepository->remove($messageDocument, true);
$countSameHashDocuments = $messageDocumentRepository->countByHash($messageDocument->getHash());
if ($countSameHashDocuments === 0) {
$messageDocumentStorage->remove($messageDocument);
}
$urlShow = $this->generateUrl(
'message document',
[
'uid' => $messageDocument->getUid(),
'extension' => $messageDocument->getExtension(),
],
UrlGeneratorInterface::ABSOLUTE_URL,
);
return new JsonResponse([
'uid' => $messageDocument->getUid(),
'urlShow' => $urlShow,
]);
}
#[Route('/messages/documents', name: 'message documents', methods: ['GET', 'HEAD'])]
public function index(
Request $request,
MessageDocumentRepository $messageDocumentRepository,
): Response {
/** @var \App\Entity\User $user */
$user = $this->getUser();
$conditions = [
'createdBy' => $user,
];
$filter = $request->query->get('filter', '');
if ($filter === 'unattached') {
$conditions['message'] = null;
}
$messageDocuments = $messageDocumentRepository->findBy(
$conditions,
['createdAt' => 'ASC'],
);
return $this->render('message_documents/index.html.twig', [
'messageDocuments' => $messageDocuments,
]);
}
}