From cbbc45af52dd2b586cbf40e32caaa1903694927e Mon Sep 17 00:00:00 2001 From: Eldad Fux Date: Sat, 11 Jul 2020 07:17:45 +0300 Subject: [PATCH] Added chunked output for large files --- app/controllers/api/storage.php | 7 +------ src/Appwrite/Swoole/Response.php | 36 ++++++++++++++++++++++++++++++-- 2 files changed, 35 insertions(+), 8 deletions(-) diff --git a/app/controllers/api/storage.php b/app/controllers/api/storage.php index d361507b2e..cd7fe327e6 100644 --- a/app/controllers/api/storage.php +++ b/app/controllers/api/storage.php @@ -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); diff --git a/src/Appwrite/Swoole/Response.php b/src/Appwrite/Swoole/Response.php index 8869c51a3a..3710c3a1dd 100644 --- a/src/Appwrite/Swoole/Response.php +++ b/src/Appwrite/Swoole/Response.php @@ -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(); }