Added chunked output for large files

This commit is contained in:
Eldad Fux
2020-07-11 07:17:45 +03:00
parent e18f3a55e4
commit cbbc45af52
2 changed files with 35 additions and 8 deletions
+1 -6
View File
@@ -75,14 +75,10 @@ App::post('/v1/storage/files')
//throw new Exception('File type not allowed', 400);
//}
// Check if file size is exceeding allowed limit
if (!$fileSize->isValid($file['size'])) {
if (!$fileSize->isValid($file['size'])) { // Check if file size is exceeding allowed limit
throw new Exception('File size not allowed', 400);
}
/*
* Models
*/
$device = Storage::getDevice('local');
if (!$upload->isValid($file['tmp_name'])) {
@@ -102,7 +98,6 @@ App::post('/v1/storage/files')
if (App::getEnv('_APP_STORAGE_ANTIVIRUS') === 'enabled') { // Check if scans are enabled
$antiVirus = new Network('clamav', 3310);
// Check if file size is exceeding allowed limit
if (!$antiVirus->fileScan($path)) {
$device->delete($path);
throw new Exception('Invalid file', 403);
+34 -2
View File
@@ -13,6 +13,23 @@ class Response extends UtopiaResponse
* @var SwooleResponse
*/
protected $swoole = null;
/**
* Mime Types
* with compression support
*
* @var array
*/
protected $compressed = [
'text/plain' => true,
'text/css' => true,
'text/javascript' => true,
'application/javascript' => true,
'application/json' => true,
'application/json; charset=UTF-8' => true,
'image/svg+xml' => true,
'application/xml+rss' => true,
];
/**
* Response constructor.
@@ -43,9 +60,24 @@ class Response extends UtopiaResponse
->appendHeaders()
;
$this->size = $this->size + mb_strlen(implode("\n", $this->headers)) + mb_strlen($body, '8bit');
$chunk = 2000000; // Max chunk of 2 mb
$length = strlen($body);
$this->swoole->end($body);
$this->size = $this->size + strlen(implode("\n", $this->headers)) + $length;
if(array_key_exists(
$this->contentType,
$this->compressed
) && ($length <= $chunk)) { // Dont compress with GZIP / Brotli if header is not listed and size is bigger than 2mb
$this->swoole->end($body);
}
else {
for ($i=0; $i < ceil($length / $chunk); $i++) {
$this->swoole->write(substr($body, ($i * $chunk), min((($i * $chunk) + $chunk), $length)));
}
$this->swoole->end();
}
$this->disablePayload();
}