|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | + |
| 7 | +namespace <Namespace>\<Module>\Model; |
| 8 | + |
| 9 | +use Magento\Framework\App\Filesystem\DirectoryList; |
| 10 | +use Magento\Framework\App\ObjectManager; |
| 11 | +use Magento\Framework\Exception\LocalizedException; |
| 12 | +use Magento\Framework\File\Name; |
| 13 | +use Magento\Framework\Filesystem; |
| 14 | +use Magento\Framework\Filesystem\Directory\WriteInterface; |
| 15 | +use Magento\MediaStorage\Helper\File\Storage\Database; |
| 16 | +use Magento\MediaStorage\Model\File\UploaderFactory; |
| 17 | +use Magento\Store\Model\StoreManagerInterface; |
| 18 | +use Psr\Log\LoggerInterface; |
| 19 | + |
| 20 | +/** |
| 21 | + * Catalog image uploader |
| 22 | + */ |
| 23 | +class ImageUploader |
| 24 | +{ |
| 25 | + /** |
| 26 | + * @var Database |
| 27 | + */ |
| 28 | + protected $coreFileStorageDatabase; |
| 29 | + |
| 30 | + /** |
| 31 | + * @var WriteInterface |
| 32 | + */ |
| 33 | + protected $mediaDirectory; |
| 34 | + |
| 35 | + /** |
| 36 | + * @var UploaderFactory |
| 37 | + */ |
| 38 | + private $uploaderFactory; |
| 39 | + |
| 40 | + /** |
| 41 | + * @var StoreManagerInterface |
| 42 | + */ |
| 43 | + protected $storeManager; |
| 44 | + |
| 45 | + /** |
| 46 | + * @var LoggerInterface |
| 47 | + */ |
| 48 | + protected $logger; |
| 49 | + |
| 50 | + /** |
| 51 | + * @var string |
| 52 | + */ |
| 53 | + protected $baseTmpPath; |
| 54 | + |
| 55 | + /** |
| 56 | + * @var string |
| 57 | + */ |
| 58 | + protected $basePath; |
| 59 | + |
| 60 | + /** |
| 61 | + * @var string |
| 62 | + */ |
| 63 | + protected $allowedExtensions; |
| 64 | + |
| 65 | + /** |
| 66 | + * @var string[] |
| 67 | + */ |
| 68 | + private $allowedMimeTypes; |
| 69 | + |
| 70 | + /** |
| 71 | + * @var Name |
| 72 | + */ |
| 73 | + private $fileNameLookup; |
| 74 | + |
| 75 | + /** |
| 76 | + * ImageUploader constructor. |
| 77 | + * |
| 78 | + * @param Database $coreFileStorageDatabase |
| 79 | + * @param Filesystem $filesystem |
| 80 | + * @param UploaderFactory $uploaderFactory |
| 81 | + * @param StoreManagerInterface $storeManager |
| 82 | + * @param LoggerInterface $logger |
| 83 | + * @param string $baseTmpPath |
| 84 | + * @param string $basePath |
| 85 | + * @param string[] $allowedExtensions |
| 86 | + * @param string[] $allowedMimeTypes |
| 87 | + * @param Name|null $fileNameLookup |
| 88 | + * @SuppressWarnings(PHPMD.ExcessiveParameterList) |
| 89 | + */ |
| 90 | + public function __construct( |
| 91 | + Database $coreFileStorageDatabase, |
| 92 | + Filesystem $filesystem, |
| 93 | + UploaderFactory $uploaderFactory, |
| 94 | + StoreManagerInterface $storeManager, |
| 95 | + LoggerInterface $logger, |
| 96 | + $baseTmpPath, |
| 97 | + $basePath, |
| 98 | + $allowedExtensions, |
| 99 | + $allowedMimeTypes = [], |
| 100 | + Name $fileNameLookup = null |
| 101 | + ) { |
| 102 | + $this->coreFileStorageDatabase = $coreFileStorageDatabase; |
| 103 | + $this->mediaDirectory = $filesystem->getDirectoryWrite(DirectoryList::MEDIA); |
| 104 | + $this->uploaderFactory = $uploaderFactory; |
| 105 | + $this->storeManager = $storeManager; |
| 106 | + $this->logger = $logger; |
| 107 | + $this->baseTmpPath = $baseTmpPath; |
| 108 | + $this->basePath = $basePath; |
| 109 | + $this->allowedExtensions = $allowedExtensions; |
| 110 | + $this->allowedMimeTypes = $allowedMimeTypes; |
| 111 | + $this->fileNameLookup = $fileNameLookup ?? ObjectManager::getInstance()->get(Name::class); |
| 112 | + } |
| 113 | + |
| 114 | + /** |
| 115 | + * Set base tmp path |
| 116 | + * |
| 117 | + * @param string $baseTmpPath |
| 118 | + * @return void |
| 119 | + */ |
| 120 | + public function setBaseTmpPath($baseTmpPath) |
| 121 | + { |
| 122 | + $this->baseTmpPath = $baseTmpPath; |
| 123 | + } |
| 124 | + |
| 125 | + /** |
| 126 | + * Set base path |
| 127 | + * |
| 128 | + * @param string $basePath |
| 129 | + * @return void |
| 130 | + */ |
| 131 | + public function setBasePath($basePath) |
| 132 | + { |
| 133 | + $this->basePath = $basePath; |
| 134 | + } |
| 135 | + |
| 136 | + /** |
| 137 | + * Set allowed extensions |
| 138 | + * |
| 139 | + * @param string[] $allowedExtensions |
| 140 | + * @return void |
| 141 | + */ |
| 142 | + public function setAllowedExtensions($allowedExtensions) |
| 143 | + { |
| 144 | + $this->allowedExtensions = $allowedExtensions; |
| 145 | + } |
| 146 | + |
| 147 | + /** |
| 148 | + * Retrieve base tmp path |
| 149 | + * |
| 150 | + * @return string |
| 151 | + */ |
| 152 | + public function getBaseTmpPath() |
| 153 | + { |
| 154 | + return $this->baseTmpPath; |
| 155 | + } |
| 156 | + |
| 157 | + /** |
| 158 | + * Retrieve base path |
| 159 | + * |
| 160 | + * @return string |
| 161 | + */ |
| 162 | + public function getBasePath() |
| 163 | + { |
| 164 | + return '<namespace>/<module>';//$this->basePath; |
| 165 | + } |
| 166 | + |
| 167 | + /** |
| 168 | + * Retrieve allowed extensions |
| 169 | + * |
| 170 | + * @return string[] |
| 171 | + */ |
| 172 | + public function getAllowedExtensions() |
| 173 | + { |
| 174 | + return $this->allowedExtensions; |
| 175 | + } |
| 176 | + |
| 177 | + /** |
| 178 | + * Retrieve path |
| 179 | + * |
| 180 | + * @param string $path |
| 181 | + * @param string $imageName |
| 182 | + * @return string |
| 183 | + */ |
| 184 | + public function getFilePath($path, $imageName) |
| 185 | + { |
| 186 | + $path = $path !== null ? rtrim($path, '/') : ''; |
| 187 | + $imageName = $imageName !== null ? ltrim($imageName, '/') : ''; |
| 188 | + return $path . '/' . $imageName; |
| 189 | + } |
| 190 | + |
| 191 | + /** |
| 192 | + * Checking file for moving and move it |
| 193 | + * |
| 194 | + * @param string $imageName |
| 195 | + * @param bool $returnRelativePath |
| 196 | + * @return string |
| 197 | + * |
| 198 | + * @throws LocalizedException |
| 199 | + */ |
| 200 | + public function moveFileFromTmp($imageName, $returnRelativePath = false) |
| 201 | + { |
| 202 | + $baseTmpPath = $this->getBaseTmpPath(); |
| 203 | + $basePath = $this->getBasePath(); |
| 204 | + |
| 205 | + $baseImagePath = $this->getFilePath( |
| 206 | + $basePath, |
| 207 | + $this->fileNameLookup->getNewFileName( |
| 208 | + $this->mediaDirectory->getAbsolutePath( |
| 209 | + $this->getFilePath($basePath, $imageName) |
| 210 | + ) |
| 211 | + ) |
| 212 | + ); |
| 213 | + $baseTmpImagePath = $this->getFilePath($baseTmpPath, $imageName); |
| 214 | + try { |
| 215 | + $this->coreFileStorageDatabase->renameFile( |
| 216 | + $baseTmpImagePath, |
| 217 | + $baseImagePath |
| 218 | + ); |
| 219 | + $this->mediaDirectory->renameFile( |
| 220 | + $baseTmpImagePath, |
| 221 | + $baseImagePath |
| 222 | + ); |
| 223 | + } catch (\Exception $e) { |
| 224 | + $this->logger->critical($e); |
| 225 | + throw new LocalizedException(__('Something went wrong while saving the file(s).'), $e); |
| 226 | + } |
| 227 | + |
| 228 | + return $returnRelativePath ? $baseImagePath : $imageName; |
| 229 | + } |
| 230 | + |
| 231 | + /** |
| 232 | + * Checking file for save and save it to tmp dir |
| 233 | + * |
| 234 | + * @param string $fileId |
| 235 | + * @return string[] |
| 236 | + * |
| 237 | + * @throws LocalizedException |
| 238 | + */ |
| 239 | + public function saveFileToTmpDir($fileId) |
| 240 | + { |
| 241 | + $baseTmpPath = '<namespace>/tmp/<module>';//$this->getBaseTmpPath(); |
| 242 | + |
| 243 | + /** @var \Magento\MediaStorage\Model\File\Uploader $uploader */ |
| 244 | + $uploader = $this->uploaderFactory->create(['fileId' => $fileId]); |
| 245 | + $uploader->setAllowedExtensions($this->getAllowedExtensions()); |
| 246 | + $uploader->setAllowRenameFiles(true); |
| 247 | + if (!$uploader->checkMimeType($this->allowedMimeTypes)) { |
| 248 | + throw new LocalizedException(__('File validation failed.')); |
| 249 | + } |
| 250 | + $result = $uploader->save($this->mediaDirectory->getAbsolutePath($baseTmpPath)); |
| 251 | + |
| 252 | + if (!$result) { |
| 253 | + throw new LocalizedException(__('File can not be saved to the destination folder.')); |
| 254 | + } |
| 255 | + unset($result['path']); |
| 256 | + |
| 257 | + /** |
| 258 | + * Workaround for prototype 1.7 methods "isJSON", "evalJSON" on Windows OS |
| 259 | + */ |
| 260 | + $result['tmp_name'] = isset($result['tmp_name']) ? str_replace('\\', '/', $result['tmp_name']) : ''; |
| 261 | + $result['url'] = $this->storeManager |
| 262 | + ->getStore() |
| 263 | + ->getBaseUrl( |
| 264 | + \Magento\Framework\UrlInterface::URL_TYPE_MEDIA |
| 265 | + ) . $this->getFilePath($baseTmpPath, $result['file']); |
| 266 | + $result['name'] = $result['file']; |
| 267 | + |
| 268 | + if (isset($result['file'])) { |
| 269 | + try { |
| 270 | + $relativePath = rtrim($baseTmpPath, '/') . '/' . ltrim($result['file'], '/'); |
| 271 | + $this->coreFileStorageDatabase->saveFile($relativePath); |
| 272 | + } catch (\Exception $e) { |
| 273 | + $this->logger->critical($e); |
| 274 | + throw new LocalizedException( |
| 275 | + __('Something went wrong while saving the file(s).'), |
| 276 | + $e |
| 277 | + ); |
| 278 | + } |
| 279 | + } |
| 280 | + |
| 281 | + return $result; |
| 282 | + } |
| 283 | +} |
0 commit comments