Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
238 lines (211 loc) · 6.1 KB

File metadata and controls

238 lines (211 loc) · 6.1 KB
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
<?php
App::uses('CakeEventListener', 'Event');
/**
* Local Image Processor Event Listener for the CakePHP FileStorage plugin
*
* @deprecated Use ImageProcessingListener instead
* @author Florian Krämer
* @copy 2012 Florian Krämer
* @license MIT
*/
class LocalImageProcessingListener extends Object implements CakeEventListener {
/**
* Implemented Events
*
* @return array
*/
public function implementedEvents() {
return array(
'ImageVersion.createVersion' => 'createVersions',
'ImageVersion.removeVersion' => 'removeVersions',
'ImageStorage.afterSave' => 'afterSave',
'ImageStorage.afterDelete' => 'afterDelete',
'FileStorage.ImageHelper.imagePath' => 'imagePath',
);
}
/**
* Creates the different versions of images that are configured
*
* @param Model $Model
* @param array $record
* @param array $operations
* @return void
*/
protected function _createVersions($Model, $record, $operations) {
$Storage = StorageManager::adapter($record['adapter']);
$path = $this->_buildPath($record, true);
$tmpFile = $this->_tmpFile($Storage, $path);
foreach ($operations as $version => $imageOperations) {
$hash = $Model->hashOperations($imageOperations);
$string = $this->_buildPath($record, true, $hash);
if ($Storage->has($string)) {
continue;
}
try {
$image = $Model->processImage($tmpFile, null, array('format' => $record['extension']), $imageOperations);
$result = $Storage->write($string, $image->get($record['extension']), true);
} catch (Exception $e) {
$this->log($e->getMessage(), 'file_storage');
unlink($tmpFile);
throw $e;
}
}
unlink($tmpFile);
}
/**
* Creates versions for a given image record
*
* @param CakeEvent $Event
* @return void
*/
public function createVersions($Event) {
if ($this->_checkEvent($Event)) {
$Model = $Event->subject();
$Storage = $Event->data['storage'];
$record = $Event->data['record'][$Model->alias];
$this->_createVersions($Model, $record, $Event->data['operations']);
$Event->stopPropagation();
}
}
/**
* Removes versions for a given image record
*
* @param CakeEvent $Event
* @return void
*/
public function removeVersions($Event) {
if ($this->_checkEvent($Event)) {
$Model = $Event->subject();
$Storage = $Event->data['storage'];
$record = $Event->data['record'][$Model->alias];
foreach ($Event->data['operations'] as $version => $operations) {
$hash = $Model->hashOperations($operations);
$string = $this->_buildPath($record, true, $hash);
try {
if ($Storage->has($string)) {
$Storage->delete($string);
}
} catch (Exception $e) {
$this->log($e->getMessage(), 'file_storage');
}
}
$Event->stopPropagation();
}
}
/**
* afterDelete
*
* @param CakeEvent $Event
* @return void
*/
public function afterDelete($Event) {
if ($this->_checkEvent($Event)) {
$Model = $Event->subject();
$path = Configure::read('Media.basePath') . $Event->data['record'][$Model->alias]['path'];
if (is_dir($path)) {
$Folder = new Folder($path);
return $Folder->delete();
}
return false;
}
}
/**
* afterSave
*
* @param CakeEvent $Event
* @return void
*/
public function afterSave($Event) {
if ($this->_checkEvent($Event)) {
$Model = $Event->subject();
$Storage = StorageManager::adapter($Model->data[$Model->alias]['adapter']);
$record = $Model->data[$Model->alias];
try {
$id = $record[$Model->primaryKey];
$filename = $Model->stripUuid($id);
$file = $record['file'];
$format = $record['extension'];
$sizes = Configure::read('Media.imageSizes.' . $record['model']);
$record['path'] = $Model->fsPath('images' . DS . $record['model'], $id);
$result = $Storage->write($record['path'] . $filename . '.' . $record['extension'], file_get_contents($file['tmp_name']), true);
$data = $Model->save(array($Model->alias => $record), array(
'validate' => false,
'callbacks' => false));
$operations = Configure::read('Media.imageSizes.' . $record['model']);
if (!empty($operations)) {
$this->_createVersions($Model, $record, $operations);
}
$Model->data = $data;
} catch (Exception $e) {
$this->log($e->getMessage(), 'file_storage');
}
}
}
/**
* Generates the path the image depending on the storage adapter
*
* @param CakeEvent $Event
* @return void
*/
public function imagePath($Event) {
if ($Event->data['image']['adapter'] == 'Local') {
$Helper = $Event->subject();
extract($Event->data);
$path = $this->_buildPath($image, true, $hash);
$Event->data['path'] = $path;
$Event->stopPropagation();
}
}
/**
* It is required to get the file first and write it to a tmp file
*
* The adapter might not be one that is using a local file system, so we first
* get the file from the storage system, store it locally in a tmp file and
* later load the new file that was generated based on the tmp file into the
* storage adapter. This method here just generates the tmp file.
*
* @param $Storage
* @param $path
* @return bool|string
*/
protected function _tmpFile($Storage, $path) {
try {
if (!is_dir(TMP . 'image-processing')) {
mkdir(TMP . 'image-processing');
}
$tmpFile = TMP . 'image-processing' . DS . String::uuid();
$imageData = $Storage->read($path);
file_put_contents($tmpFile, $imageData);
return $tmpFile;
} catch (Exception $e) {
throw $e;
}
}
/**
* Builds a path to a file
*
* @param array $image
* @param boolean $extension
* @param string $hash
*/
protected function _buildPath($image, $extension = true, $hash = null) {
$path = $image['path'] . str_replace('-', '', $image['id']);
if (!empty($hash)) {
$path .= '.' . $hash;
}
if ($extension == true) {
$path .= '.' . $image['extension'];
}
return '/' . $path;
}
/**
* Check if the event is of a type / model we want to process with this listener
*
* @param CakeEvent $Event
* @return boolean
*/
protected function _checkEvent($Event) {
$Model = $Event->subject();
return ($Model instanceOf ImageStorage && isset($Event->data['record'][$Model->alias]['adapter']) && $Event->data['record'][$Model->alias]['adapter'] == 'Local');
}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.