@@ -92,19 +92,33 @@ public function __construct(
92
92
if (!$ dir ->isExist ($ this ->fileOptions ['filePath ' ])) {
93
93
throw new InvalidArgumentException ("File ' {$ this ->fileOptions ['filePath ' ]}' does not exists. " );
94
94
}
95
- $ this ->setFileHeaders ();
96
95
}
97
96
98
97
/**
99
98
* @inheritDoc
100
99
*/
101
100
public function sendResponse ()
102
101
{
102
+ $ dir = $ this ->filesystem ->getDirectoryWrite ($ this ->fileOptions ['directoryCode ' ]);
103
+ $ filePath = $ this ->fileOptions ['filePath ' ];
104
+ $ contentType = $ this ->fileOptions ['contentType ' ]
105
+ ?? $ dir ->stat ($ filePath )['mimeType ' ]
106
+ ?? $ this ->mime ->getMimeType ($ dir ->getAbsolutePath ($ filePath ));
107
+ $ contentLength = $ this ->fileOptions ['contentLength ' ]
108
+ ?? $ dir ->stat ($ filePath )['size ' ];
109
+ $ fileName = $ this ->fileOptions ['fileName ' ]
110
+ ?? basename ($ filePath );
111
+ $ this ->response ->setHttpResponseCode (200 );
112
+ $ this ->response ->setHeader ('Content-type ' , $ contentType , true )
113
+ ->setHeader ('Content-Length ' , $ contentLength )
114
+ ->setHeader ('Content-Disposition ' , 'attachment; filename=" ' . $ fileName . '" ' , true )
115
+ ->setHeader ('Pragma ' , 'public ' , true )
116
+ ->setHeader ('Cache-Control ' , 'must-revalidate, post-check=0, pre-check=0 ' , true )
117
+ ->setHeader ('Last-Modified ' , date ('r ' ), true );
118
+
103
119
$ this ->response ->sendHeaders ();
104
120
105
121
if (!$ this ->request ->isHead ()) {
106
- $ dir = $ this ->filesystem ->getDirectoryWrite ($ this ->fileOptions ['directoryCode ' ]);
107
- $ filePath = $ this ->fileOptions ['filePath ' ];
108
122
$ stream = $ dir ->openFile ($ filePath , 'r ' );
109
123
while (!$ stream ->eof ()) {
110
124
// phpcs:ignore Magento2.Security.LanguageConstruct.DirectOutput
@@ -143,30 +157,4 @@ public function clearHeader($name)
143
157
{
144
158
return $ this ->response ->clearHeader ($ name );
145
159
}
146
-
147
- /**
148
- * Set appropriate headers for the file attachment
149
- *
150
- * @return void
151
- * @throws \Magento\Framework\Exception\FileSystemException
152
- */
153
- private function setFileHeaders (): void
154
- {
155
- $ dir = $ this ->filesystem ->getDirectoryWrite ($ this ->fileOptions ['directoryCode ' ]);
156
- $ filePath = $ this ->fileOptions ['filePath ' ];
157
- $ contentType = $ this ->fileOptions ['contentType ' ]
158
- ?? $ dir ->stat ($ filePath )['mimeType ' ]
159
- ?? $ this ->mime ->getMimeType ($ dir ->getAbsolutePath ($ filePath ));
160
- $ contentLength = $ this ->fileOptions ['contentLength ' ]
161
- ?? $ dir ->stat ($ filePath )['size ' ];
162
- $ fileName = $ this ->fileOptions ['fileName ' ]
163
- ?? basename ($ filePath );
164
- $ this ->response ->setHttpResponseCode (200 );
165
- $ this ->response ->setHeader ('Content-type ' , $ contentType , true )
166
- ->setHeader ('Content-Length ' , $ contentLength )
167
- ->setHeader ('Content-Disposition ' , 'attachment; filename=" ' . $ fileName . '" ' , true )
168
- ->setHeader ('Pragma ' , 'public ' , true )
169
- ->setHeader ('Cache-Control ' , 'must-revalidate, post-check=0, pre-check=0 ' , true )
170
- ->setHeader ('Last-Modified ' , date ('r ' ), true );
171
- }
172
160
}
0 commit comments