mageprince /
magento2-FAQ
| 1 | <?php |
||
| 2 | /** |
||
| 3 | * MagePrince |
||
| 4 | * |
||
| 5 | * NOTICE OF LICENSE |
||
| 6 | * |
||
| 7 | * This source file is subject to the mageprince.com license that is |
||
| 8 | * available through the world-wide-web at this URL: |
||
| 9 | * https://mageprince.com/end-user-license-agreement |
||
| 10 | * |
||
| 11 | * DISCLAIMER |
||
| 12 | * |
||
| 13 | * Do not edit or add to this file if you wish to upgrade this extension to newer |
||
| 14 | * version in the future. |
||
| 15 | * |
||
| 16 | * @category MagePrince |
||
| 17 | * @package Mageprince_Faq |
||
| 18 | * @copyright Copyright (c) MagePrince (https://mageprince.com/) |
||
| 19 | * @license https://mageprince.com/end-user-license-agreement |
||
| 20 | */ |
||
| 21 | |||
| 22 | namespace Mageprince\Faq\Model; |
||
| 23 | |||
| 24 | use Magento\Framework\App\Filesystem\DirectoryList; |
||
| 25 | use Magento\Framework\Exception\LocalizedException; |
||
| 26 | use Magento\Framework\Filesystem; |
||
| 27 | use Magento\Framework\Filesystem\Directory\WriteInterface; |
||
| 28 | use Magento\MediaStorage\Helper\File\Storage\Database; |
||
| 29 | use Magento\MediaStorage\Model\File\UploaderFactory; |
||
| 30 | use Magento\Store\Model\StoreManagerInterface; |
||
| 31 | use Psr\Log\LoggerInterface; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * Catalog image uploader |
||
| 35 | */ |
||
| 36 | class ImageUploader |
||
| 37 | { |
||
| 38 | /** |
||
| 39 | * @var Database |
||
| 40 | */ |
||
| 41 | private $coreFileStorageDatabase; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * @var WriteInterface |
||
| 45 | */ |
||
| 46 | private $mediaDirectory; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * @var UploaderFactory |
||
| 50 | */ |
||
| 51 | private $uploaderFactory; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * @var StoreManagerInterface |
||
| 55 | */ |
||
| 56 | private $storeManager; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * @var LoggerInterface |
||
| 60 | */ |
||
| 61 | private $logger; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * @var string |
||
| 65 | */ |
||
| 66 | public $baseTmpPath; |
||
| 67 | |||
| 68 | /** |
||
| 69 | * @var string |
||
| 70 | */ |
||
| 71 | public $basePath; |
||
| 72 | |||
| 73 | /** |
||
| 74 | * @var string |
||
| 75 | */ |
||
| 76 | public $allowedExtensions; |
||
| 77 | |||
| 78 | /** |
||
| 79 | * ImageUploader constructor. |
||
| 80 | * @param Database $coreFileStorageDatabase |
||
| 81 | * @param Filesystem $filesystem |
||
| 82 | * @param UploaderFactory $uploaderFactory |
||
| 83 | * @param StoreManagerInterface $storeManager |
||
| 84 | * @param LoggerInterface $logger |
||
| 85 | * @throws \Magento\Framework\Exception\FileSystemException |
||
| 86 | */ |
||
| 87 | public function __construct( |
||
| 88 | Database $coreFileStorageDatabase, |
||
| 89 | Filesystem $filesystem, |
||
| 90 | UploaderFactory $uploaderFactory, |
||
| 91 | StoreManagerInterface $storeManager, |
||
| 92 | LoggerInterface $logger |
||
| 93 | ) { |
||
| 94 | $this->coreFileStorageDatabase = $coreFileStorageDatabase; |
||
| 95 | $this->mediaDirectory = $filesystem->getDirectoryWrite(DirectoryList::MEDIA); |
||
| 96 | $this->uploaderFactory = $uploaderFactory; |
||
| 97 | $this->storeManager = $storeManager; |
||
| 98 | $this->logger = $logger; |
||
| 99 | $this->baseTmpPath = "faq/tmp/icon"; |
||
| 100 | $this->basePath = "faq/icon"; |
||
| 101 | $this->allowedExtensions= ['jpg', 'jpeg', 'gif', 'png']; |
||
|
0 ignored issues
–
show
|
|||
| 102 | } |
||
| 103 | |||
| 104 | /** |
||
| 105 | * Set base tmp path |
||
| 106 | * |
||
| 107 | * @param string $baseTmpPath |
||
| 108 | * |
||
| 109 | * @return void |
||
| 110 | */ |
||
| 111 | public function setBaseTmpPath($baseTmpPath) |
||
| 112 | { |
||
| 113 | $this->baseTmpPath = $baseTmpPath; |
||
| 114 | } |
||
| 115 | |||
| 116 | /** |
||
| 117 | * Set base path |
||
| 118 | * |
||
| 119 | * @param string $basePath |
||
| 120 | * |
||
| 121 | * @return void |
||
| 122 | */ |
||
| 123 | public function setBasePath($basePath) |
||
| 124 | { |
||
| 125 | $this->basePath = $basePath; |
||
| 126 | } |
||
| 127 | |||
| 128 | /** |
||
| 129 | * Set allowed extensions |
||
| 130 | * |
||
| 131 | * @param string[] $allowedExtensions |
||
| 132 | * |
||
| 133 | * @return void |
||
| 134 | */ |
||
| 135 | public function setAllowedExtensions($allowedExtensions) |
||
| 136 | { |
||
| 137 | $this->allowedExtensions = $allowedExtensions; |
||
|
0 ignored issues
–
show
It seems like
$allowedExtensions of type string[] is incompatible with the declared type string of property $allowedExtensions.
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property. Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property.. Loading history...
|
|||
| 138 | } |
||
| 139 | |||
| 140 | /** |
||
| 141 | * Retrieve base tmp path |
||
| 142 | * |
||
| 143 | * @return string |
||
| 144 | */ |
||
| 145 | public function getBaseTmpPath() |
||
| 146 | { |
||
| 147 | return $this->baseTmpPath; |
||
| 148 | } |
||
| 149 | |||
| 150 | /** |
||
| 151 | * Retrieve base path |
||
| 152 | * |
||
| 153 | * @return string |
||
| 154 | */ |
||
| 155 | public function getBasePath() |
||
| 156 | { |
||
| 157 | return $this->basePath; |
||
| 158 | } |
||
| 159 | |||
| 160 | /** |
||
| 161 | * Retrieve base path |
||
| 162 | * |
||
| 163 | * @return string[] |
||
| 164 | */ |
||
| 165 | public function getAllowedExtensions() |
||
| 166 | { |
||
| 167 | return $this->allowedExtensions; |
||
| 168 | } |
||
| 169 | |||
| 170 | /** |
||
| 171 | * Retrieve path |
||
| 172 | * |
||
| 173 | * @param string $path |
||
| 174 | * @param string $imageName |
||
| 175 | * |
||
| 176 | * @return string |
||
| 177 | */ |
||
| 178 | public function getFilePath($path, $imageName) |
||
| 179 | { |
||
| 180 | return rtrim($path, '/') . '/' . ltrim($imageName, '/'); |
||
| 181 | } |
||
| 182 | |||
| 183 | /** |
||
| 184 | * Checking file for moving and move it |
||
| 185 | * |
||
| 186 | * @param string $imageName |
||
| 187 | * |
||
| 188 | * @return string |
||
| 189 | * |
||
| 190 | * @throws LocalizedException |
||
| 191 | */ |
||
| 192 | public function moveFileFromTmp($imageName) |
||
| 193 | { |
||
| 194 | $baseTmpPath = $this->getBaseTmpPath(); |
||
| 195 | $basePath = $this->getBasePath(); |
||
| 196 | |||
| 197 | $baseImagePath = $this->getFilePath($basePath, $imageName); |
||
| 198 | $baseTmpImagePath = $this->getFilePath($baseTmpPath, $imageName); |
||
| 199 | |||
| 200 | try { |
||
| 201 | $this->coreFileStorageDatabase->copyFile( |
||
| 202 | $baseTmpImagePath, |
||
| 203 | $baseImagePath |
||
| 204 | ); |
||
| 205 | $this->mediaDirectory->renameFile( |
||
| 206 | $baseTmpImagePath, |
||
| 207 | $baseImagePath |
||
| 208 | ); |
||
| 209 | } catch (\Exception $e) { |
||
| 210 | throw new LocalizedException( |
||
| 211 | __('Something went wrong while saving the file(s).') |
||
| 212 | ); |
||
| 213 | } |
||
| 214 | |||
| 215 | return $imageName; |
||
| 216 | } |
||
| 217 | |||
| 218 | /** |
||
| 219 | * Checking file for save and save it to tmp dir |
||
| 220 | * |
||
| 221 | * @param string $fileId |
||
| 222 | * |
||
| 223 | * @return string[] |
||
| 224 | * |
||
| 225 | * @throws LocalizedException |
||
| 226 | */ |
||
| 227 | public function saveFileToTmpDir($fileId) |
||
| 228 | { |
||
| 229 | $baseTmpPath = $this->getBaseTmpPath(); |
||
| 230 | |||
| 231 | $uploader = $this->uploaderFactory->create(['fileId' => $fileId]); |
||
| 232 | $uploader->setAllowedExtensions($this->getAllowedExtensions()); |
||
| 233 | $uploader->setAllowRenameFiles(true); |
||
| 234 | |||
| 235 | $result = $uploader->save($this->mediaDirectory->getAbsolutePath($baseTmpPath)); |
||
| 236 | |||
| 237 | if (!$result) { |
||
| 238 | throw new LocalizedException( |
||
| 239 | __('File can not be saved to the destination folder.') |
||
| 240 | ); |
||
| 241 | } |
||
| 242 | |||
| 243 | /** |
||
| 244 | * Workaround for prototype 1.7 methods "isJSON", "evalJSON" on Windows OS |
||
| 245 | */ |
||
| 246 | $result['tmp_name'] = str_replace('\\', '/', $result['tmp_name']); |
||
| 247 | $result['path'] = str_replace('\\', '/', $result['path']); |
||
| 248 | $result['url'] = $this->storeManager |
||
| 249 | ->getStore() |
||
| 250 | ->getBaseUrl( |
||
| 251 | \Magento\Framework\UrlInterface::URL_TYPE_MEDIA |
||
| 252 | ) . $this->getFilePath($baseTmpPath, $result['file']); |
||
| 253 | $result['name'] = $result['file']; |
||
| 254 | |||
| 255 | if (isset($result['file'])) { |
||
| 256 | try { |
||
| 257 | $relativePath = rtrim($baseTmpPath, '/') . '/' . ltrim($result['file'], '/'); |
||
| 258 | $this->coreFileStorageDatabase->saveFile($relativePath); |
||
| 259 | } catch (\Exception $e) { |
||
| 260 | $this->logger->critical($e); |
||
| 261 | throw new LocalizedException( |
||
| 262 | __('Something went wrong while saving the file(s).') |
||
| 263 | ); |
||
| 264 | } |
||
| 265 | } |
||
| 266 | |||
| 267 | return $result; |
||
| 268 | } |
||
| 269 | } |
||
| 270 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..