1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* File: DownloadProcessor.php |
7
|
|
|
* |
8
|
|
|
* @author Bartosz Kubicki [email protected]> |
9
|
|
|
* @copyright Copyright (C) 2020 Lizard Media (http://lizardmedia.pl) |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace LizardMedia\ProductAttachment\Controller; |
13
|
|
|
|
14
|
|
|
use Exception; |
15
|
|
|
use LizardMedia\ProductAttachment\Api\Data\AttachmentInterface; |
16
|
|
|
use Magento\Framework\App\Response\Http\FileFactory; |
17
|
|
|
use Magento\Framework\Controller\Result\Raw; |
18
|
|
|
use Magento\Framework\Controller\Result\RawFactory; |
19
|
|
|
use Magento\Framework\Exception\FileSystemException; |
20
|
|
|
use Magento\Framework\Filesystem\Io\File; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Class DownloadProcessor |
24
|
|
|
* @package LizardMedia\ProductAttachment\Controller |
25
|
|
|
*/ |
26
|
|
|
class DownloadProcessor |
27
|
|
|
{ |
28
|
|
|
/** |
29
|
|
|
* @var DownloadResourceResolver |
30
|
|
|
*/ |
31
|
|
|
private $downloadResourceResolver; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var FileFactory |
35
|
|
|
*/ |
36
|
|
|
private $fileFactory; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var RawFactory |
40
|
|
|
*/ |
41
|
|
|
private $rawFactory; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @var File |
45
|
|
|
*/ |
46
|
|
|
private $file; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* DownloadProcessor constructor. |
50
|
|
|
* @param DownloadResourceResolver $downloadResourceResolver |
51
|
|
|
* @param FileFactory $fileFactory |
52
|
|
|
* @param RawFactory $rawFactory |
53
|
|
|
* @param File $file |
54
|
|
|
*/ |
55
|
|
|
public function __construct( |
56
|
|
|
DownloadResourceResolver $downloadResourceResolver, |
57
|
|
|
FileFactory $fileFactory, |
58
|
|
|
RawFactory $rawFactory, |
59
|
|
|
File $file |
60
|
|
|
) { |
61
|
|
|
$this->downloadResourceResolver = $downloadResourceResolver; |
62
|
|
|
$this->fileFactory = $fileFactory; |
63
|
|
|
$this->rawFactory = $rawFactory; |
64
|
|
|
$this->file = $file; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @param AttachmentInterface $attachment |
69
|
|
|
* @return Raw |
70
|
|
|
* @throws FileSystemException |
71
|
|
|
*/ |
72
|
|
|
public function processDownload(AttachmentInterface $attachment): Raw |
73
|
|
|
{ |
74
|
|
|
/** @var $raw Raw */ |
75
|
|
|
try { |
76
|
|
|
$raw = $this->rawFactory->create(); |
77
|
|
|
$response = $this->fileFactory->create( |
78
|
|
|
basename($this->downloadResourceResolver->resolveResource($attachment)), |
79
|
|
|
$this->readFile($attachment) |
80
|
|
|
); |
81
|
|
|
return $raw->renderResult($response); |
82
|
|
|
} catch (Exception $exception) { |
83
|
|
|
throw new FileSystemException(__('File could not be downloaded')); |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @param AttachmentInterface $attachment |
89
|
|
|
* @return string |
90
|
|
|
* @throws FileSystemException |
91
|
|
|
*/ |
92
|
|
|
private function readFile(AttachmentInterface $attachment): string |
93
|
|
|
{ |
94
|
|
|
$fileContent = $this->file->read($this->downloadResourceResolver->resolveResource($attachment)); |
95
|
|
|
if ($fileContent === false ) { |
96
|
|
|
throw new FileSystemException(__('File could not be read')); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
return $fileContent; |
100
|
|
|
} |
101
|
|
|
} |