PageRenderTime 40ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/app/code/Magento/Customer/Controller/Adminhtml/Index/Viewfile.php

https://gitlab.com/crazybutterfly815/magento2
PHP | 201 lines | 137 code | 8 blank | 56 comment | 7 complexity | d5d421e6751c3f36c2c0af0513863d6e MD5 | raw file
  1. <?php
  2. /**
  3. * Copyright © 2016 Magento. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Customer\Controller\Adminhtml\Index;
  7. use Magento\Customer\Api\AccountManagementInterface;
  8. use Magento\Customer\Api\AddressRepositoryInterface;
  9. use Magento\Customer\Api\CustomerMetadataInterface;
  10. use Magento\Customer\Api\CustomerRepositoryInterface;
  11. use Magento\Customer\Api\Data\AddressInterfaceFactory;
  12. use Magento\Customer\Api\Data\CustomerInterfaceFactory;
  13. use Magento\Customer\Model\Address\Mapper;
  14. use Magento\Framework\Exception\NotFoundException;
  15. use Magento\Framework\App\Filesystem\DirectoryList;
  16. use Magento\Framework\DataObjectFactory;
  17. /**
  18. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  19. */
  20. class Viewfile extends \Magento\Customer\Controller\Adminhtml\Index
  21. {
  22. /**
  23. * @var \Magento\Framework\Controller\Result\RawFactory
  24. */
  25. protected $resultRawFactory;
  26. /**
  27. * @var \Magento\Framework\Url\DecoderInterface
  28. */
  29. protected $urlDecoder;
  30. /**
  31. * @param \Magento\Backend\App\Action\Context $context
  32. * @param \Magento\Framework\Registry $coreRegistry
  33. * @param \Magento\Framework\App\Response\Http\FileFactory $fileFactory
  34. * @param \Magento\Customer\Model\CustomerFactory $customerFactory
  35. * @param \Magento\Customer\Model\AddressFactory $addressFactory
  36. * @param \Magento\Customer\Model\Metadata\FormFactory $formFactory
  37. * @param \Magento\Newsletter\Model\SubscriberFactory $subscriberFactory
  38. * @param \Magento\Customer\Helper\View $viewHelper
  39. * @param \Magento\Framework\Math\Random $random
  40. * @param CustomerRepositoryInterface $customerRepository
  41. * @param \Magento\Framework\Api\ExtensibleDataObjectConverter $extensibleDataObjectConverter
  42. * @param Mapper $addressMapper
  43. * @param AccountManagementInterface $customerAccountManagement
  44. * @param AddressRepositoryInterface $addressRepository
  45. * @param CustomerInterfaceFactory $customerDataFactory
  46. * @param AddressInterfaceFactory $addressDataFactory
  47. * @param \Magento\Customer\Model\Customer\Mapper $customerMapper
  48. * @param \Magento\Framework\Reflection\DataObjectProcessor $dataObjectProcessor
  49. * @param \Magento\Framework\Api\DataObjectHelper $dataObjectHelper
  50. * @param DataObjectFactory $objectFactory
  51. * @param \Magento\Framework\View\LayoutFactory $layoutFactory
  52. * @param \Magento\Framework\View\Result\LayoutFactory $resultLayoutFactory
  53. * @param \Magento\Framework\View\Result\PageFactory $resultPageFactory
  54. * @param \Magento\Backend\Model\View\Result\ForwardFactory $resultForwardFactory
  55. * @param \Magento\Framework\Controller\Result\JsonFactory $resultJsonFactory
  56. * @param \Magento\Framework\Controller\Result\RawFactory $resultRawFactory
  57. * @param \Magento\Framework\Url\DecoderInterface $urlDecoder
  58. *
  59. * @SuppressWarnings(PHPMD.ExcessiveParameterList)
  60. */
  61. public function __construct(
  62. \Magento\Backend\App\Action\Context $context,
  63. \Magento\Framework\Registry $coreRegistry,
  64. \Magento\Framework\App\Response\Http\FileFactory $fileFactory,
  65. \Magento\Customer\Model\CustomerFactory $customerFactory,
  66. \Magento\Customer\Model\AddressFactory $addressFactory,
  67. \Magento\Customer\Model\Metadata\FormFactory $formFactory,
  68. \Magento\Newsletter\Model\SubscriberFactory $subscriberFactory,
  69. \Magento\Customer\Helper\View $viewHelper,
  70. \Magento\Framework\Math\Random $random,
  71. CustomerRepositoryInterface $customerRepository,
  72. \Magento\Framework\Api\ExtensibleDataObjectConverter $extensibleDataObjectConverter,
  73. Mapper $addressMapper,
  74. AccountManagementInterface $customerAccountManagement,
  75. AddressRepositoryInterface $addressRepository,
  76. CustomerInterfaceFactory $customerDataFactory,
  77. AddressInterfaceFactory $addressDataFactory,
  78. \Magento\Customer\Model\Customer\Mapper $customerMapper,
  79. \Magento\Framework\Reflection\DataObjectProcessor $dataObjectProcessor,
  80. \Magento\Framework\Api\DataObjectHelper $dataObjectHelper,
  81. DataObjectFactory $objectFactory,
  82. \Magento\Framework\View\LayoutFactory $layoutFactory,
  83. \Magento\Framework\View\Result\LayoutFactory $resultLayoutFactory,
  84. \Magento\Framework\View\Result\PageFactory $resultPageFactory,
  85. \Magento\Backend\Model\View\Result\ForwardFactory $resultForwardFactory,
  86. \Magento\Framework\Controller\Result\JsonFactory $resultJsonFactory,
  87. \Magento\Framework\Controller\Result\RawFactory $resultRawFactory,
  88. \Magento\Framework\Url\DecoderInterface $urlDecoder
  89. ) {
  90. parent::__construct(
  91. $context,
  92. $coreRegistry,
  93. $fileFactory,
  94. $customerFactory,
  95. $addressFactory,
  96. $formFactory,
  97. $subscriberFactory,
  98. $viewHelper,
  99. $random,
  100. $customerRepository,
  101. $extensibleDataObjectConverter,
  102. $addressMapper,
  103. $customerAccountManagement,
  104. $addressRepository,
  105. $customerDataFactory,
  106. $addressDataFactory,
  107. $customerMapper,
  108. $dataObjectProcessor,
  109. $dataObjectHelper,
  110. $objectFactory,
  111. $layoutFactory,
  112. $resultLayoutFactory,
  113. $resultPageFactory,
  114. $resultForwardFactory,
  115. $resultJsonFactory
  116. );
  117. $this->resultRawFactory = $resultRawFactory;
  118. $this->urlDecoder = $urlDecoder;
  119. }
  120. /**
  121. * Customer view file action
  122. *
  123. * @return \Magento\Framework\Controller\ResultInterface|void
  124. * @throws NotFoundException
  125. *
  126. * @SuppressWarnings(PHPMD.ExitExpression)
  127. */
  128. public function execute()
  129. {
  130. $file = null;
  131. $plain = false;
  132. if ($this->getRequest()->getParam('file')) {
  133. // download file
  134. $file = $this->urlDecoder->decode(
  135. $this->getRequest()->getParam('file')
  136. );
  137. } elseif ($this->getRequest()->getParam('image')) {
  138. // show plain image
  139. $file = $this->urlDecoder->decode(
  140. $this->getRequest()->getParam('image')
  141. );
  142. $plain = true;
  143. } else {
  144. throw new NotFoundException(__('Page not found.'));
  145. }
  146. /** @var \Magento\Framework\Filesystem $filesystem */
  147. $filesystem = $this->_objectManager->get(\Magento\Framework\Filesystem::class);
  148. $directory = $filesystem->getDirectoryRead(DirectoryList::MEDIA);
  149. $fileName = CustomerMetadataInterface::ENTITY_TYPE_CUSTOMER . '/' . ltrim($file, '/');
  150. $path = $directory->getAbsolutePath($fileName);
  151. if (!$directory->isFile($fileName)
  152. && !$this->_objectManager->get(\Magento\MediaStorage\Helper\File\Storage::class)->processStorageFile($path)
  153. ) {
  154. throw new NotFoundException(__('Page not found.'));
  155. }
  156. if ($plain) {
  157. $extension = pathinfo($path, PATHINFO_EXTENSION);
  158. switch (strtolower($extension)) {
  159. case 'gif':
  160. $contentType = 'image/gif';
  161. break;
  162. case 'jpg':
  163. $contentType = 'image/jpeg';
  164. break;
  165. case 'png':
  166. $contentType = 'image/png';
  167. break;
  168. default:
  169. $contentType = 'application/octet-stream';
  170. break;
  171. }
  172. $stat = $directory->stat($fileName);
  173. $contentLength = $stat['size'];
  174. $contentModify = $stat['mtime'];
  175. /** @var \Magento\Framework\Controller\Result\Raw $resultRaw */
  176. $resultRaw = $this->resultRawFactory->create();
  177. $resultRaw->setHttpResponseCode(200)
  178. ->setHeader('Pragma', 'public', true)
  179. ->setHeader('Content-type', $contentType, true)
  180. ->setHeader('Content-Length', $contentLength)
  181. ->setHeader('Last-Modified', date('r', $contentModify));
  182. $resultRaw->setContents($directory->readFile($fileName));
  183. return $resultRaw;
  184. } else {
  185. $name = pathinfo($path, PATHINFO_BASENAME);
  186. $this->_fileFactory->create(
  187. $name,
  188. ['type' => 'filename', 'value' => $fileName],
  189. DirectoryList::MEDIA
  190. );
  191. }
  192. }
  193. }