Skip to content

Commit

Permalink
Merge pull request #43 from lizardmedia/fix-for-not-removed-download-…
Browse files Browse the repository at this point in the history
…file

Fix for not remove download file
  • Loading branch information
Bartosz Kubicki committed Nov 26, 2020
2 parents fc04916 + 141e15a commit 5c5f1e4
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 48 deletions.
22 changes: 8 additions & 14 deletions Controller/Adminhtml/Attachment/File/Preview.php
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}

/**
Expand All @@ -85,12 +87,4 @@ private function loadAttachmentById(int $id) : ?AttachmentInterface
return null;
}
}

/**
* @return Redirect
*/
private function goBack(): Redirect
{
return $this->resultRedirectFactory->create()->setRefererUrl();
}
}
22 changes: 7 additions & 15 deletions Controller/Download/Attachment.php
Expand Up @@ -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;

Expand Down Expand Up @@ -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;
}

/**
Expand All @@ -86,12 +86,4 @@ private function loadAttachmentById(int $id): ?AttachmentInterface
return null;
}
}

/**
* @return Redirect
*/
private function goBack(): Redirect
{
return $this->resultRedirectFactory->create()->setRefererUrl();
}
}
32 changes: 13 additions & 19 deletions Controller/DownloadProcessor.php
Expand Up @@ -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;

Expand All @@ -35,11 +34,6 @@ class DownloadProcessor
*/
private $fileFactory;

/**
* @var RawFactory
*/
private $rawFactory;

/**
* @var File
*/
Expand All @@ -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'));
}
Expand All @@ -98,4 +92,4 @@ private function readFile(AttachmentInterface $attachment): string

return $fileContent;
}
}
}

0 comments on commit 5c5f1e4

Please sign in to comment.