ImageUploader.php 7.1 KB

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