Skip to content
This repository was archived by the owner on Mar 5, 2022. It is now read-only.

Commit 0861f0b

Browse files
author
Florian Krämer
committed
Attempt to fix issues with the image version shell
1 parent c462135 commit 0861f0b

File tree

4 files changed

+33
-17
lines changed

4 files changed

+33
-17
lines changed

src/Event/ImageProcessingListener.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Cake\ORM\Table;
99
use Burzum\FileStorage\Storage\StorageManager;
1010
use Burzum\FileStorage\Storage\StorageUtils;
11+
use RuntimeException;
1112

1213
/**
1314
* @author Florian Krämer
@@ -113,7 +114,7 @@ protected function _autoRotate($imageFile, $format) {
113114
* @return false|null
114115
*/
115116
protected function _createVersions(Table $table, $entity, array $operations) {
116-
$Storage = StorageManager::adapter($entity['adapter']);
117+
$Storage = StorageManager::getAdapter($entity['adapter']);
117118
$path = $this->_buildPath($entity, true);
118119
$tmpFile = $this->_tmpFile($Storage, $path, TMP . 'image-processing');
119120

@@ -266,7 +267,7 @@ public function afterSave(Event $Event) {
266267
if ($this->_checkEvent($Event)) {
267268
$table = $Event->subject();
268269
$record = $Event->getData('record');
269-
$Storage = StorageManager::adapter($record->adapter);
270+
$Storage = StorageManager::getAdapter($record->get('adapter'));
270271
try {
271272
$id = $record->{$table->primaryKey()};
272273
$filename = $this->stripDashes($id);
@@ -313,7 +314,7 @@ public function imagePath(Event $Event) {
313314
extract($data);
314315

315316
if (!isset($Event->data['image']['adapter'])) {
316-
throw new \RuntimeException(__d('file_storage', 'No adapter config key passed!'));
317+
throw new RuntimeException(__d('file_storage', 'No adapter config key passed!'));
317318
}
318319

319320
$adapterClass = $this->getAdapterClassName($Event->data['image']['adapter']);
@@ -323,7 +324,7 @@ public function imagePath(Event $Event) {
323324
return $this->$buildMethod($Event);
324325
}
325326

326-
throw new \RuntimeException(__d('file_storage', 'No callback image url callback implemented for adapter %s', $adapterClass));
327+
throw new RuntimeException(__d('file_storage', 'No callback image url callback implemented for adapter %s', $adapterClass));
327328
}
328329

329330
/**

src/Shell/ImageVersionShell.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -266,17 +266,20 @@ protected function _loop($action, $model, $operations = [], $options = []) {
266266
'options' => $options
267267
);
268268

269-
if ($action == 'generate' || $action == 'regenerate') {
269+
if ($action === 'generate' || $action === 'regenerate') {
270270
$Event = new Event('ImageVersion.createVersion', $this->Table, $payload);
271271
EventManager::instance()->dispatch($Event);
272272
}
273273

274-
if ($action == 'remove') {
274+
if ($action === 'remove') {
275275
$Event = new Event('ImageVersion.removeVersion', $this->Table, $payload);
276276
EventManager::instance()->dispatch($Event);
277277
}
278278

279-
$this->out(__('{0} processed', $image->id));
279+
$this->out(__('{0} processed', $image->get('id')));
280+
$this->verbose('Path: ' . $image->get('path'));
281+
$this->verbose('Adapter Config: ' . $image->get('adapter'));
282+
$this->verbose($this->nl());
280283
}
281284
}
282285
}

src/Storage/Listener/BaseListener.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ protected function _processImages(Event $event, $method) {
185185

186186
$this->_loadImageProcessingFromConfig();
187187

188-
$event->setResult($this->{$method}(
188+
$event->setResult($this->{$method} (
189189
$event->getData('record'),
190190
$versions,
191191
$options
@@ -204,7 +204,7 @@ protected function _processImages(Event $event, $method) {
204204
*/
205205
protected function _getVersionData($event)
206206
{
207-
$data = $event->data['versions'];
207+
$data = $event->getData();
208208

209209
if (isset($data['versions'])) {
210210
$versions = $data['versions'];

src/Storage/Listener/ImageProcessingTrait.php

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,11 @@
77
namespace Burzum\FileStorage\Storage\Listener;
88

99
use Burzum\FileStorage\Storage\StorageUtils;
10+
use Cake\Console\Shell;
1011
use Cake\Core\Configure;
1112
use Cake\Datasource\EntityInterface;
13+
use Exception;
14+
use RuntimeException;
1215

1316
/**
1417
* ImageProcessingTrait
@@ -84,7 +87,7 @@ public function imageProcessor(array $config = [], $renew = false) {
8487
*/
8588
public function getImageVersionHash($model, $version) {
8689
if (empty($this->_imageVersionHashes[$model][$version])) {
87-
throw new \RuntimeException(sprintf('Version "%s" for identifier "%s" does not exist!', $version, $model));
90+
throw new RuntimeException(sprintf('Version "%s" for identifier "%s" does not exist!', $version, $model));
8891
}
8992
return $this->_imageVersionHashes[$model][$version];
9093
}
@@ -99,11 +102,11 @@ public function getImageVersionHash($model, $version) {
99102
*/
100103
protected function _checkImageVersions($identifier, array $versions) {
101104
if (!isset($this->_imageVersions[$identifier])) {
102-
throw new \RuntimeException(sprintf('No image version config found for identifier "%s"!', $identifier));
105+
throw new RuntimeException(sprintf('No image version config found for identifier "%s"!', $identifier));
103106
}
104107
foreach ($versions as $version) {
105108
if (!isset($this->_imageVersions[$identifier][$version])) {
106-
throw new \RuntimeException(sprintf('Invalid version "%s" for identifier "%s"!', $identifier, $version));
109+
throw new RuntimeException(sprintf('Invalid version "%s" for identifier "%s"!', $identifier, $version));
107110
}
108111
}
109112
}
@@ -117,18 +120,27 @@ protected function _checkImageVersions($identifier, array $versions) {
117120
* @return array
118121
*/
119122
public function createImageVersions(EntityInterface $entity, array $versions, array $options = []) {
120-
$this->_checkImageVersions($entity->model, $versions);
123+
$this->_checkImageVersions($entity->get('model'), $versions);
121124

122125
$options += $this->_defaultOutput + [
123126
'overwrite' => true
124127
];
125128

126129
$result = [];
127-
$storage = $this->storageAdapter($entity->adapter);
128-
foreach ($this->_imageVersions[$entity->model] as $version => $operations) {
130+
$storage = $this->storageAdapter($entity->get('adapter'));
131+
132+
foreach ($this->_imageVersions[$entity->get('model')] as $version => $operations) {
129133
if (!in_array($version, $versions)) {
134+
if ($this instanceof Shell) {
135+
$this->warn(sprintf(
136+
'Version `%s` for identifier `%s` not found',
137+
$version,
138+
$entity->get('model')
139+
));
140+
}
130141
continue;
131142
}
143+
132144
$saveOptions = $options + ['format' => $entity->extension];
133145
if (isset($operations['_output'])) {
134146
$saveOptions = $operations['_output'] + $saveOptions;
@@ -155,7 +167,7 @@ public function createImageVersions(EntityInterface $entity, array $versions, ar
155167
'path' => $path,
156168
'hash' => $this->getImageVersionHash($entity->model, $version)
157169
];
158-
} catch (\Exception $e) {
170+
} catch (Exception $e) {
159171
$result[$version] = [
160172
'status' => 'error',
161173
'error' => $e->getMessage(),
@@ -207,7 +219,7 @@ public function removeImageVersions(EntityInterface $entity, array $versions, ar
207219
*/
208220
public function getAllVersionsKeysForModel($identifier) {
209221
if (!isset($this->_imageVersions[$identifier])) {
210-
throw new \RuntimeException(sprintf('No image config present for identifier "%s"!', $identifier));
222+
throw new RuntimeException(sprintf('No image config present for identifier "%s"!', $identifier));
211223
}
212224
return array_keys($this->_imageVersions[$identifier]);
213225
}

0 commit comments

Comments
 (0)