From 141e15a4730df30a7e4aa04df6e2bfd4660a1c0b Mon Sep 17 00:00:00 2001 From: Bartosz Kubicki Date: Fri, 13 Nov 2020 13:24:57 +0100 Subject: [PATCH] Fix for not remove download file --- .../Adminhtml/Attachment/File/Preview.php | 22 +++++-------- Controller/Download/Attachment.php | 22 ++++--------- Controller/DownloadProcessor.php | 32 ++++++++----------- 3 files changed, 28 insertions(+), 48 deletions(-) diff --git a/Controller/Adminhtml/Attachment/File/Preview.php b/Controller/Adminhtml/Attachment/File/Preview.php index 0028713..bef3769 100644 --- a/Controller/Adminhtml/Attachment/File/Preview.php +++ b/Controller/Adminhtml/Attachment/File/Preview.php @@ -17,6 +17,7 @@ use LizardMedia\ProductAttachment\Model\Attachment; use Magento\Backend\App\Action; use Magento\Backend\App\Action\Context; +use Magento\Framework\App\ResponseInterface; use Magento\Framework\Controller\Result\Redirect; use Magento\Framework\Controller\ResultInterface; use Magento\Framework\Exception\FileSystemException; @@ -54,22 +55,23 @@ public function __construct( } /** - * @return ResultInterface + * @return ResponseInterface */ - public function execute(): ResultInterface + public function execute(): ResponseInterface { $attachmentId = $this->getRequest()->getParam(Attachment::ID, 0); $attachment = $this->loadAttachmentById((int) $attachmentId); if ($attachment instanceof AttachmentInterface) { try { - return $this->downloadProcessor->processDownload($attachment); + $this->downloadProcessor->processDownload($attachment); + return $this->_response; } catch (FileSystemException $exception) { $this->messageManager->addErrorMessage(__('Sorry, there was an error getting requested content.')); - return $this->goBack(); } - } else { - return $this->goBack(); } + + $this->_redirect->redirect($this->_response, $this->_redirect->getRefererUrl()); + return $this->_response; } /** @@ -85,12 +87,4 @@ private function loadAttachmentById(int $id) : ?AttachmentInterface return null; } } - - /** - * @return Redirect - */ - private function goBack(): Redirect - { - return $this->resultRedirectFactory->create()->setRefererUrl(); - } } diff --git a/Controller/Download/Attachment.php b/Controller/Download/Attachment.php index 241325c..594dabf 100644 --- a/Controller/Download/Attachment.php +++ b/Controller/Download/Attachment.php @@ -16,9 +16,7 @@ use LizardMedia\ProductAttachment\Controller\DownloadProcessor; use Magento\Framework\App\Action\Action; use Magento\Framework\App\Action\Context; -use Magento\Framework\Controller\Result\RawFactory; -use Magento\Framework\Controller\Result\Redirect; -use Magento\Framework\Controller\ResultInterface; +use Magento\Framework\App\ResponseInterface; use Magento\Framework\Exception\FileSystemException; use Magento\Framework\Exception\NoSuchEntityException; @@ -55,22 +53,24 @@ public function __construct( } /** - * @return ResultInterface + * @return ResponseInterface */ - public function execute(): ResultInterface + public function execute(): ResponseInterface { $attachmentId = (int) $this->getRequest()->getParam('id', 0); $attachment = $this->loadAttachmentById($attachmentId); if ($attachment instanceof AttachmentInterface) { try { - return $this->downloadProcessor->processDownload($attachment); + $this->downloadProcessor->processDownload($attachment); + return $this->_response; } catch (FileSystemException $exception) { $this->messageManager->addErrorMessage(__('Sorry, there was an error getting requested content.')); } } - return $this->goBack(); + $this->_redirect->redirect($this->_response, $this->_redirect->getRefererUrl()); + return $this->_response; } /** @@ -86,12 +86,4 @@ private function loadAttachmentById(int $id): ?AttachmentInterface return null; } } - - /** - * @return Redirect - */ - private function goBack(): Redirect - { - return $this->resultRedirectFactory->create()->setRefererUrl(); - } } diff --git a/Controller/DownloadProcessor.php b/Controller/DownloadProcessor.php index 933850c..8fe752b 100644 --- a/Controller/DownloadProcessor.php +++ b/Controller/DownloadProcessor.php @@ -13,9 +13,8 @@ use Exception; use LizardMedia\ProductAttachment\Api\Data\AttachmentInterface; +use Magento\Framework\App\Filesystem\DirectoryList; use Magento\Framework\App\Response\Http\FileFactory; -use Magento\Framework\Controller\Result\Raw; -use Magento\Framework\Controller\Result\RawFactory; use Magento\Framework\Exception\FileSystemException; use Magento\Framework\Filesystem\Io\File; @@ -35,11 +34,6 @@ class DownloadProcessor */ private $fileFactory; - /** - * @var RawFactory - */ - private $rawFactory; - /** * @var File */ @@ -49,36 +43,36 @@ class DownloadProcessor * DownloadProcessor constructor. * @param DownloadResourceResolver $downloadResourceResolver * @param FileFactory $fileFactory - * @param RawFactory $rawFactory * @param File $file */ public function __construct( DownloadResourceResolver $downloadResourceResolver, FileFactory $fileFactory, - RawFactory $rawFactory, File $file ) { $this->downloadResourceResolver = $downloadResourceResolver; $this->fileFactory = $fileFactory; - $this->rawFactory = $rawFactory; $this->file = $file; } /** * @param AttachmentInterface $attachment - * @return Raw + * @return void * @throws FileSystemException */ - public function processDownload(AttachmentInterface $attachment): Raw + public function processDownload(AttachmentInterface $attachment): void { - /** @var $raw Raw */ try { - $raw = $this->rawFactory->create(); - $response = $this->fileFactory->create( - basename($this->downloadResourceResolver->resolveResource($attachment)), - $this->readFile($attachment) + $name = basename($this->downloadResourceResolver->resolveResource($attachment)); + $this->fileFactory->create( + $name, + [ + 'type' => 'string', + 'value' => $this->readFile($attachment), + 'rm' => true + ], + DirectoryList::TMP ); - return $raw->renderResult($response); } catch (Exception $exception) { throw new FileSystemException(__('File could not be downloaded')); } @@ -98,4 +92,4 @@ private function readFile(AttachmentInterface $attachment): string return $fileContent; } -} \ No newline at end of file +}