|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
/** |
|
4
|
|
|
* @copyright Copyright (c) 2018 John Molakvoæ <[email protected]> |
|
5
|
|
|
* |
|
6
|
|
|
* @author John Molakvoæ <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* @license GNU AGPL version 3 or any later version |
|
9
|
|
|
* |
|
10
|
|
|
* This program is free software: you can redistribute it and/or modify |
|
11
|
|
|
* it under the terms of the GNU Affero General Public License as |
|
12
|
|
|
* published by the Free Software Foundation, either version 3 of the |
|
13
|
|
|
* License, or (at your option) any later version. |
|
14
|
|
|
* |
|
15
|
|
|
* This program is distributed in the hope that it will be useful, |
|
16
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
17
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
18
|
|
|
* GNU Affero General Public License for more details. |
|
19
|
|
|
* |
|
20
|
|
|
* You should have received a copy of the GNU Affero General Public License |
|
21
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
22
|
|
|
* |
|
23
|
|
|
*/ |
|
24
|
|
|
|
|
25
|
|
|
namespace OCA\Richdocuments\Controller; |
|
26
|
|
|
|
|
27
|
|
|
use OCA\Richdocuments\TemplateManager; |
|
28
|
|
|
use OCP\AppFramework\Controller; |
|
29
|
|
|
use OCP\AppFramework\Http; |
|
30
|
|
|
use OCP\AppFramework\Http\DataResponse; |
|
31
|
|
|
use OCP\AppFramework\Http\FileDisplayResponse; |
|
32
|
|
|
use OCP\AppFramework\Http\JSONResponse; |
|
33
|
|
|
use OCP\AppFramework\Http\NotFoundResponse; |
|
34
|
|
|
use OCP\Files\Node; |
|
35
|
|
|
use OCP\Files\NotFoundException; |
|
36
|
|
|
use OCP\Files\SimpleFS\ISimpleFile; |
|
37
|
|
|
use OCP\IL10N; |
|
38
|
|
|
use OCP\IPreview; |
|
39
|
|
|
use OCP\IRequest; |
|
40
|
|
|
use OC\Files\Filesystem; |
|
41
|
|
|
|
|
42
|
|
|
class TemplatesController extends Controller { |
|
43
|
|
|
/** @var IL10N */ |
|
44
|
|
|
private $l10n; |
|
45
|
|
|
|
|
46
|
|
|
/** @var TemplateManager */ |
|
47
|
|
|
private $manager; |
|
48
|
|
|
|
|
49
|
|
|
/** @var IPreview */ |
|
50
|
|
|
private $preview; |
|
51
|
|
|
|
|
52
|
|
|
/** @var int Max template size */ |
|
53
|
|
|
private $maxSize = 20 * 1024 * 1024; |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Templates controller |
|
57
|
|
|
* |
|
58
|
|
|
* @param string $appName |
|
59
|
|
|
* @param IRequest $request |
|
60
|
|
|
* @param L10N $l10n |
|
61
|
|
|
* @param TemplateManager $manager |
|
62
|
|
|
* @param IPreview $preview |
|
63
|
|
|
*/ |
|
64
|
|
|
public function __construct($appName, |
|
65
|
|
|
IRequest $request, |
|
66
|
|
|
IL10N $l10n, |
|
67
|
|
|
TemplateManager $manager, |
|
68
|
|
|
IPreview $preview) { |
|
69
|
|
|
parent::__construct($appName, $request); |
|
70
|
|
|
|
|
71
|
|
|
$this->appName = $appName; |
|
72
|
|
|
$this->request = $request; |
|
73
|
|
|
$this->l10n = $l10n; |
|
74
|
|
|
$this->manager = $manager; |
|
75
|
|
|
$this->preview = $preview; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* @NoAdminRequired |
|
80
|
|
|
* @NoCSRFRequired |
|
81
|
|
|
* |
|
82
|
|
|
* Get preview for a specific template |
|
83
|
|
|
* |
|
84
|
|
|
* @param int $fileId The template id |
|
85
|
|
|
* @param int $x |
|
86
|
|
|
* @param int $y |
|
87
|
|
|
* @param bool $a |
|
88
|
|
|
* @param bool $forceIcon |
|
89
|
|
|
* @param string $mode |
|
90
|
|
|
* @return DataResponse |
|
91
|
|
|
* @throws NotFoundResponse |
|
92
|
|
|
*/ |
|
93
|
|
|
public function getPreview($fileId, |
|
94
|
|
|
$x = 150, |
|
95
|
|
|
$y = 150, |
|
96
|
|
|
$a = false, |
|
97
|
|
|
$forceIcon = true, |
|
98
|
|
|
$mode = 'fill') { |
|
99
|
|
|
|
|
100
|
|
|
if ($fileId === '' || $x === 0 || $y === 0) { |
|
|
|
|
|
|
101
|
|
|
return new DataResponse([], Http::STATUS_BAD_REQUEST); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
try { |
|
105
|
|
|
$template = $this->manager->get($fileId); |
|
106
|
|
|
} catch (NotFoundException $e) { |
|
|
|
|
|
|
107
|
|
|
return new DataResponse([], Http::STATUS_NOT_FOUND); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
if ($template instanceof ISimpleFile) { |
|
|
|
|
|
|
111
|
|
|
return new DataResponse([], Http::STATUS_NOT_FOUND); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
return $this->fetchPreview($template, $x, $y, $a, $forceIcon, $mode); |
|
|
|
|
|
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* Add a global template |
|
119
|
|
|
* |
|
120
|
|
|
* @return JSONResponse |
|
121
|
|
|
*/ |
|
122
|
|
|
public function add() { |
|
123
|
|
|
$files = $this->request->getUploadedFile('files'); |
|
124
|
|
|
|
|
125
|
|
|
if (!is_null($files)) { |
|
126
|
|
|
if ($files['error'][0] === 0 |
|
127
|
|
|
&& is_uploaded_file($files['tmp_name'][0]) |
|
128
|
|
|
&& !Filesystem::isFileBlacklisted($files['tmp_name'][0])) { |
|
129
|
|
|
|
|
130
|
|
|
// TODO: ensure the size limit is decent for preview |
|
131
|
|
|
if ($files['size'][0] > $this->maxSize) { |
|
132
|
|
|
return new JSONResponse( |
|
133
|
|
|
['data' => ['message' => $this->l10n->t('File is too big')]], |
|
134
|
|
|
Http::STATUS_BAD_REQUEST |
|
135
|
|
|
); |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
$templateName = $files['name'][0]; |
|
139
|
|
|
$templateFile = file_get_contents($files['tmp_name'][0]); |
|
140
|
|
|
|
|
141
|
|
|
unlink($files['tmp_name'][0]); |
|
142
|
|
|
|
|
143
|
|
|
$template = $this->manager->add($templateName, $templateFile); |
|
144
|
|
|
|
|
145
|
|
|
return new JSONResponse( |
|
146
|
|
|
['data' => $template], |
|
147
|
|
|
Http::STATUS_CREATED |
|
148
|
|
|
); |
|
149
|
|
|
} |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
return new JSONResponse( |
|
153
|
|
|
['data' => ['message' => $this->l10n->t('Invalid file provided')]], |
|
154
|
|
|
Http::STATUS_BAD_REQUEST |
|
155
|
|
|
); |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
/** |
|
159
|
|
|
* Delete a global template |
|
160
|
|
|
* |
|
161
|
|
|
* @param int $fileId |
|
162
|
|
|
* @return JSONResponse |
|
163
|
|
|
*/ |
|
164
|
|
|
public function delete($fileId) { |
|
165
|
|
|
try { |
|
166
|
|
|
$this->manager->delete($fileId); |
|
167
|
|
|
|
|
168
|
|
|
return new JSONResponse( |
|
169
|
|
|
['data' => ['status' => 'success']], |
|
170
|
|
|
Http::STATUS_NO_CONTENT |
|
171
|
|
|
); |
|
172
|
|
|
} catch (NotFoundException $e) { |
|
|
|
|
|
|
173
|
|
|
return new JSONResponse( |
|
174
|
|
|
['data' => ['message' => $this->l10n->t('Template not found')]], |
|
175
|
|
|
Http::STATUS_NOT_FOUND |
|
176
|
|
|
); |
|
177
|
|
|
} |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
|
|
/** |
|
181
|
|
|
* @param Node $node |
|
182
|
|
|
* @param int $x |
|
183
|
|
|
* @param int $y |
|
184
|
|
|
* @param bool $a |
|
185
|
|
|
* @param bool $forceIcon |
|
186
|
|
|
* @param string $mode |
|
187
|
|
|
* @return DataResponse|FileDisplayResponse |
|
188
|
|
|
*/ |
|
189
|
|
|
private function fetchPreview( |
|
190
|
|
|
Node $node, |
|
191
|
|
|
$x, |
|
192
|
|
|
$y, |
|
193
|
|
|
$a = false, |
|
194
|
|
|
$forceIcon = true, |
|
195
|
|
|
string $mode): Http\Response { |
|
196
|
|
|
|
|
197
|
|
|
if (!($node instanceof Node) || (!$forceIcon && !$this->preview->isAvailable($node))) { |
|
|
|
|
|
|
198
|
|
|
return new DataResponse([], Http::STATUS_NOT_FOUND); |
|
199
|
|
|
} |
|
200
|
|
|
if (!$node->isReadable()) { |
|
201
|
|
|
return new DataResponse([], Http::STATUS_FORBIDDEN); |
|
202
|
|
|
} |
|
203
|
|
|
|
|
204
|
|
|
try { |
|
205
|
|
|
$f = $this->preview->getPreview($node, $x, $y, !$a, $mode); |
|
206
|
|
|
$response = new FileDisplayResponse($f, Http::STATUS_OK, ['Content-Type' => $f->getMimeType()]); |
|
207
|
|
|
$response->cacheFor(3600 * 24); |
|
208
|
|
|
|
|
209
|
|
|
return $response; |
|
210
|
|
|
} catch (NotFoundException $e) { |
|
|
|
|
|
|
211
|
|
|
return new DataResponse([], Http::STATUS_NOT_FOUND); |
|
212
|
|
|
} catch (\InvalidArgumentException $e) { |
|
213
|
|
|
return new DataResponse([], Http::STATUS_BAD_REQUEST); |
|
214
|
|
|
} |
|
215
|
|
|
} |
|
216
|
|
|
} |
|
217
|
|
|
|