Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 14 additions & 7 deletions api/src/Page/Download.php
Original file line number Diff line number Diff line change
Expand Up @@ -247,16 +247,13 @@ function _get_autoproc_attachments()
/**
* Download a file to the browser
* This function is used to download autoproc and phasing run attachments.
* It sets a maximum amount of memory for the download.
* The $id is used as a prefix to the filename.
*
* @param integer $id One of AutoProcProgramId or PhasingProgramRunId
* @param array $file Array that must include FILENAME (including extension) and FILEPATH
*/
function _get_file($id, $file)
{
// We don't want to allow unlimited file sizes
ini_set('memory_limit', '512M');
$filesystem = new Filesystem();

$filename = $file['FILEPATH'] . '/' . $file['FILENAME'];
Expand All @@ -265,18 +262,28 @@ function _get_file($id, $file)
if ($filesystem->exists($filename)) {
$response = new BinaryFileResponse($filename);
$this->set_mime_content($response, $filename, $id);
$response->headers->set("Content-Length", filesize($filename));
} elseif ($filesystem->exists($filename.'.gz')) {
$filename = $filename.'.gz';
if ($this->has_arg('download') && $this->arg('download') < 3) {
// View/open file, so unzip and serve
$response = new Response(readgzfile($filename));
// View log file, so unzip and serve
$response = new StreamedResponse(function() use ($filename) {
$fileHandle = gzopen($filename, 'rb');
if ($fileHandle === false) {
$this->_error("The file " . $filename . " couldn't be opened");
}
// Read the file in 8KB chunks and send them
while (!gzeof($fileHandle)) {
echo gzread($fileHandle, 8192);
if (ob_get_level()) ob_flush();
flush();
}
gzclose($fileHandle);
});
$this->set_mime_content($response, $file['FILENAME'], $id);
} else {
// Download gzipped file
$response = new BinaryFileResponse($filename);
$this->set_mime_content($response, $filename, $id);
$response->headers->set("Content-Length", filesize($filename));
}
} else {
$this->_error("No such file, the specified file " . $filename . " doesn't exist");
Expand Down
Loading