-
-
Notifications
You must be signed in to change notification settings - Fork 625
Expand file tree
/
Copy pathThumbnailController.php
More file actions
192 lines (163 loc) · 4.45 KB
/
ThumbnailController.php
File metadata and controls
192 lines (163 loc) · 4.45 KB
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
<?php
namespace Statamic\Http\Controllers\CP\Assets;
use Illuminate\Support\Facades\Cache;
use League\Flysystem\UnableToReadFile;
use League\Glide\Server;
use Statamic\Exceptions\NotFoundHttpException;
use Statamic\Facades\Asset;
use Statamic\Facades\Config;
use Statamic\Facades\Image;
use Statamic\Http\Controllers\Controller;
use Statamic\Imaging\ImageGenerator;
use Statamic\Statamic;
class ThumbnailController extends Controller
{
/**
* @var Server
*/
protected $server;
/**
* @var ImageGenerator
*/
protected $generator;
/**
* @var \Statamic\Contracts\Assets\Asset
*/
protected $asset;
/**
* @var string
*/
protected $size;
/**
* @var string
*/
protected $orientation;
/**
* @var string
*/
protected $mutex;
public function __construct(Server $server, ImageGenerator $generator)
{
$this->server = $server;
$this->generator = $generator;
}
/**
* Display the thumbnail.
*
* @param string $asset
* @param string $size
* @param string $orientation
* @return \Illuminate\Http\Response
*/
public function show($asset, $size = null, $orientation = null)
{
$this->size = $size;
$this->orientation = $orientation;
$this->asset = $this->asset($asset);
$this->authorize('view', $this->asset);
if ($placeholder = $this->getPlaceholderResponse()) {
return $placeholder;
}
return $this->server->getResponseFactory()->create(
$this->server->getCache(),
$this->generate()
);
}
/**
* Get an asset, or throw a 404 if not found.
*
* @param string $asset An encoded asset ID from the URL.
* @return \Statamic\Contracts\Assets\Asset
*/
private function asset($asset)
{
if (! $asset = Asset::find(base64_decode($asset))) {
abort(404);
}
return $asset;
}
/**
* Generate the image.
*
* @return string
*/
private function generate()
{
$this->waitIfProcessing();
Cache::put($this->mutex(), true, now()->addMinutes(5));
try {
$preset = $this->size ? $this->getPreset() : null;
if ($preset && ! collect(Image::cpManipulationPresets())->has($preset)) {
throw new \Exception('Invalid preset');
}
$path = $this->generator->generateByAsset(
$this->asset,
$preset ? ['p' => $preset] : []
);
} catch (UnableToReadFile $e) {
throw new NotFoundHttpException;
} finally {
Cache::forget($this->mutex());
}
return $path;
}
/**
* Get control panel thumbnail image preset name.
*
* Statamic has few control panel specific image presets
*
* @see \Statamic\Imaging\Manager::cpManipulationPresets
*
* @return string
*/
private function getPreset()
{
return "cp_thumbnail_{$this->size}_{$this->getOrientation()}";
}
/**
* Get orientation override from URL path or directly from asset.
*
* @return string|null
*/
private function getOrientation()
{
return $this->orientation ?? $this->asset->orientation();
}
/**
* If the image is being processed in a different request, just hold on for a moment.
*
* @return void
*/
private function waitIfProcessing()
{
while ($cache = Cache::get($this->mutex())) {
sleep(1);
}
}
/**
* Get the cache key of the mutex.
*
* @return string
*/
private function mutex()
{
if ($this->mutex) {
return $this->mutex;
}
return $this->mutex = 'imagegenerator::generating.'.md5($this->asset->id().$this->size);
}
/**
* If an image is deemed too large for thumbnail generation, we'll give it a placeholder icon.
*
* @return \Illuminate\Http\Response
*/
private function getPlaceholderResponse()
{
$maxWidth = Config::get('statamic.assets.thumbnails.max_width');
$maxHeight = Config::get('statamic.assets.thumbnails.max_height');
if ($this->asset->width() < $maxWidth && $this->asset->height() < $maxHeight) {
return;
}
return response(Statamic::svg('filetypes/picture'))->header('Content-Type', 'image/svg+xml');
}
}