PageRenderTime 41ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 1ms

/vendor/magento/module-customer/Model/FileProcessor.php

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