Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
FileProcessor.php
Go to the documentation of this file.
1 <?php
6 namespace Magento\Customer\Model;
7 
9 {
13  const TMP_DIR = 'tmp';
14 
18  private $mediaDirectory;
19 
23  private $uploaderFactory;
24 
28  private $urlBuilder;
29 
33  private $urlEncoder;
34 
38  private $entityTypeCode;
39 
43  private $allowedExtensions = [];
44 
48  private $mime;
49 
59  public function __construct(
60  \Magento\Framework\Filesystem $filesystem,
61  \Magento\MediaStorage\Model\File\UploaderFactory $uploaderFactory,
62  \Magento\Framework\UrlInterface $urlBuilder,
63  \Magento\Framework\Url\EncoderInterface $urlEncoder,
64  $entityTypeCode,
65  \Magento\Framework\File\Mime $mime,
66  array $allowedExtensions = []
67  ) {
68  $this->mediaDirectory = $filesystem->getDirectoryWrite(\Magento\Framework\App\Filesystem\DirectoryList::MEDIA);
69  $this->uploaderFactory = $uploaderFactory;
70  $this->urlBuilder = $urlBuilder;
71  $this->urlEncoder = $urlEncoder;
72  $this->entityTypeCode = $entityTypeCode;
73  $this->mime = $mime;
74  $this->allowedExtensions = $allowedExtensions;
75  }
76 
84  {
85  $filePath = $this->entityTypeCode . '/' . ltrim($fileName, '/');
86 
87  $fileContent = $this->mediaDirectory->readFile($filePath);
88 
89  $encodedContent = base64_encode($fileContent);
90  return $encodedContent;
91  }
92 
99  public function getStat($fileName)
100  {
101  $filePath = $this->entityTypeCode . '/' . ltrim($fileName, '/');
102 
103  $result = $this->mediaDirectory->stat($filePath);
104  return $result;
105  }
106 
113  public function getMimeType($fileName)
114  {
115  $filePath = $this->entityTypeCode . '/' . ltrim($fileName, '/');
116  $absoluteFilePath = $this->mediaDirectory->getAbsolutePath($filePath);
117 
118  $result = $this->mime->getMimeType($absoluteFilePath);
119  return $result;
120  }
121 
128  public function isExist($fileName)
129  {
130  $filePath = $this->entityTypeCode . '/' . ltrim($fileName, '/');
131 
132  $result = $this->mediaDirectory->isExist($filePath);
133  return $result;
134  }
135 
143  public function getViewUrl($filePath, $type)
144  {
145  $viewUrl = '';
146 
147  if ($this->entityTypeCode == \Magento\Customer\Api\AddressMetadataInterface::ENTITY_TYPE_ADDRESS) {
148  $filePath = $this->entityTypeCode . '/' . ltrim($filePath, '/');
149  $viewUrl = $this->urlBuilder->getBaseUrl(['_type' => \Magento\Framework\UrlInterface::URL_TYPE_MEDIA])
150  . $this->mediaDirectory->getRelativePath($filePath);
151  }
152 
153  if ($this->entityTypeCode == \Magento\Customer\Api\CustomerMetadataInterface::ENTITY_TYPE_CUSTOMER) {
154  $viewUrl = $this->urlBuilder->getUrl(
155  'customer/index/viewfile',
156  [$type => $this->urlEncoder->encode(ltrim($filePath, '/'))]
157  );
158  }
159 
160  return $viewUrl;
161  }
162 
170  public function saveTemporaryFile($fileId)
171  {
173  $uploader = $this->uploaderFactory->create(['fileId' => $fileId]);
174  $uploader->setFilesDispersion(false);
175  $uploader->setFilenamesCaseSensitivity(false);
176  $uploader->setAllowRenameFiles(true);
177  $uploader->setAllowedExtensions($this->allowedExtensions);
178 
179  $path = $this->mediaDirectory->getAbsolutePath(
180  $this->entityTypeCode . '/' . self::TMP_DIR
181  );
182 
183  $result = $uploader->save($path);
184  unset($result['path']);
185  if (!$result) {
186  throw new \Magento\Framework\Exception\LocalizedException(
187  __('File can not be saved to the destination folder.')
188  );
189  }
190 
191  return $result;
192  }
193 
201  public function moveTemporaryFile($fileName)
202  {
203  $fileName = ltrim($fileName, '/');
204 
206  $destinationPath = $this->entityTypeCode . $dispersionPath;
207 
208  if (!$this->mediaDirectory->create($destinationPath)) {
209  throw new \Magento\Framework\Exception\LocalizedException(
210  __('Unable to create directory %1.', $destinationPath)
211  );
212  }
213 
214  if (!$this->mediaDirectory->isWritable($destinationPath)) {
215  throw new \Magento\Framework\Exception\LocalizedException(
216  __('Destination folder is not writable or does not exists.')
217  );
218  }
219 
221  $this->mediaDirectory->getAbsolutePath($destinationPath) . '/' . $fileName
222  );
223 
224  try {
225  $this->mediaDirectory->renameFile(
226  $this->entityTypeCode . '/' . self::TMP_DIR . '/' . $fileName,
227  $destinationPath . '/' . $destinationFileName
228  );
229  } catch (\Exception $e) {
230  throw new \Magento\Framework\Exception\LocalizedException(
231  __('Something went wrong while saving the file.')
232  );
233  }
234 
235  $fileName = $dispersionPath . '/' . $fileName;
236  return $fileName;
237  }
238 
245  public function removeUploadedFile($fileName)
246  {
247  $filePath = $this->entityTypeCode . '/' . ltrim($fileName, '/');
248 
249  $result = $this->mediaDirectory->delete($filePath);
250  return $result;
251  }
252 }
__()
Definition: __.php:13
$type
Definition: item.phtml:13
$fileName
Definition: translate.phtml:15
__construct(\Magento\Framework\Filesystem $filesystem, \Magento\MediaStorage\Model\File\UploaderFactory $uploaderFactory, \Magento\Framework\UrlInterface $urlBuilder, \Magento\Framework\Url\EncoderInterface $urlEncoder, $entityTypeCode, \Magento\Framework\File\Mime $mime, array $allowedExtensions=[])
$filesystem
static getNewFileName($destinationFile)
Definition: Uploader.php:605
static getDispersionPath($fileName)
Definition: Uploader.php:642