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
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Swatches\Controller\Adminhtml\Iframe;
use Magento\Framework\App\Filesystem\DirectoryList;
/**
* Class to show swatch image and save it on disk
*/
class Show extends \Magento\Backend\App\Action
{
/**
* Authorization level of a basic admin session
*
* @see _isAllowed()
*/
const ADMIN_RESOURCE = 'Magento_Swatches::iframe';
/**
* Helper to move image from tmp to catalog
*
* @var \Magento\Swatches\Helper\Media
*/
protected $swatchHelper;
/**
* @var \Magento\Framework\Image\AdapterFactory
*/
protected $adapterFactory;
/**
* @var \Magento\Catalog\Model\Product\Media\Config
*/
protected $config;
/**
* @var \Magento\Framework\Filesystem
*/
protected $filesystem;
/**
* @var \Magento\MediaStorage\Model\File\UploaderFactory
*/
protected $uploaderFactory;
/**
* @param \Magento\Backend\App\Action\Context $context
* @param \Magento\Swatches\Helper\Media $swatchHelper
* @param \Magento\Framework\Image\AdapterFactory $adapterFactory
* @param \Magento\Catalog\Model\Product\Media\Config $config
* @param \Magento\Framework\Filesystem $filesystem
* @param \Magento\MediaStorage\Model\File\UploaderFactory $uploaderFactory
*/
public function __construct(
\Magento\Backend\App\Action\Context $context,
\Magento\Swatches\Helper\Media $swatchHelper,
\Magento\Framework\Image\AdapterFactory $adapterFactory,
\Magento\Catalog\Model\Product\Media\Config $config,
\Magento\Framework\Filesystem $filesystem,
\Magento\MediaStorage\Model\File\UploaderFactory $uploaderFactory
) {
$this->swatchHelper = $swatchHelper;
$this->adapterFactory = $adapterFactory;
$this->config = $config;
$this->filesystem = $filesystem;
$this->uploaderFactory = $uploaderFactory;
parent::__construct($context);
}
/**
* Image upload action in iframe
*
* @return string
*/
public function execute()
{
try {
$uploader = $this->uploaderFactory->create(['fileId' => 'datafile']);
$uploader->setAllowedExtensions(['jpg', 'jpeg', 'gif', 'png']);
/** @var \Magento\Framework\Image\Adapter\AdapterInterface $imageAdapter */
$imageAdapter = $this->adapterFactory->create();
$uploader->addValidateCallback('catalog_product_image', $imageAdapter, 'validateUploadFile');
$uploader->setAllowRenameFiles(true);
$uploader->setFilesDispersion(true);
/** @var \Magento\Framework\Filesystem\Directory\Read $mediaDirectory */
$mediaDirectory = $this->filesystem->getDirectoryRead(DirectoryList::MEDIA);
$config = $this->config;
$result = $uploader->save($mediaDirectory->getAbsolutePath($config->getBaseTmpMediaPath()));
unset($result['path']);
$this->_eventManager->dispatch(
'swatch_gallery_upload_image_after',
['result' => $result, 'action' => $this]
);
unset($result['tmp_name']);
$result['url'] = $this->config->getTmpMediaUrl($result['file']);
$result['file'] = $result['file'] . '.tmp';
$newFile = $this->swatchHelper->moveImageFromTmp($result['file']);
$this->swatchHelper->generateSwatchVariations($newFile);
$fileData = ['swatch_path' => $this->swatchHelper->getSwatchMediaUrl(), 'file_path' => $newFile];
$this->getResponse()->setBody(json_encode($fileData));
} catch (\Exception $e) {
$result = ['error' => $e->getMessage(), 'errorcode' => $e->getCode()];
$this->getResponse()->setBody(json_encode($result));
}
}
}