Skip to content

Commit abb8b4e

Browse files
author
Viktor Sevch
committed
MC-39340: Improve image upload
1 parent d54adb7 commit abb8b4e

File tree

3 files changed

+12
-13
lines changed
  • app/code/Magento/Catalog/Controller/Adminhtml/Product/Gallery
  • dev/tests/integration/testsuite/Magento/Catalog/Controller/Adminhtml/Product/Gallery
  • lib/internal/Magento/Framework/Image/Adapter

3 files changed

+12
-13
lines changed

app/code/Magento/Catalog/Controller/Adminhtml/Product/Gallery/Upload.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
use Magento\Framework\Exception\LocalizedException;
1212

1313
/**
14-
* Class Upload
14+
* Class Upload image(s)
1515
*/
1616
class Upload extends \Magento\Backend\App\Action implements HttpPostActionInterface
1717
{
@@ -108,8 +108,10 @@ public function execute()
108108

109109
$result['url'] = $this->productMediaConfig->getTmpMediaUrl($result['file']);
110110
$result['file'] = $result['file'] . '.tmp';
111-
} catch (\Exception $e) {
111+
} catch (LocalizedException $e) {
112112
$result = ['error' => $e->getMessage(), 'errorcode' => $e->getCode()];
113+
} catch (\Throwable $e) {
114+
$result = ['error' => 'Something went wrong while saving the file(s).', 'errorcode' => 0];
113115
}
114116

115117
/** @var \Magento\Framework\Controller\Result\Raw $response */

dev/tests/integration/testsuite/Magento/Catalog/Controller/Adminhtml/Product/Gallery/UploadTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -198,15 +198,15 @@ public function uploadActionWithErrorsDataProvider(): array
198198
'current_path' => '/../../../../_files',
199199
],
200200
'expectation' => [
201-
'message' => 'Wrong file size.',
201+
'message' => 'Something went wrong while saving the file(s).',
202202
'errorcode' => 0,
203203
'tmp_media_path' => '/m/a/magento_empty.jpg',
204204
],
205205
],
206206
'upload_without_image' => [
207207
'file' => [],
208208
'expectation' => [
209-
'message' => '$_FILES array is empty',
209+
'message' => 'Something went wrong while saving the file(s).',
210210
'errorcode' => 0,
211211
],
212212
],
@@ -218,7 +218,7 @@ public function uploadActionWithErrorsDataProvider(): array
218218
'current_path' => '/../../../../_files',
219219
],
220220
'expectation' => [
221-
'message' => 'Unsupported image format.',
221+
'message' => 'Something went wrong while saving the file(s).',
222222
'errorcode' => 0,
223223
'tmp_media_path' => '/m/w/magento_wrong.png',
224224
],

lib/internal/Magento/Framework/Image/Adapter/Gd2.php

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66

77
namespace Magento\Framework\Image\Adapter;
88

9-
use Magento\Framework\Exception\LocalizedException;
10-
119
/**
1210
* Gd2 adapter.
1311
*
@@ -58,7 +56,7 @@ protected function _reset()
5856
*
5957
* @param string $filename
6058
* @return void
61-
* @throws \OverflowException|LocalizedException
59+
* @throws \OverflowException
6260
*/
6361
public function open($filename)
6462
{
@@ -73,11 +71,10 @@ public function open($filename)
7371
throw new \OverflowException('Memory limit has been reached.');
7472
}
7573
$this->imageDestroy();
76-
try {
77-
$this->_imageHandler = call_user_func($this->_getCallback('create'), $this->_fileName);
78-
} catch (\Throwable $ex) {
79-
throw new LocalizedException(__('Unsupported image format.'));
80-
}
74+
$this->_imageHandler = call_user_func(
75+
$this->_getCallback('create', null, sprintf('Unsupported image format. File: %s', $this->_fileName)),
76+
$this->_fileName
77+
);
8178
}
8279

8380
/**

0 commit comments

Comments
 (0)