1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* ownCloud - galleryplus |
4
|
|
|
* |
5
|
|
|
* This file is licensed under the Affero General Public License version 3 or |
6
|
|
|
* later. See the COPYING file. |
7
|
|
|
* |
8
|
|
|
* @author Olivier Paroz <[email protected]> |
9
|
|
|
* @author Robin Appelman <[email protected]> |
10
|
|
|
* |
11
|
|
|
* @copyright Olivier Paroz 2014-2015 |
12
|
|
|
* @copyright Robin Appelman 2012-2014 |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
namespace OCA\GalleryPlus\Controller; |
16
|
|
|
|
17
|
|
|
use OCP\IURLGenerator; |
18
|
|
|
use OCP\ILogger; |
19
|
|
|
use OCP\Files\File; |
20
|
|
|
|
21
|
|
|
use OCP\AppFramework\Http; |
22
|
|
|
|
23
|
|
|
use OCA\GalleryPlus\Service\ServiceException; |
24
|
|
|
use OCA\GalleryPlus\Service\NotFoundServiceException; |
25
|
|
|
use OCA\GalleryPlus\Service\ConfigService; |
26
|
|
|
use OCA\GalleryPlus\Service\ThumbnailService; |
27
|
|
|
use OCA\GalleryPlus\Service\PreviewService; |
28
|
|
|
use OCA\GalleryPlus\Service\DownloadService; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Class Preview |
32
|
|
|
* |
33
|
|
|
* @package OCA\GalleryPlus\Controller |
34
|
|
|
*/ |
35
|
|
|
trait Preview { |
36
|
|
|
|
37
|
|
|
use HttpError; |
38
|
|
|
|
39
|
|
|
/** @var IURLGenerator */ |
40
|
|
|
private $urlGenerator; |
41
|
|
|
/** @var ConfigService */ |
42
|
|
|
private $configService; |
43
|
|
|
/** @var ThumbnailService */ |
44
|
|
|
private $thumbnailService; |
45
|
|
|
/** @var PreviewService */ |
46
|
|
|
private $previewService; |
47
|
|
|
/** @var DownloadService */ |
48
|
|
|
private $downloadService; |
49
|
|
|
/** @var ILogger */ |
50
|
|
|
private $logger; |
51
|
|
|
/** @type bool */ |
52
|
|
|
private $download = false; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Exits the controller in a live environment and throws an exception when testing |
56
|
|
|
*/ |
57
|
8 |
|
protected function exitController() { |
58
|
8 |
|
if (defined('PHPUNIT_RUN')) { |
59
|
8 |
|
throw new \Exception(); |
60
|
|
|
// @codeCoverageIgnoreStart |
61
|
|
|
} else { |
62
|
|
|
exit(); |
63
|
|
|
} |
64
|
|
|
// @codeCoverageIgnoreEnd |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Retrieves the thumbnail to send back to the browser |
69
|
|
|
* |
70
|
|
|
* The thumbnail is either a resized preview of the file or the original file |
71
|
|
|
* Thumbnails are base64encoded before getting sent back |
72
|
|
|
* |
73
|
|
|
* |
74
|
|
|
* @param int $fileId the ID of the file of which we need a thumbnail preview of |
75
|
|
|
* @param bool $square whether the thumbnail should be square |
76
|
|
|
* @param double $scale whether we're allowed to scale the preview up |
77
|
|
|
* |
78
|
|
|
* @return array<string,array|string> |
79
|
|
|
*/ |
80
|
17 |
|
private function getThumbnail($fileId, $square, $scale) { |
81
|
|
|
list($width, $height, $aspect, $animatedPreview, $base64Encode) = |
82
|
17 |
|
$this->thumbnailService->getThumbnailSpecs($square, $scale); |
83
|
|
|
/** @type File $file */ |
84
|
|
|
list($file, $preview, $status, $type) = |
85
|
17 |
|
$this->getData( |
86
|
|
|
$fileId, $width, $height, $aspect, $animatedPreview, $base64Encode |
87
|
|
|
); |
88
|
17 |
|
if ($preview === null) { |
89
|
11 |
|
$preview = $this->prepareEmptyThumbnail($file, $status); |
90
|
|
|
} else { |
91
|
6 |
|
if ($type === 'preview') { |
92
|
6 |
|
$preview['preview'] = |
93
|
6 |
|
$this->previewService->previewValidator($square, $base64Encode); |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|
97
|
17 |
|
return [$preview, $status]; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Returns either a generated preview, the file as-is or an empty object |
102
|
|
|
* |
103
|
|
|
* @param int $fileId |
104
|
|
|
* @param int $width |
105
|
|
|
* @param int $height |
106
|
|
|
* @param bool $keepAspect |
107
|
|
|
* @param bool $animatedPreview |
108
|
|
|
* @param bool $base64Encode |
109
|
|
|
* |
110
|
|
|
* @return array<string,\OC_Image|string> |
111
|
|
|
* |
112
|
|
|
* @throws NotFoundServiceException |
113
|
|
|
*/ |
114
|
33 |
|
private function getData( |
115
|
|
|
$fileId, $width, $height, $keepAspect = true, $animatedPreview = true, $base64Encode = false |
116
|
|
|
) { |
117
|
|
|
/** @type File $file */ |
118
|
33 |
|
list($file, $status) = $this->getFile($fileId); |
119
|
|
|
try { |
120
|
33 |
|
if (!is_null($file)) { |
121
|
28 |
|
$data = $this->getPreviewData( |
122
|
|
|
$file, $animatedPreview, $width, $height, $keepAspect, $base64Encode |
123
|
|
|
); |
124
|
|
|
} else { |
125
|
27 |
|
$data = $this->getErrorData($status); |
126
|
|
|
} |
127
|
6 |
|
} catch (ServiceException $exception) { |
128
|
6 |
|
$data = $this->getExceptionData($exception); |
129
|
|
|
} |
130
|
33 |
|
array_unshift($data, $file); |
131
|
|
|
|
132
|
33 |
|
return $data; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Returns the file of which a preview will be generated |
137
|
|
|
* |
138
|
|
|
* @param int $fileId |
139
|
|
|
* |
140
|
|
|
* @return array<File|int|null> |
141
|
|
|
*/ |
142
|
33 |
|
private function getFile($fileId) { |
143
|
33 |
|
$status = Http::STATUS_OK; |
144
|
|
|
try { |
145
|
|
|
/** @type File $file */ |
146
|
33 |
|
$file = $this->previewService->getResourceFromId($fileId); |
147
|
28 |
|
$this->configService->validateMimeType($file->getMimeType()); |
148
|
5 |
|
} catch (ServiceException $exception) { |
149
|
5 |
|
$file = null; |
150
|
5 |
|
$status = $this->getHttpStatusCode($exception); |
151
|
|
|
} |
152
|
|
|
|
153
|
33 |
|
return [$file, $status]; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* @param File $file |
158
|
|
|
* @param bool $animatedPreview |
159
|
|
|
* @param int $width |
160
|
|
|
* @param int $height |
161
|
|
|
* @param bool $keepAspect |
162
|
|
|
* @param bool $base64Encode |
163
|
|
|
* |
164
|
|
|
* @return array |
165
|
|
|
*/ |
166
|
28 |
|
private function getPreviewData( |
167
|
|
|
$file, $animatedPreview, $width, $height, $keepAspect, $base64Encode |
168
|
|
|
) { |
169
|
28 |
|
$status = Http::STATUS_OK; |
170
|
28 |
|
if ($this->previewService->isPreviewRequired($file, $animatedPreview)) { |
171
|
19 |
|
$type = 'preview'; |
172
|
19 |
|
$preview = $this->previewService->createPreview( |
173
|
|
|
$file, $width, $height, $keepAspect, $base64Encode |
174
|
|
|
); |
175
|
|
|
} else { |
176
|
6 |
|
$type = 'download'; |
177
|
6 |
|
$preview = $this->downloadService->downloadFile($file, $base64Encode); |
178
|
|
|
} |
179
|
22 |
|
if (!$preview) { |
180
|
8 |
|
list($preview, $status, $type) = $this->getErrorData(); |
181
|
|
|
} |
182
|
|
|
|
183
|
22 |
|
return [$preview, $status, $type]; |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* Returns an error array |
188
|
|
|
* |
189
|
|
|
* @param $status |
190
|
|
|
* |
191
|
|
|
* @return array<null|int|string> |
192
|
|
|
*/ |
193
|
19 |
|
private function getErrorData($status = Http::STATUS_INTERNAL_SERVER_ERROR) { |
194
|
19 |
|
return [null, $status, 'error']; |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* Returns an error array |
199
|
|
|
* |
200
|
|
|
* @param ServiceException $exception |
201
|
|
|
* |
202
|
|
|
* @return array<null|int|string> |
203
|
|
|
*/ |
204
|
6 |
|
private function getExceptionData($exception) { |
205
|
6 |
|
$code = $this->getHttpStatusCode($exception); |
206
|
|
|
|
207
|
6 |
|
return $this->getErrorData($code); |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* Prepares an empty Thumbnail array to send back |
212
|
|
|
* |
213
|
|
|
* When we can't even get the file information, we send an empty mimeType |
214
|
|
|
* |
215
|
|
|
* @param File $file |
216
|
|
|
* @param int $status |
217
|
|
|
* |
218
|
|
|
* @return array<string,null|string> |
219
|
|
|
*/ |
220
|
11 |
|
private function prepareEmptyThumbnail($file, $status) { |
221
|
11 |
|
$thumbnail = []; |
222
|
11 |
|
if ($status !== Http::STATUS_NOT_FOUND) { |
223
|
10 |
|
$mimeType = ''; |
224
|
10 |
|
if ($file) { |
225
|
10 |
|
$mimeType = $file->getMimeType(); |
226
|
|
|
} |
227
|
10 |
|
$thumbnail = ['preview' => null, 'mimetype' => $mimeType]; |
228
|
|
|
} |
229
|
|
|
|
230
|
11 |
|
return $thumbnail; |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
} |
234
|
|
|
|