@@ -92,19 +92,33 @@ public function __construct(
9292 if (!$ dir ->isExist ($ this ->fileOptions ['filePath ' ])) {
9393 throw new InvalidArgumentException ("File ' {$ this ->fileOptions ['filePath ' ]}' does not exists. " );
9494 }
95- $ this ->setFileHeaders ();
9695 }
9796
9897 /**
9998 * @inheritDoc
10099 */
101100 public function sendResponse ()
102101 {
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+
103119 $ this ->response ->sendHeaders ();
104120
105121 if (!$ this ->request ->isHead ()) {
106- $ dir = $ this ->filesystem ->getDirectoryWrite ($ this ->fileOptions ['directoryCode ' ]);
107- $ filePath = $ this ->fileOptions ['filePath ' ];
108122 $ stream = $ dir ->openFile ($ filePath , 'r ' );
109123 while (!$ stream ->eof ()) {
110124 // phpcs:ignore Magento2.Security.LanguageConstruct.DirectOutput
@@ -143,30 +157,4 @@ public function clearHeader($name)
143157 {
144158 return $ this ->response ->clearHeader ($ name );
145159 }
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- }
172160}
0 commit comments