|
9 | 9 |
|
10 | 10 | use Magento\Backend\App\Action;
|
11 | 11 | use Magento\Framework\App\Action\HttpPostActionInterface;
|
12 |
| -use Magento\Framework\Controller\ResultFactory; |
13 | 12 | use Magento\Framework\Exception\FileSystemException;
|
14 |
| -use Magento\Framework\Exception\LocalizedException; |
15 | 13 | use Magento\Framework\App\Filesystem\DirectoryList;
|
16 | 14 | use Magento\ImportExport\Controller\Adminhtml\Export as ExportController;
|
17 | 15 | use Magento\Framework\Filesystem;
|
@@ -56,29 +54,33 @@ public function __construct(
|
56 | 54 | /**
|
57 | 55 | * Controller basic method implementation.
|
58 | 56 | *
|
59 |
| - * @return \Magento\Framework\App\ResponseInterface|\Magento\Framework\Controller\ResultInterface |
60 |
| - * @throws LocalizedException |
| 57 | + * @return \Magento\Framework\Controller\ResultInterface |
61 | 58 | */
|
62 | 59 | public function execute()
|
63 | 60 | {
|
| 61 | + /** @var \Magento\Framework\Controller\Result\Redirect $resultRedirect */ |
| 62 | + $resultRedirect = $this->resultRedirectFactory->create(); |
| 63 | + $resultRedirect->setPath('adminhtml/export/index'); |
| 64 | + $fileName = $this->getRequest()->getParam('filename'); |
| 65 | + if (empty($fileName)) { |
| 66 | + $this->messageManager->addErrorMessage(\__('Please provide valid export file name')); |
| 67 | + |
| 68 | + return $resultRedirect; |
| 69 | + } |
64 | 70 | try {
|
65 |
| - if (empty($fileName = $this->getRequest()->getParam('filename'))) { |
66 |
| - throw new LocalizedException(__('Please provide export file name')); |
67 |
| - } |
68 | 71 | $directory = $this->filesystem->getDirectoryRead(DirectoryList::VAR_DIR);
|
69 | 72 | $path = $directory->getAbsolutePath() . 'export/' . $fileName;
|
70 | 73 |
|
71 |
| - if (!$directory->isFile($path)) { |
72 |
| - throw new LocalizedException(__('Sorry, but the data is invalid or the file is not uploaded.')); |
| 74 | + if ($directory->isFile($path)) { |
| 75 | + $this->file->deleteFile($path); |
| 76 | + $this->messageManager->addSuccessMessage(\__('File %1 deleted', $fileName)); |
| 77 | + } else { |
| 78 | + $this->messageManager->addErrorMessage(\__('%1 is not a valid file', $fileName)); |
73 | 79 | }
|
74 |
| - |
75 |
| - $this->file->deleteFile($path); |
76 |
| - /** @var \Magento\Backend\Model\View\Result\Redirect $resultRedirect */ |
77 |
| - $resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT); |
78 |
| - $resultRedirect->setPath('adminhtml/export/index'); |
79 |
| - return $resultRedirect; |
80 | 80 | } catch (FileSystemException $exception) {
|
81 |
| - throw new LocalizedException(__('There are no export file with such name %1', $fileName)); |
| 81 | + $this->messageManager->addErrorMessage($exception->getMessage()); |
82 | 82 | }
|
| 83 | + |
| 84 | + return $resultRedirect; |
83 | 85 | }
|
84 | 86 | }
|
0 commit comments