FileProcessor.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Customer\Model;
  7. class FileProcessor
  8. {
  9. /**
  10. * Temporary directory name
  11. */
  12. const TMP_DIR = 'tmp';
  13. /**
  14. * @var \Magento\Framework\Filesystem\Directory\WriteInterface
  15. */
  16. private $mediaDirectory;
  17. /**
  18. * @var \Magento\MediaStorage\Model\File\UploaderFactory
  19. */
  20. private $uploaderFactory;
  21. /**
  22. * @var \Magento\Framework\UrlInterface
  23. */
  24. private $urlBuilder;
  25. /**
  26. * @var \Magento\Framework\Url\EncoderInterface
  27. */
  28. private $urlEncoder;
  29. /**
  30. * @var string
  31. */
  32. private $entityTypeCode;
  33. /**
  34. * @var array
  35. */
  36. private $allowedExtensions = [];
  37. /**
  38. * @var \Magento\Framework\File\Mime
  39. */
  40. private $mime;
  41. /**
  42. * @param \Magento\Framework\Filesystem $filesystem
  43. * @param \Magento\MediaStorage\Model\File\UploaderFactory $uploaderFactory
  44. * @param \Magento\Framework\UrlInterface $urlBuilder
  45. * @param \Magento\Framework\Url\EncoderInterface $urlEncoder
  46. * @param string $entityTypeCode
  47. * @param \Magento\Framework\File\Mime $mime
  48. * @param array $allowedExtensions
  49. */
  50. public function __construct(
  51. \Magento\Framework\Filesystem $filesystem,
  52. \Magento\MediaStorage\Model\File\UploaderFactory $uploaderFactory,
  53. \Magento\Framework\UrlInterface $urlBuilder,
  54. \Magento\Framework\Url\EncoderInterface $urlEncoder,
  55. $entityTypeCode,
  56. \Magento\Framework\File\Mime $mime,
  57. array $allowedExtensions = []
  58. ) {
  59. $this->mediaDirectory = $filesystem->getDirectoryWrite(\Magento\Framework\App\Filesystem\DirectoryList::MEDIA);
  60. $this->uploaderFactory = $uploaderFactory;
  61. $this->urlBuilder = $urlBuilder;
  62. $this->urlEncoder = $urlEncoder;
  63. $this->entityTypeCode = $entityTypeCode;
  64. $this->mime = $mime;
  65. $this->allowedExtensions = $allowedExtensions;
  66. }
  67. /**
  68. * Retrieve base64 encoded file content
  69. *
  70. * @param string $fileName
  71. * @return string
  72. */
  73. public function getBase64EncodedData($fileName)
  74. {
  75. $filePath = $this->entityTypeCode . '/' . ltrim($fileName, '/');
  76. $fileContent = $this->mediaDirectory->readFile($filePath);
  77. $encodedContent = base64_encode($fileContent);
  78. return $encodedContent;
  79. }
  80. /**
  81. * Get file statistics data
  82. *
  83. * @param string $fileName
  84. * @return array
  85. */
  86. public function getStat($fileName)
  87. {
  88. $filePath = $this->entityTypeCode . '/' . ltrim($fileName, '/');
  89. $result = $this->mediaDirectory->stat($filePath);
  90. return $result;
  91. }
  92. /**
  93. * Retrieve MIME type of requested file
  94. *
  95. * @param string $fileName
  96. * @return string
  97. */
  98. public function getMimeType($fileName)
  99. {
  100. $filePath = $this->entityTypeCode . '/' . ltrim($fileName, '/');
  101. $absoluteFilePath = $this->mediaDirectory->getAbsolutePath($filePath);
  102. $result = $this->mime->getMimeType($absoluteFilePath);
  103. return $result;
  104. }
  105. /**
  106. * Check if the file exists
  107. *
  108. * @param string $fileName
  109. * @return bool
  110. */
  111. public function isExist($fileName)
  112. {
  113. $filePath = $this->entityTypeCode . '/' . ltrim($fileName, '/');
  114. $result = $this->mediaDirectory->isExist($filePath);
  115. return $result;
  116. }
  117. /**
  118. * Retrieve customer/index/viewfile action URL
  119. *
  120. * @param string $filePath
  121. * @param string $type
  122. * @return string
  123. */
  124. public function getViewUrl($filePath, $type)
  125. {
  126. $viewUrl = '';
  127. if ($this->entityTypeCode == \Magento\Customer\Api\AddressMetadataInterface::ENTITY_TYPE_ADDRESS) {
  128. $filePath = $this->entityTypeCode . '/' . ltrim($filePath, '/');
  129. $viewUrl = $this->urlBuilder->getBaseUrl(['_type' => \Magento\Framework\UrlInterface::URL_TYPE_MEDIA])
  130. . $this->mediaDirectory->getRelativePath($filePath);
  131. }
  132. if ($this->entityTypeCode == \Magento\Customer\Api\CustomerMetadataInterface::ENTITY_TYPE_CUSTOMER) {
  133. $viewUrl = $this->urlBuilder->getUrl(
  134. 'customer/index/viewfile',
  135. [$type => $this->urlEncoder->encode(ltrim($filePath, '/'))]
  136. );
  137. }
  138. return $viewUrl;
  139. }
  140. /**
  141. * Save uploaded file to temporary directory
  142. *
  143. * @param string $fileId
  144. * @return \string[]
  145. * @throws \Magento\Framework\Exception\LocalizedException
  146. */
  147. public function saveTemporaryFile($fileId)
  148. {
  149. /** @var \Magento\MediaStorage\Model\File\Uploader $uploader */
  150. $uploader = $this->uploaderFactory->create(['fileId' => $fileId]);
  151. $uploader->setFilesDispersion(false);
  152. $uploader->setFilenamesCaseSensitivity(false);
  153. $uploader->setAllowRenameFiles(true);
  154. $uploader->setAllowedExtensions($this->allowedExtensions);
  155. $path = $this->mediaDirectory->getAbsolutePath(
  156. $this->entityTypeCode . '/' . self::TMP_DIR
  157. );
  158. $result = $uploader->save($path);
  159. unset($result['path']);
  160. if (!$result) {
  161. throw new \Magento\Framework\Exception\LocalizedException(
  162. __('File can not be saved to the destination folder.')
  163. );
  164. }
  165. return $result;
  166. }
  167. /**
  168. * Move file from temporary directory into base directory
  169. *
  170. * @param string $fileName
  171. * @return string
  172. * @throws \Magento\Framework\Exception\LocalizedException
  173. */
  174. public function moveTemporaryFile($fileName)
  175. {
  176. $fileName = ltrim($fileName, '/');
  177. $dispersionPath = \Magento\MediaStorage\Model\File\Uploader::getDispersionPath($fileName);
  178. $destinationPath = $this->entityTypeCode . $dispersionPath;
  179. if (!$this->mediaDirectory->create($destinationPath)) {
  180. throw new \Magento\Framework\Exception\LocalizedException(
  181. __('Unable to create directory %1.', $destinationPath)
  182. );
  183. }
  184. if (!$this->mediaDirectory->isWritable($destinationPath)) {
  185. throw new \Magento\Framework\Exception\LocalizedException(
  186. __('Destination folder is not writable or does not exists.')
  187. );
  188. }
  189. $destinationFileName = \Magento\MediaStorage\Model\File\Uploader::getNewFileName(
  190. $this->mediaDirectory->getAbsolutePath($destinationPath) . '/' . $fileName
  191. );
  192. try {
  193. $this->mediaDirectory->renameFile(
  194. $this->entityTypeCode . '/' . self::TMP_DIR . '/' . $fileName,
  195. $destinationPath . '/' . $destinationFileName
  196. );
  197. } catch (\Exception $e) {
  198. throw new \Magento\Framework\Exception\LocalizedException(
  199. __('Something went wrong while saving the file.')
  200. );
  201. }
  202. $fileName = $dispersionPath . '/' . $fileName;
  203. return $fileName;
  204. }
  205. /**
  206. * Remove uploaded file
  207. *
  208. * @param string $fileName
  209. * @return bool
  210. */
  211. public function removeUploadedFile($fileName)
  212. {
  213. $filePath = $this->entityTypeCode . '/' . ltrim($fileName, '/');
  214. $result = $this->mediaDirectory->delete($filePath);
  215. return $result;
  216. }
  217. }