Uploader.php 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\View\Design\Theme\Image;
  7. /**
  8. * Theme Image Uploader
  9. */
  10. class Uploader
  11. {
  12. /**
  13. * Allowed file extensions to upload
  14. *
  15. * @var array
  16. */
  17. protected $_allowedExtensions = ['jpg', 'jpeg', 'gif', 'png', 'xbm', 'wbmp'];
  18. /**
  19. * File system
  20. *
  21. * @var \Magento\Framework\Filesystem
  22. */
  23. protected $_filesystem;
  24. /**
  25. * Transfer adapter
  26. *
  27. * @var \Zend_File_Transfer_Adapter_Http
  28. */
  29. protected $_transferAdapter;
  30. /**
  31. * Uploader factory
  32. *
  33. * @var \Magento\Framework\File\UploaderFactory
  34. */
  35. protected $_uploaderFactory;
  36. /**
  37. * Constructor
  38. *
  39. * @param \Magento\Framework\Filesystem $filesystem
  40. * @param \Magento\Framework\HTTP\Adapter\FileTransferFactory $adapterFactory
  41. * @param \Magento\Framework\File\UploaderFactory $uploaderFactory
  42. */
  43. public function __construct(
  44. \Magento\Framework\Filesystem $filesystem,
  45. \Magento\Framework\HTTP\Adapter\FileTransferFactory $adapterFactory,
  46. \Magento\Framework\File\UploaderFactory $uploaderFactory
  47. ) {
  48. $this->_filesystem = $filesystem;
  49. $this->_transferAdapter = $adapterFactory->create();
  50. $this->_uploaderFactory = $uploaderFactory;
  51. }
  52. /**
  53. * Upload preview image
  54. *
  55. * @param string $scope the request key for file
  56. * @param string $destinationPath path to upload directory
  57. * @return bool
  58. * @throws \Magento\Framework\Exception\LocalizedException
  59. */
  60. public function uploadPreviewImage($scope, $destinationPath)
  61. {
  62. if (!$this->_transferAdapter->isUploaded($scope)) {
  63. return false;
  64. }
  65. if (!$this->_transferAdapter->isValid($scope)) {
  66. throw new \Magento\Framework\Exception\LocalizedException(
  67. new \Magento\Framework\Phrase('Uploaded image is not valid')
  68. );
  69. }
  70. $upload = $this->_uploaderFactory->create(['fileId' => $scope]);
  71. $upload->setAllowCreateFolders(true);
  72. $upload->setAllowedExtensions($this->_allowedExtensions);
  73. $upload->setAllowRenameFiles(true);
  74. $upload->setFilesDispersion(false);
  75. if (!$upload->checkAllowedExtension($upload->getFileExtension())) {
  76. throw new \Magento\Framework\Exception\LocalizedException(
  77. new \Magento\Framework\Phrase('Invalid image file type.')
  78. );
  79. }
  80. if (!$upload->save($destinationPath)) {
  81. throw new \Magento\Framework\Exception\LocalizedException(
  82. new \Magento\Framework\Phrase('Image can not be saved.')
  83. );
  84. }
  85. return $destinationPath . '/' . $upload->getUploadedFileName();
  86. }
  87. }