Uploader.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\DownloadableImportExport\Helper;
  7. use Magento\Framework\App\Filesystem\DirectoryList;
  8. /**
  9. * Class Uploader
  10. */
  11. class Uploader extends \Magento\Framework\App\Helper\AbstractHelper
  12. {
  13. /**
  14. * Media files uploader
  15. *
  16. * @var \Magento\CatalogImportExport\Model\Import\Uploader
  17. */
  18. protected $fileUploader;
  19. /**
  20. * File helper downloadable product
  21. *
  22. * @var \Magento\Downloadable\Helper\File
  23. */
  24. protected $fileHelper;
  25. /**
  26. * @var \Magento\Framework\Filesystem\Directory\WriteInterface
  27. */
  28. protected $mediaDirectory;
  29. /**
  30. * Entity model parameters.
  31. *
  32. * @var array
  33. */
  34. protected $parameters = [];
  35. /**
  36. * @var \Magento\Framework\DB\Adapter\AdapterInterface
  37. */
  38. public $connection;
  39. /**
  40. * Construct
  41. *
  42. * @param \Magento\Framework\App\Helper\Context $context
  43. * @param \Magento\Downloadable\Helper\File $fileHelper
  44. * @param \Magento\CatalogImportExport\Model\Import\UploaderFactory $uploaderFactory
  45. * @param \Magento\Framework\App\ResourceConnection $resource
  46. * @param \Magento\Framework\Filesystem $filesystem
  47. */
  48. public function __construct(
  49. \Magento\Framework\App\Helper\Context $context,
  50. \Magento\Downloadable\Helper\File $fileHelper,
  51. \Magento\CatalogImportExport\Model\Import\UploaderFactory $uploaderFactory,
  52. \Magento\Framework\App\ResourceConnection $resource,
  53. \Magento\Framework\Filesystem $filesystem
  54. ) {
  55. parent::__construct($context);
  56. $this->fileHelper = $fileHelper;
  57. $this->fileUploader = $uploaderFactory->create();
  58. $this->fileUploader->init();
  59. $this->fileUploader->setAllowedExtensions($this->getAllowedExtensions());
  60. $this->fileUploader->removeValidateCallback('catalog_product_image');
  61. $this->connection = $resource->getConnection('write');
  62. $this->mediaDirectory = $filesystem->getDirectoryWrite(DirectoryList::ROOT);
  63. }
  64. /**
  65. * Returns an object for upload a media files
  66. *
  67. * @param string $type
  68. * @param array $parameters
  69. * @return \Magento\CatalogImportExport\Model\Import\Uploader
  70. * @throws \Magento\Framework\Exception\LocalizedException
  71. */
  72. public function getUploader($type, $parameters)
  73. {
  74. $dirConfig = DirectoryList::getDefaultConfig();
  75. $dirAddon = $dirConfig[DirectoryList::MEDIA][DirectoryList::PATH];
  76. if (!empty($parameters[\Magento\ImportExport\Model\Import::FIELD_NAME_IMG_FILE_DIR])) {
  77. $tmpPath = $parameters[\Magento\ImportExport\Model\Import::FIELD_NAME_IMG_FILE_DIR];
  78. } else {
  79. $tmpPath = $dirAddon . '/' . $this->mediaDirectory->getRelativePath('import');
  80. }
  81. if (!$this->fileUploader->setTmpDir($tmpPath)) {
  82. throw new \Magento\Framework\Exception\LocalizedException(
  83. __('File directory \'%1\' is not readable.', $tmpPath)
  84. );
  85. }
  86. $destinationDir = "downloadable/files/" . $type;
  87. $destinationPath = $dirAddon . '/' . $this->mediaDirectory->getRelativePath($destinationDir);
  88. $this->mediaDirectory->create($destinationPath);
  89. if (!$this->fileUploader->setDestDir($destinationPath)) {
  90. throw new \Magento\Framework\Exception\LocalizedException(
  91. __('File directory \'%1\' is not writable.', $destinationPath)
  92. );
  93. }
  94. return $this->fileUploader;
  95. }
  96. /**
  97. * Get all allowed extensions
  98. *
  99. * @return array
  100. */
  101. protected function getAllowedExtensions()
  102. {
  103. $result = [];
  104. foreach (array_keys($this->fileHelper->getAllMineTypes()) as $option) {
  105. $result[] = substr($option, 1);
  106. }
  107. return $result;
  108. }
  109. }