|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types = 1); |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* File: Upload.php |
|
7
|
|
|
* |
|
8
|
|
|
* @author Bartosz Kubicki [email protected]> |
|
9
|
|
|
* @copyright Copyright (C) 2018 Lizard Media (http://lizardmedia.pl) |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace LizardMedia\ProductAttachment\Controller\Adminhtml\Attachment\File; |
|
13
|
|
|
|
|
14
|
|
|
use Exception; |
|
15
|
|
|
use LizardMedia\ProductAttachment\Api\Data\AttachmentInterface; |
|
16
|
|
|
use LizardMedia\ProductAttachment\Model\Attachment; |
|
17
|
|
|
use Magento\Backend\App\Action\Context; |
|
18
|
|
|
use Magento\Downloadable\Controller\Adminhtml\Downloadable\File; |
|
19
|
|
|
use Magento\Downloadable\Helper\File as FileHelper; |
|
20
|
|
|
use Magento\Framework\Controller\ResultFactory; |
|
21
|
|
|
use Magento\Framework\Controller\ResultInterface; |
|
22
|
|
|
use Magento\Framework\Exception\FileSystemException; |
|
23
|
|
|
use Magento\MediaStorage\Helper\File\Storage\Database; |
|
24
|
|
|
use Magento\MediaStorage\Model\File\UploaderFactory; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Class Upload |
|
28
|
|
|
* @package LizardMedia\ProductAttachment\Controller\Adminhtml\Attachment\File |
|
29
|
|
|
*/ |
|
30
|
|
|
class Upload extends File |
|
31
|
|
|
{ |
|
32
|
|
|
/** |
|
33
|
|
|
* @var string |
|
34
|
|
|
*/ |
|
35
|
|
|
const DOWNLOADABLE_ATTACHEMENT_TYPE = 'attachments'; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @var AttachmentInterface |
|
39
|
|
|
*/ |
|
40
|
|
|
private $attachment; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @var FileHelper |
|
44
|
|
|
*/ |
|
45
|
|
|
private $fileHelper; |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @var Database |
|
49
|
|
|
*/ |
|
50
|
|
|
private $storageDatabase; |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @var UploaderFactory |
|
54
|
|
|
*/ |
|
55
|
|
|
private $uploaderFactory; |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* @param AttachmentInterface $attachment |
|
59
|
|
|
* @param Context $context |
|
60
|
|
|
* @param FileHelper $fileHelper |
|
61
|
|
|
* @param Database $storageDatabase |
|
62
|
|
|
* @param UploaderFactory $uploaderFactory |
|
63
|
|
|
*/ |
|
64
|
|
|
public function __construct( |
|
65
|
|
|
AttachmentInterface $attachment, |
|
66
|
|
|
Context $context, |
|
67
|
|
|
FileHelper $fileHelper, |
|
68
|
|
|
Database $storageDatabase, |
|
69
|
|
|
UploaderFactory $uploaderFactory |
|
70
|
|
|
) { |
|
71
|
|
|
parent::__construct($context); |
|
72
|
|
|
$this->attachment = $attachment; |
|
73
|
|
|
$this->fileHelper = $fileHelper; |
|
74
|
|
|
$this->uploaderFactory = $uploaderFactory; |
|
75
|
|
|
$this->storageDatabase = $storageDatabase; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* @return ResultInterface |
|
80
|
|
|
*/ |
|
81
|
|
|
public function execute() : ResultInterface |
|
82
|
|
|
{ |
|
83
|
|
|
$type = $this->getRequest()->getParam(Attachment::ATTACHMENT_TYPE); |
|
84
|
|
|
$tmpPath = ''; |
|
85
|
|
|
|
|
86
|
|
|
if ($type === self::DOWNLOADABLE_ATTACHEMENT_TYPE) { |
|
87
|
|
|
$tmpPath = $this->attachment->getBaseTmpPath(); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
try { |
|
91
|
|
|
$uploader = $this->uploaderFactory->create(['fileId' => $type]); |
|
92
|
|
|
$result = $this->fileHelper->uploadFromTmp($tmpPath, $uploader); |
|
93
|
|
|
|
|
94
|
|
|
if (!$result) { |
|
95
|
|
|
throw new FileSystemException(__('File can not be moved from temporary folder to the destination folder.')); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
unset($result['tmp_name'], $result['path']); |
|
99
|
|
|
|
|
100
|
|
|
if (isset($result[Attachment::ATTACHMENT_FILE])) { |
|
101
|
|
|
$relativePath = rtrim( |
|
102
|
|
|
$tmpPath, |
|
103
|
|
|
DIRECTORY_SEPARATOR |
|
104
|
|
|
) |
|
105
|
|
|
. DIRECTORY_SEPARATOR . ltrim($result[Attachment::ATTACHMENT_FILE], DIRECTORY_SEPARATOR); |
|
106
|
|
|
|
|
107
|
|
|
$this->storageDatabase->saveFile($relativePath); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
$result['cookie'] = [ |
|
111
|
|
|
'name' => $this->_getSession()->getName(), |
|
112
|
|
|
'value' => $this->_getSession()->getSessionId(), |
|
113
|
|
|
'lifetime' => $this->_getSession()->getCookieLifetime(), |
|
114
|
|
|
'path' => $this->_getSession()->getCookiePath(), |
|
115
|
|
|
'domain' => $this->_getSession()->getCookieDomain(), |
|
116
|
|
|
]; |
|
117
|
|
|
} catch (Exception $exception) { |
|
118
|
|
|
$result = ['error' => $exception->getMessage(), 'errorcode' => $exception->getCode()]; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
return $this->resultFactory->create(ResultFactory::TYPE_JSON)->setData($result); |
|
122
|
|
|
} |
|
123
|
|
|
} |
|
124
|
|
|
|