1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Gallery |
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
|
|
|
* |
10
|
|
|
* @copyright Olivier Paroz 2016 |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
namespace OCA\Gallery\Controller; |
14
|
|
|
|
15
|
|
|
use OCP\IRequest; |
16
|
|
|
use OCP\IURLGenerator; |
17
|
|
|
use OCP\ILogger; |
18
|
|
|
use OCP\Files\File; |
19
|
|
|
|
20
|
|
|
use OCP\AppFramework\Controller; |
21
|
|
|
use OCP\AppFramework\Http; |
22
|
|
|
use OCP\AppFramework\Http\JSONResponse; |
23
|
|
|
|
24
|
|
|
use OCA\Gallery\Http\ImageResponse; |
25
|
|
|
use OCA\Gallery\Service\ConfigService; |
26
|
|
|
use OCA\Gallery\Service\ThumbnailService; |
27
|
|
|
use OCA\Gallery\Service\PreviewService; |
28
|
|
|
use OCA\Gallery\Service\DownloadService; |
29
|
|
|
use OCA\Gallery\Utility\EventSource; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Class PreviewController |
33
|
|
|
* |
34
|
|
|
* @package OCA\Gallery\Controller |
35
|
|
|
*/ |
36
|
|
|
class PreviewController extends Controller { |
37
|
|
|
use Preview; |
38
|
|
|
|
39
|
|
|
/** @var EventSource */ |
40
|
|
|
private $eventSource; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Constructor |
44
|
|
|
* |
45
|
|
|
* @param string $appName |
46
|
|
|
* @param IRequest $request |
47
|
|
|
* @param IURLGenerator $urlGenerator |
48
|
|
|
* @param ConfigService $configService |
49
|
|
|
* @param ThumbnailService $thumbnailService |
50
|
|
|
* @param PreviewService $previewService |
51
|
|
|
* @param DownloadService $downloadService |
52
|
|
|
* @param EventSource $eventSource |
53
|
|
|
* @param ILogger $logger |
54
|
|
|
*/ |
55
|
|
View Code Duplication |
public function __construct( |
|
|
|
|
56
|
|
|
$appName, |
57
|
|
|
IRequest $request, |
58
|
|
|
IURLGenerator $urlGenerator, |
59
|
|
|
ConfigService $configService, |
60
|
|
|
ThumbnailService $thumbnailService, |
61
|
|
|
PreviewService $previewService, |
62
|
|
|
DownloadService $downloadService, |
63
|
|
|
EventSource $eventSource, |
64
|
|
|
ILogger $logger |
65
|
|
|
) { |
66
|
|
|
parent::__construct($appName, $request); |
67
|
|
|
|
68
|
|
|
$this->urlGenerator = $urlGenerator; |
69
|
|
|
$this->configService = $configService; |
70
|
|
|
$this->thumbnailService = $thumbnailService; |
71
|
|
|
$this->previewService = $previewService; |
72
|
|
|
$this->downloadService = $downloadService; |
73
|
|
|
$this->eventSource = $eventSource; |
74
|
|
|
$this->logger = $logger; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @NoAdminRequired |
79
|
|
|
* |
80
|
|
|
* Generates thumbnails |
81
|
|
|
* |
82
|
|
|
* Uses EventSource to send thumbnails back as soon as they're created |
83
|
|
|
* |
84
|
|
|
* FIXME: @LukasReschke says: The exit is required here because |
85
|
|
|
* otherwise the AppFramework is trying to add headers as well after |
86
|
|
|
* dispatching the request which results in a "Cannot modify header |
87
|
|
|
* information" notice. |
88
|
|
|
* |
89
|
|
|
* WARNING: Returning a JSON response does not get rid of the problem |
90
|
|
|
* |
91
|
|
|
* @param string $ids the ID of the files of which we need thumbnail previews of |
92
|
|
|
* @param bool $square |
93
|
|
|
* @param double $scale |
94
|
|
|
* |
95
|
|
|
* @return array<string,array|string|null> |
|
|
|
|
96
|
|
|
*/ |
97
|
|
|
public function getThumbnails($ids, $square, $scale) { |
98
|
|
|
$idsArray = \explode(';', $ids); |
99
|
|
|
|
100
|
|
|
foreach ($idsArray as $id) { |
101
|
|
|
// Casting to integer here instead of using array_map to extract IDs from the URL |
102
|
|
|
list($thumbnail, $status) = $this->getThumbnail((int)$id, $square, $scale); |
103
|
|
|
$thumbnail['fileid'] = $id; |
104
|
|
|
$thumbnail['status'] = $status; |
105
|
|
|
|
106
|
|
|
$this->eventSource->send('preview', $thumbnail); |
107
|
|
|
} |
108
|
|
|
$this->eventSource->close(); |
109
|
|
|
|
110
|
|
|
$this->exitController(); |
111
|
|
|
// @codeCoverageIgnoreStart |
112
|
|
|
} // @codeCoverageIgnoreEnd |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @NoAdminRequired |
116
|
|
|
* |
117
|
|
|
* Sends either a large preview of the requested file or the original file itself |
118
|
|
|
* |
119
|
|
|
* @param int $fileId the ID of the file of which we need a large preview of |
120
|
|
|
* @param int $width |
121
|
|
|
* @param int $height |
122
|
|
|
* |
123
|
|
|
* @return ImageResponse|Http\JSONResponse |
124
|
|
|
*/ |
125
|
|
|
public function getPreview($fileId, $width, $height) { |
126
|
|
|
/** @type File $file */ |
127
|
|
|
list($file, $preview, $status) = $this->getData($fileId, $width, $height); |
128
|
|
|
|
129
|
|
|
if (!$preview) { |
130
|
|
|
return new JSONResponse( |
131
|
|
|
[ |
132
|
|
|
'message' => "I'm truly sorry, but we were unable to generate a preview for this file", |
133
|
|
|
'success' => false |
134
|
|
|
], $status |
135
|
|
|
); |
136
|
|
|
} |
137
|
|
|
$preview['name'] = $file->getName(); |
138
|
|
|
|
139
|
|
|
return new ImageResponse($preview, $status); |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.