1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Wraps the Image object. We have to replace a few more methods |
4
|
|
|
* with Image because things like formatting and dimensions don't |
5
|
|
|
* work the same with a placeholder file. |
6
|
|
|
* |
7
|
|
|
* @mixin CloudFileExtension |
8
|
|
|
* |
9
|
|
|
* @author Mark Guinn <[email protected]> |
10
|
|
|
* @date 01.10.2014 |
11
|
|
|
* @package cloudassets |
12
|
|
|
* @subpackage wrappers |
13
|
|
|
*/ |
14
|
|
|
class CloudImage extends Image implements CloudAssetInterface |
15
|
|
|
{ |
16
|
|
|
private static $has_many = array( |
|
|
|
|
17
|
|
|
'DerivedImages' => 'CloudImageCachedStore', |
18
|
|
|
); |
19
|
|
|
|
20
|
|
|
|
21
|
|
|
public function Link() |
22
|
|
|
{ |
23
|
|
|
$this->createLocalIfNeeded(); |
|
|
|
|
24
|
|
|
return $this->CloudStatus == 'Live' ? $this->getCloudURL() : parent::Link(); |
|
|
|
|
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
public function RelativeLink() |
28
|
|
|
{ |
29
|
|
|
$this->createLocalIfNeeded(); |
|
|
|
|
30
|
|
|
return $this->CloudStatus == 'Live' ? $this->getCloudURL() : parent::RelativeLink(); |
|
|
|
|
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
public function getURL() |
34
|
|
|
{ |
35
|
|
|
$this->createLocalIfNeeded(); |
|
|
|
|
36
|
|
|
return $this->CloudStatus == 'Live' ? $this->getCloudURL() : parent::getURL(); |
|
|
|
|
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public function getAbsoluteURL() |
40
|
|
|
{ |
41
|
|
|
$this->createLocalIfNeeded(); |
|
|
|
|
42
|
|
|
return $this->CloudStatus == 'Live' ? $this->getCloudURL() : parent::getAbsoluteURL(); |
|
|
|
|
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public function getAbsoluteSize() |
46
|
|
|
{ |
47
|
|
|
$this->createLocalIfNeeded(); |
|
|
|
|
48
|
|
|
return $this->CloudStatus == 'Live' ? $this->CloudSize : parent::getAbsoluteSize(); |
|
|
|
|
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public function exists() |
52
|
|
|
{ |
53
|
|
|
$this->createLocalIfNeeded(); |
|
|
|
|
54
|
|
|
return parent::exists(); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Save the dimensions before we potentially wipe out the file |
60
|
|
|
*/ |
61
|
|
|
public function onBeforeCloudPut() |
62
|
|
|
{ |
63
|
|
|
$this->setCloudMeta('Dimensions', $this->getDimensions('live')); |
|
|
|
|
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @param string $dim - 'string' or 0 (width) or 1 (height) |
69
|
|
|
* @return int|string |
70
|
|
|
*/ |
71
|
|
|
public function getDimensions($dim = "string") |
72
|
|
|
{ |
73
|
|
|
// give an option to get the real dimensions |
74
|
|
|
if ($dim === 'live') { |
75
|
|
|
return parent::getDimensions(); |
76
|
|
|
} |
77
|
|
|
if ($this->CloudStatus != 'Live') { |
|
|
|
|
78
|
|
|
return parent::getDimensions($dim); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
// otherwise we need to resort to stored dimensions because the |
82
|
|
|
// file may be in the cloud and the local may be a placeholder |
83
|
|
|
$val = $this->getCloudMeta('Dimensions'); |
|
|
|
|
84
|
|
|
if (empty($val) || !preg_match('/^\d+x\d+$/', $val)) { |
85
|
|
|
$this->downloadFromCloud(); |
|
|
|
|
86
|
|
|
$val = parent::getDimensions('string'); |
87
|
|
|
$this->convertToPlaceholder(); |
|
|
|
|
88
|
|
|
|
89
|
|
|
if (preg_match('/^\d+x\d+$/', $val)) { |
90
|
|
|
$this->setCloudMeta('Dimensions', $val); |
|
|
|
|
91
|
|
|
$this->write(); |
92
|
|
|
} else { |
93
|
|
|
CloudAssets::inst()->getLogger()->error("Corrupted image/metadata in {$this->ID} => {$val}"); |
94
|
|
|
$val = "1x1"; |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
if ($dim === 'string') { |
99
|
|
|
return $val; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
$val = explode('x', $val); |
103
|
|
|
return $val[$dim]; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Return an image object representing the image in the given format. |
109
|
|
|
* This image will be generated using generateFormattedImage(). |
110
|
|
|
* The generated image is cached, to flush the cache append ?flush=1 to your URL. |
111
|
|
|
* |
112
|
|
|
* Just pass the correct number of parameters expected by the working function |
113
|
|
|
* |
114
|
|
|
* @param string $format The name of the format. |
115
|
|
|
* @return CloudImageCached|null |
116
|
|
|
*/ |
117
|
|
|
public function getFormattedImage($format) |
|
|
|
|
118
|
|
|
{ |
119
|
|
|
$args = func_get_args(); |
120
|
|
|
$logger = CloudAssets::inst()->getLogger(); |
121
|
|
|
|
122
|
|
|
if ($this->ID && $this->Filename && Director::fileExists($this->Filename)) { |
|
|
|
|
123
|
|
|
$cacheFile = call_user_func_array(array($this, "cacheFilename"), $args); |
124
|
|
|
$cachePath = Director::baseFolder()."/".$cacheFile; |
125
|
|
|
|
126
|
|
|
/** @var CloudImageCachedStore $stored */ |
127
|
|
|
$stored = CloudImageCachedStore::get()->filter('Filename', $cacheFile)->first(); |
128
|
|
|
if ($stored && !$stored->exists()) { |
129
|
|
|
$stored = null; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
// If ?flush is present, always wipe existing data and start clean |
133
|
|
|
if (isset($_GET['flush'])) { |
134
|
|
|
// There was a bug here caused by the fact that GDBackend tries |
135
|
|
|
// to read the size off the cached image, which would be a placeholder |
136
|
|
|
// in certain cases. |
137
|
|
|
// I'm not 100% sure what the correct behaviour is here. For now |
138
|
|
|
// we'll destroy the existing image, causing it to be re-uploaded |
139
|
|
|
// every time. That seems safer if a little bit wasteful. |
140
|
|
|
$logger->debug("CloudAssets: deleting cached image because of flush: $cachePath"); |
141
|
|
|
if (file_exists($cachePath)) { |
142
|
|
|
unlink($cachePath); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
// delete the existing meta if it existed |
146
|
|
|
if ($stored) { |
147
|
|
|
$stored->delete(); |
148
|
|
|
$stored = null; |
149
|
|
|
} |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
// start building out the record |
153
|
|
|
$cached = new CloudImageCached($cacheFile); |
154
|
|
|
$cached->Title = $this->Title; |
155
|
|
|
$cached->ParentID = $this->ParentID; |
156
|
|
|
|
157
|
|
|
// Is there a meta record for this formatted file? |
158
|
|
|
if ($stored) { |
159
|
|
|
// Has it been successfully uploaded to the cloud? |
160
|
|
|
// If so, we can just send this puppy on |
161
|
|
|
// If not, is there a local file that's present and correct? |
162
|
|
|
// If not, we need to wipe the meta and anything local and regenerate |
163
|
|
|
if ($stored->CloudStatus !== 'Live' && $cached->isLocalMissing()) { |
|
|
|
|
164
|
|
|
$stored->delete(); |
165
|
|
|
$stored = null; |
166
|
|
|
if (file_exists($cachePath)) { |
167
|
|
|
unlink($cachePath); |
168
|
|
|
} |
169
|
|
|
} else { |
170
|
|
|
$cached->setStoreRecord($stored); |
171
|
|
|
} |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
// If there is no meta record (or an invalid one), is there a local file or placeholder? |
175
|
|
|
if (!$stored) { |
176
|
|
|
// if the local exists as a placeholder, we need to check if the cloud version is valid |
177
|
|
|
if (file_exists($cachePath) && $cached->containsPlaceholder()) { |
|
|
|
|
178
|
|
|
try { |
179
|
|
|
$cached->downloadFromCloud(); |
|
|
|
|
180
|
|
|
} catch (Exception $e) { |
181
|
|
|
// We want to fail silently here if there is any trouble |
182
|
|
|
// because we can always regenerate the thumbnail |
183
|
|
|
if (CloudAssets::config()->missing_image) { |
184
|
|
|
return new CloudImageMissing($this, $args); |
|
|
|
|
185
|
|
|
} |
186
|
|
|
} |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
// If we don't have a valid local version at this point... |
190
|
|
|
if ($cached->isLocalMissing()) { |
|
|
|
|
191
|
|
|
// delete whatever might have been there |
192
|
|
|
if (file_exists($cachePath)) { |
193
|
|
|
unlink($cachePath); |
194
|
|
|
} |
195
|
|
|
$logger->debug("CloudAssets: generating formatted image at $cachePath"); |
196
|
|
|
|
197
|
|
|
// Regenerate the formatted image |
198
|
|
|
if ($this->CloudStatus === 'Live' && $this->isLocalMissing()) { |
|
|
|
|
199
|
|
|
try { |
200
|
|
|
$this->downloadFromCloud(); |
|
|
|
|
201
|
|
|
} catch (Exception $e) { |
202
|
|
|
if (CloudAssets::config()->missing_image) { |
203
|
|
|
return new CloudImageMissing($this, $args); |
|
|
|
|
204
|
|
|
} |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
call_user_func_array(array($this, "generateFormattedImage"), $args); |
208
|
|
|
$this->convertToPlaceholder(); |
|
|
|
|
209
|
|
|
} else { |
210
|
|
|
call_user_func_array(array($this, "generateFormattedImage"), $args); |
211
|
|
|
} |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
// If we now have a valid image, generate a stored meta record for it |
215
|
|
|
if (file_exists($cachePath)) { |
216
|
|
|
$stored = new CloudImageCachedStore(); |
217
|
|
|
$stored->Filename = $cacheFile; |
|
|
|
|
218
|
|
|
$stored->SourceID = $this->ID; |
|
|
|
|
219
|
|
|
$stored->write(); |
220
|
|
|
// all the other fields will get set when the cloud status is updated |
221
|
|
|
$cached->setStoreRecord($stored); |
222
|
|
|
} |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
// upload to cloud if needed |
226
|
|
|
$cached->updateCloudStatus(); |
|
|
|
|
227
|
|
|
|
228
|
|
|
return $cached; |
|
|
|
|
229
|
|
|
} |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
|
233
|
|
|
/** |
234
|
|
|
* Checks if the local file is an image that can be used and not |
235
|
|
|
* a placeholder or a corrupted file. |
236
|
|
|
* |
237
|
|
|
* @return bool |
238
|
|
|
*/ |
239
|
|
|
public function isLocalValid() |
240
|
|
|
{ |
241
|
|
|
$path = $this->getFullPath(); |
242
|
|
|
if (!file_exists($path)) { |
243
|
|
|
return false; |
244
|
|
|
} |
245
|
|
|
return (getimagesize($path) !== false); |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
|
249
|
|
|
/** |
250
|
|
|
* @return int The number of formatted images deleted |
251
|
|
|
*/ |
252
|
|
|
public function deleteFormattedImages() |
253
|
|
|
{ |
254
|
|
|
foreach ($this->DerivedImages() as $store) { |
|
|
|
|
255
|
|
|
$img = $store->getCloudImageCached(); |
256
|
|
|
$img->delete(); |
257
|
|
|
} |
258
|
|
|
|
259
|
|
|
// The above should have covered everything but this will clean up any |
260
|
|
|
// loose ends that were maybe created before wrapping or fell through a crack |
261
|
|
|
parent::deleteFormattedImages(); |
262
|
|
|
} |
263
|
|
|
} |
264
|
|
|
|