Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Uploader.php
Go to the documentation of this file.
1 <?php
7 
9 
14 {
20  protected $fileUploader;
21 
27  protected $fileHelper;
28 
32  protected $mediaDirectory;
33 
39  protected $parameters = [];
40 
50  public function __construct(
51  \Magento\Framework\App\Helper\Context $context,
52  \Magento\Downloadable\Helper\File $fileHelper,
53  \Magento\CatalogImportExport\Model\Import\UploaderFactory $uploaderFactory,
54  \Magento\Framework\App\ResourceConnection $resource,
55  \Magento\Framework\Filesystem $filesystem
56  ) {
57  parent::__construct($context);
58  $this->fileHelper = $fileHelper;
59  $this->fileUploader = $uploaderFactory->create();
60  $this->fileUploader->init();
61  $this->fileUploader->setAllowedExtensions($this->getAllowedExtensions());
62  $this->fileUploader->removeValidateCallback('catalog_product_image');
63  $this->connection = $resource->getConnection('write');
64  $this->mediaDirectory = $filesystem->getDirectoryWrite(DirectoryList::ROOT);
65  }
66 
75  public function getUploader($type, $parameters)
76  {
77  $dirConfig = DirectoryList::getDefaultConfig();
78  $dirAddon = $dirConfig[DirectoryList::MEDIA][DirectoryList::PATH];
79 
80  if (!empty($parameters[\Magento\ImportExport\Model\Import::FIELD_NAME_IMG_FILE_DIR])) {
82  } else {
83  $tmpPath = $dirAddon . '/' . $this->mediaDirectory->getRelativePath('import');
84  }
85 
86  if (!$this->fileUploader->setTmpDir($tmpPath)) {
87  throw new \Magento\Framework\Exception\LocalizedException(
88  __('File directory \'%1\' is not readable.', $tmpPath)
89  );
90  }
91  $destinationDir = "downloadable/files/" . $type;
92  $destinationPath = $dirAddon . '/' . $this->mediaDirectory->getRelativePath($destinationDir);
93 
94  $this->mediaDirectory->create($destinationPath);
95  if (!$this->fileUploader->setDestDir($destinationPath)) {
96  throw new \Magento\Framework\Exception\LocalizedException(
97  __('File directory \'%1\' is not writable.', $destinationPath)
98  );
99  }
100  return $this->fileUploader;
101  }
102 
108  protected function getAllowedExtensions()
109  {
110  $result = [];
111  foreach (array_keys($this->fileHelper->getAllMineTypes()) as $option) {
112  $result[] = substr($option, 1);
113  }
114  return $result;
115  }
116 }
__()
Definition: __.php:13
$resource
Definition: bulk.php:12
$type
Definition: item.phtml:13
__construct(\Magento\Framework\App\Helper\Context $context, \Magento\Downloadable\Helper\File $fileHelper, \Magento\CatalogImportExport\Model\Import\UploaderFactory $uploaderFactory, \Magento\Framework\App\ResourceConnection $resource, \Magento\Framework\Filesystem $filesystem)
Definition: Uploader.php:50
$filesystem