12
12
use Magento \Framework \App \Http \Context ;
13
13
use Magento \Framework \App \PageCache \NotCacheableInterface ;
14
14
use Magento \Framework \App \Request \Http as HttpRequest ;
15
- use Magento \Framework \Exception \FileSystemException ;
16
15
use Magento \Framework \Filesystem ;
17
16
use Magento \Framework \Filesystem \Driver \File \Mime ;
18
17
use Magento \Framework \Session \Config \ConfigInterface ;
@@ -43,74 +42,72 @@ class File extends Http implements NotCacheableInterface
43
42
/**
44
43
* @var array
45
44
*/
46
- private array $ fileOptions = [
45
+ private array $ options = [
47
46
'directoryCode ' => DirectoryList::ROOT ,
48
47
'filePath ' => null ,
49
48
// File name to send to the client
50
49
'fileName ' => null ,
51
50
'contentType ' => null ,
52
51
'contentLength ' => null ,
53
- // Whether to remove after file is sent to the client
52
+ // Whether to remove the file after it is sent to the client
54
53
'remove ' => false ,
55
54
];
56
55
57
56
/**
58
57
* @param HttpRequest $request
59
- * @param Http $response
60
58
* @param CookieManagerInterface $cookieManager
61
59
* @param CookieMetadataFactory $cookieMetadataFactory
62
60
* @param Context $context
63
61
* @param DateTime $dateTime
64
62
* @param ConfigInterface $sessionConfig
63
+ * @param Http $response
65
64
* @param Filesystem $filesystem
66
65
* @param Mime $mime
67
- * @param array $fileOptions
66
+ * @param array $options
68
67
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
69
- * @throws FileSystemException
70
68
*/
71
69
public function __construct (
72
70
HttpRequest $ request ,
73
- Http $ response ,
74
71
CookieManagerInterface $ cookieManager ,
75
72
CookieMetadataFactory $ cookieMetadataFactory ,
76
73
Context $ context ,
77
74
DateTime $ dateTime ,
78
75
ConfigInterface $ sessionConfig ,
76
+ Http $ response ,
79
77
Filesystem $ filesystem ,
80
78
Mime $ mime ,
81
- array $ fileOptions = []
79
+ array $ options = []
82
80
) {
83
81
parent ::__construct ($ request , $ cookieManager , $ cookieMetadataFactory , $ context , $ dateTime , $ sessionConfig );
84
- $ this ->filesystem = $ filesystem ;
85
82
$ this ->response = $ response ;
83
+ $ this ->filesystem = $ filesystem ;
86
84
$ this ->mime = $ mime ;
87
- $ this ->fileOptions = array_merge ($ this ->fileOptions , $ fileOptions );
88
- if (!isset ($ this ->fileOptions ['filePath ' ])) {
89
- throw new InvalidArgumentException ("File path is required " );
90
- }
91
- $ dir = $ this ->filesystem ->getDirectoryRead ($ this ->fileOptions ['directoryCode ' ]);
92
- if (!$ dir ->isExist ($ this ->fileOptions ['filePath ' ])) {
93
- throw new InvalidArgumentException ("File ' {$ this ->fileOptions ['filePath ' ]}' does not exists. " );
94
- }
85
+ $ this ->options = array_merge ($ this ->options , $ options );
95
86
}
96
87
97
88
/**
98
89
* @inheritDoc
99
90
*/
100
91
public function sendResponse ()
101
92
{
102
- $ dir = $ this ->filesystem ->getDirectoryWrite ($ this ->fileOptions ['directoryCode ' ]);
103
- $ filePath = $ this ->fileOptions ['filePath ' ];
104
- $ contentType = $ this ->fileOptions ['contentType ' ]
93
+ $ dir = $ this ->filesystem ->getDirectoryWrite ($ this ->options ['directoryCode ' ]);
94
+ if (!isset ($ this ->options ['filePath ' ])) {
95
+ throw new InvalidArgumentException ("File path is required. " );
96
+ }
97
+ if (!$ dir ->isExist ($ this ->options ['filePath ' ])) {
98
+ throw new InvalidArgumentException ("File ' {$ this ->options ['filePath ' ]}' does not exists. " );
99
+ }
100
+ $ filePath = $ this ->options ['filePath ' ];
101
+ $ contentType = $ this ->options ['contentType ' ]
105
102
?? $ dir ->stat ($ filePath )['mimeType ' ]
106
103
?? $ this ->mime ->getMimeType ($ dir ->getAbsolutePath ($ filePath ));
107
- $ contentLength = $ this ->fileOptions ['contentLength ' ]
104
+ $ contentLength = $ this ->options ['contentLength ' ]
108
105
?? $ dir ->stat ($ filePath )['size ' ];
109
- $ fileName = $ this ->fileOptions ['fileName ' ]
106
+ $ fileName = $ this ->options ['fileName ' ]
110
107
?? basename ($ filePath );
111
108
$ this ->response ->setHttpResponseCode (200 );
112
109
$ this ->response ->setHeader ('Content-type ' , $ contentType , true )
113
- ->setHeader ('Content-Length ' , $ contentLength )
110
+ ->setHeader ('Content-Length ' , $ contentLength, true )
114
111
->setHeader ('Content-Disposition ' , 'attachment; filename=" ' . $ fileName . '" ' , true )
115
112
->setHeader ('Pragma ' , 'public ' , true )
116
113
->setHeader ('Cache-Control ' , 'must-revalidate, post-check=0, pre-check=0 ' , true )
@@ -125,7 +122,7 @@ public function sendResponse()
125
122
echo $ stream ->read (1024 );
126
123
}
127
124
$ stream ->close ();
128
- if ($ this ->fileOptions ['remove ' ]) {
125
+ if ($ this ->options ['remove ' ]) {
129
126
$ dir ->delete ($ filePath );
130
127
}
131
128
$ this ->response ->clearBody ();
0 commit comments