|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © 2015 Magento. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | +namespace Magento\ImportExport\Controller\Adminhtml\Import; |
| 7 | + |
| 8 | +use Magento\ImportExport\Controller\Adminhtml\Import as ImportController; |
| 9 | +use Magento\Framework\App\Filesystem\DirectoryList; |
| 10 | + |
| 11 | +/** |
| 12 | + * Download sample file controller |
| 13 | + */ |
| 14 | +class Download extends ImportController |
| 15 | +{ |
| 16 | + const SAMPLE_FILES_DIRECTORY = 'Magento/ImportExport/Files/Sample/'; |
| 17 | + |
| 18 | + /** |
| 19 | + * @var \Magento\Framework\Controller\Result\RawFactory |
| 20 | + */ |
| 21 | + protected $resultRawFactory; |
| 22 | + |
| 23 | + /** |
| 24 | + * @var \Magento\Framework\Filesystem\Directory\WriteInterface |
| 25 | + */ |
| 26 | + protected $fileDirectory; |
| 27 | + |
| 28 | + /** |
| 29 | + * @param \Magento\Backend\App\Action\Context $context |
| 30 | + * @param \Magento\Framework\App\Response\Http\FileFactory $fileFactory |
| 31 | + * @param \Magento\Framework\Controller\Result\RawFactory $resultRawFactory |
| 32 | + * @param \Magento\Framework\Filesystem $filesystem |
| 33 | + */ |
| 34 | + public function __construct( |
| 35 | + \Magento\Backend\App\Action\Context $context, |
| 36 | + \Magento\Framework\App\Response\Http\FileFactory $fileFactory, |
| 37 | + \Magento\Framework\Controller\Result\RawFactory $resultRawFactory, |
| 38 | + \Magento\Framework\Filesystem $filesystem |
| 39 | + ) { |
| 40 | + parent::__construct( |
| 41 | + $context |
| 42 | + ); |
| 43 | + $this->fileFactory = $fileFactory; |
| 44 | + $this->resultRawFactory = $resultRawFactory; |
| 45 | + $this->fileDirectory = $filesystem->getDirectoryWrite(DirectoryList::MODULES); |
| 46 | + } |
| 47 | + |
| 48 | + /** |
| 49 | + * Download sample file action |
| 50 | + * |
| 51 | + * @return \Magento\Framework\Controller\Result\Raw |
| 52 | + */ |
| 53 | + public function execute() |
| 54 | + { |
| 55 | + $fileName = $this->getRequest()->getParam('filename') . '.csv'; |
| 56 | + $filePath = self::SAMPLE_FILES_DIRECTORY . $fileName; |
| 57 | + |
| 58 | + if (!$this->fileDirectory->isFile($filePath)) { |
| 59 | + /** @var \Magento\Backend\Model\View\Result\Redirect $resultRedirect */ |
| 60 | + $this->messageManager->addError(__('There is no sample file for this entity.')); |
| 61 | + $resultRedirect = $this->resultRedirectFactory->create(); |
| 62 | + $resultRedirect->setPath('*/import'); |
| 63 | + return $resultRedirect; |
| 64 | + } |
| 65 | + |
| 66 | + $fileSize = isset($this->fileDirectory->stat($filePath)['size']) |
| 67 | + ? $this->fileDirectory->stat($filePath)['size'] : null; |
| 68 | + |
| 69 | + $this->fileFactory->create( |
| 70 | + $fileName, |
| 71 | + null, |
| 72 | + DirectoryList::VAR_DIR, |
| 73 | + 'application/octet-stream', |
| 74 | + $fileSize |
| 75 | + ); |
| 76 | + |
| 77 | + /** @var \Magento\Framework\Controller\Result\Raw $resultRaw */ |
| 78 | + $resultRaw = $this->resultRawFactory->create(); |
| 79 | + $resultRaw->setContents($this->fileDirectory->readFile($filePath)); |
| 80 | + return $resultRaw; |
| 81 | + } |
| 82 | +} |
0 commit comments