Skip to content

Commit c43fc5e

Browse files
author
Sergey Semenov
committed
MAGETWO-42954: Can't view uploaded image
1 parent 3fb5616 commit c43fc5e

File tree

13 files changed

+1276
-763
lines changed

13 files changed

+1276
-763
lines changed
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
<?php
2+
/**
3+
* Copyright © 2016 Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Customer\Controller\Adminhtml\File\Address;
7+
8+
use Magento\Backend\App\Action;
9+
use Magento\Backend\App\Action\Context;
10+
use Magento\Customer\Api\AddressMetadataInterface;
11+
use Magento\Customer\Model\FileUploader;
12+
use Magento\Customer\Model\FileUploaderFactory;
13+
use Magento\Framework\Controller\ResultFactory;
14+
use Magento\Framework\Exception\LocalizedException;
15+
use Psr\Log\LoggerInterface;
16+
17+
class Upload extends Action
18+
{
19+
/**
20+
* Authorization level of a basic admin session
21+
*
22+
* @see _isAllowed()
23+
*/
24+
const ADMIN_RESOURCE = 'Magento_Customer::manage';
25+
26+
/**
27+
* @var FileUploaderFactory
28+
*/
29+
private $fileUploaderFactory;
30+
31+
/**
32+
* @var AddressMetadataInterface
33+
*/
34+
private $addressMetadataService;
35+
36+
/**
37+
* @var LoggerInterface
38+
*/
39+
private $logger;
40+
41+
/**
42+
* @param Context $context
43+
* @param FileUploaderFactory $fileUploaderFactory
44+
* @param AddressMetadataInterface $addressMetadataService
45+
* @param LoggerInterface $logger
46+
*/
47+
public function __construct(
48+
Context $context,
49+
FileUploaderFactory $fileUploaderFactory,
50+
AddressMetadataInterface $addressMetadataService,
51+
LoggerInterface $logger
52+
) {
53+
$this->fileUploaderFactory = $fileUploaderFactory;
54+
$this->addressMetadataService = $addressMetadataService;
55+
$this->logger = $logger;
56+
parent::__construct($context);
57+
}
58+
59+
/**
60+
* @inheritDoc
61+
*/
62+
public function execute()
63+
{
64+
try {
65+
if (empty($_FILES)) {
66+
throw new \Exception('$_FILES array is empty.');
67+
}
68+
69+
// Must be executed before any operations with $_FILES!
70+
$this->convertFilesArray();
71+
72+
$attributeCode = key($_FILES['address']['name']);
73+
$attributeMetadata = $this->addressMetadataService->getAttributeMetadata($attributeCode);
74+
75+
/** @var FileUploader $fileUploader */
76+
$fileUploader = $this->fileUploaderFactory->create([
77+
'attributeMetadata' => $attributeMetadata,
78+
'entityTypeCode' => AddressMetadataInterface::ENTITY_TYPE_ADDRESS,
79+
'scope' => 'address',
80+
]);
81+
82+
$errors = $fileUploader->validate();
83+
if (true !== $errors) {
84+
$errorMessage = implode('</br>', $errors);
85+
throw new LocalizedException(__($errorMessage));
86+
}
87+
88+
$result = $fileUploader->upload();
89+
} catch (LocalizedException $e) {
90+
$result = [
91+
'error' => $e->getMessage(),
92+
'errorcode' => $e->getCode(),
93+
];
94+
} catch (\Exception $e) {
95+
$this->logger->critical($e);
96+
$result = [
97+
'error' => __('Something went wrong while saving file.'),
98+
'errorcode' => $e->getCode(),
99+
];
100+
}
101+
102+
/** @var \Magento\Framework\Controller\Result\Json $resultJson */
103+
$resultJson = $this->resultFactory->create(ResultFactory::TYPE_JSON);
104+
$resultJson->setData($result);
105+
return $resultJson;
106+
}
107+
108+
/**
109+
* Update global $_FILES array. Convert data to standard form
110+
*
111+
* NOTE: This conversion is required to use \Magento\Framework\File\Uploader::_setUploadFileId($fileId) method.
112+
*
113+
* @return void
114+
*/
115+
private function convertFilesArray()
116+
{
117+
foreach($_FILES['address'] as $itemKey => $item) {
118+
foreach ($item as $value) {
119+
if (is_array($value)) {
120+
$_FILES['address'][$itemKey] = [
121+
key($value) => current($value),
122+
];
123+
}
124+
}
125+
}
126+
}
127+
}
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
<?php
2+
/**
3+
* Copyright © 2016 Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Customer\Controller\Adminhtml\File\Customer;
7+
8+
use Magento\Backend\App\Action;
9+
use Magento\Backend\App\Action\Context;
10+
use Magento\Customer\Api\CustomerMetadataInterface;
11+
use Magento\Customer\Model\FileUploader;
12+
use Magento\Customer\Model\FileUploaderFactory;
13+
use Magento\Framework\Controller\ResultFactory;
14+
use Magento\Framework\Exception\LocalizedException;
15+
use Psr\Log\LoggerInterface;
16+
17+
class Upload extends Action
18+
{
19+
/**
20+
* Authorization level of a basic admin session
21+
*
22+
* @see _isAllowed()
23+
*/
24+
const ADMIN_RESOURCE = 'Magento_Customer::manage';
25+
26+
/**
27+
* @var FileUploaderFactory
28+
*/
29+
private $fileUploaderFactory;
30+
31+
/**
32+
* @var CustomerMetadataInterface
33+
*/
34+
private $customerMetadataService;
35+
36+
/**
37+
* @var LoggerInterface
38+
*/
39+
private $logger;
40+
41+
/**
42+
* @param Context $context
43+
* @param FileUploaderFactory $fileUploaderFactory
44+
* @param CustomerMetadataInterface $customerMetadataService
45+
* @param LoggerInterface $logger
46+
*/
47+
public function __construct(
48+
Context $context,
49+
FileUploaderFactory $fileUploaderFactory,
50+
CustomerMetadataInterface $customerMetadataService,
51+
LoggerInterface $logger
52+
) {
53+
$this->fileUploaderFactory = $fileUploaderFactory;
54+
$this->customerMetadataService = $customerMetadataService;
55+
$this->logger = $logger;
56+
parent::__construct($context);
57+
}
58+
59+
/**
60+
* @inheritDoc
61+
*/
62+
public function execute()
63+
{
64+
try {
65+
if (empty($_FILES)) {
66+
throw new \Exception('$_FILES array is empty.');
67+
}
68+
69+
$attributeCode = key($_FILES['customer']['name']);
70+
$attributeMetadata = $this->customerMetadataService->getAttributeMetadata($attributeCode);
71+
72+
/** @var FileUploader $fileUploader */
73+
$fileUploader = $this->fileUploaderFactory->create([
74+
'attributeMetadata' => $attributeMetadata,
75+
'entityTypeCode' => CustomerMetadataInterface::ENTITY_TYPE_CUSTOMER,
76+
'scope' => 'customer',
77+
]);
78+
79+
$errors = $fileUploader->validate();
80+
if (true !== $errors) {
81+
$errorMessage = implode('</br>', $errors);
82+
throw new LocalizedException(__($errorMessage));
83+
}
84+
85+
$result = $fileUploader->upload();
86+
} catch (LocalizedException $e) {
87+
$result = [
88+
'error' => $e->getMessage(),
89+
'errorcode' => $e->getCode(),
90+
];
91+
} catch (\Exception $e) {
92+
$this->logger->critical($e);
93+
$result = [
94+
'error' => __('Something went wrong while saving file.'),
95+
'errorcode' => $e->getCode(),
96+
];
97+
}
98+
99+
/** @var \Magento\Framework\Controller\Result\Json $resultJson */
100+
$resultJson = $this->resultFactory->create(ResultFactory::TYPE_JSON);
101+
$resultJson->setData($result);
102+
return $resultJson;
103+
}
104+
}

0 commit comments

Comments
 (0)