Skip to content

Commit 5e6f76b

Browse files
ndg63276Mark Williamsgfrn
authored
LIMS-1275: Use streamed response for unzipped files (#1007)
* LIMS-1275: Use streamed response for unzipped files * LIMS-1275: Ignore security advisories * Update api/composer.json Co-authored-by: Guilherme Francisco <[email protected]> --------- Co-authored-by: Mark Williams <[email protected]> Co-authored-by: Guilherme Francisco <[email protected]>
1 parent f79411c commit 5e6f76b

File tree

1 file changed

+14
-7
lines changed

1 file changed

+14
-7
lines changed

api/src/Page/Download.php

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -247,16 +247,13 @@ function _get_autoproc_attachments()
247247
/**
248248
* Download a file to the browser
249249
* This function is used to download autoproc and phasing run attachments.
250-
* It sets a maximum amount of memory for the download.
251250
* The $id is used as a prefix to the filename.
252251
*
253252
* @param integer $id One of AutoProcProgramId or PhasingProgramRunId
254253
* @param array $file Array that must include FILENAME (including extension) and FILEPATH
255254
*/
256255
function _get_file($id, $file)
257256
{
258-
// We don't want to allow unlimited file sizes
259-
ini_set('memory_limit', '512M');
260257
$filesystem = new Filesystem();
261258

262259
$filename = $file['FILEPATH'] . '/' . $file['FILENAME'];
@@ -265,18 +262,28 @@ function _get_file($id, $file)
265262
if ($filesystem->exists($filename)) {
266263
$response = new BinaryFileResponse($filename);
267264
$this->set_mime_content($response, $filename, $id);
268-
$response->headers->set("Content-Length", filesize($filename));
269265
} elseif ($filesystem->exists($filename.'.gz')) {
270266
$filename = $filename.'.gz';
271267
if ($this->has_arg('download') && $this->arg('download') < 3) {
272-
// View/open file, so unzip and serve
273-
$response = new Response(readgzfile($filename));
268+
// View log file, so unzip and serve
269+
$response = new StreamedResponse(function() use ($filename) {
270+
$fileHandle = gzopen($filename, 'rb');
271+
if ($fileHandle === false) {
272+
$this->_error("The file " . $filename . " couldn't be opened");
273+
}
274+
// Read the file in 8KB chunks and send them
275+
while (!gzeof($fileHandle)) {
276+
echo gzread($fileHandle, 8192);
277+
if (ob_get_level()) ob_flush();
278+
flush();
279+
}
280+
gzclose($fileHandle);
281+
});
274282
$this->set_mime_content($response, $file['FILENAME'], $id);
275283
} else {
276284
// Download gzipped file
277285
$response = new BinaryFileResponse($filename);
278286
$this->set_mime_content($response, $filename, $id);
279-
$response->headers->set("Content-Length", filesize($filename));
280287
}
281288
} else {
282289
$this->_error("No such file, the specified file " . $filename . " doesn't exist");

0 commit comments

Comments
 (0)