From fc96625fd51848ce1a25031b1c29133ec00bcba3 Mon Sep 17 00:00:00 2001 From: Damodar Lohani Date: Thu, 8 Jul 2021 17:11:11 +0545 Subject: [PATCH] upload large file using chunks --- app/controllers/api/storage.php | 38 ++++++++++++++++++--------------- 1 file changed, 21 insertions(+), 17 deletions(-) diff --git a/app/controllers/api/storage.php b/app/controllers/api/storage.php index 16dd37622a..6bcd15cfbe 100644 --- a/app/controllers/api/storage.php +++ b/app/controllers/api/storage.php @@ -287,9 +287,9 @@ App::post('/v1/storage/buckets/:bucketId/files') $file = $request->getFiles('file'); - /* - * Validators - */ + /** + * Validators + */ $allowedFileExtensions = $bucket->getAttribute('allowedFileExtensions', []); $fileExt = new FileExt($allowedFileExtensions); @@ -365,7 +365,7 @@ App::post('/v1/storage/buckets/:bucketId/files') // Save to storage $size = $size ?? $device->getFileSize($fileTmpName); $path = $device->getPath($uploadId . '.' . \pathinfo($fileName, PATHINFO_EXTENSION)); - $path = $bucket->getId() . $path; + $path = str_ireplace($device->getRoot(), $device->getRoot() . DIRECTORY_SEPARATOR . $bucket->getId(), $path); $file = $dbForInternal->getDocument('files', $uploadId); @@ -395,21 +395,23 @@ App::post('/v1/storage/buckets/:bucketId/files') } // Compression - $data = $device->read($path); - if ($size <= APP_LIMIT_COMPRESSION) { - $compressor = new GZIP(); - $data = $compressor->compress($data); + if($size <= APP_LIMIT_COMPRESSION || $size <= APP_LIMIT_ENCRYPTION) { + $data = $device->read($path); + if ($size <= APP_LIMIT_COMPRESSION) { + $compressor = new GZIP(); + $data = $compressor->compress($data); + } + + if ($bucket->getAttribute('encryption', true) && $size <= APP_LIMIT_ENCRYPTION) { + $key = App::getEnv('_APP_OPENSSL_KEY_V1'); + $iv = OpenSSL::randomPseudoBytes(OpenSSL::cipherIVLength(OpenSSL::CIPHER_AES_128_GCM)); + $data = OpenSSL::encrypt($data, OpenSSL::CIPHER_AES_128_GCM, $key, 0, $iv, $tag); + } + if (!$device->write($path, $data, $mimeType)) { + throw new Exception('Failed to save file', 500); + } } - if ($bucket->getAttribute('encryption', true) && $size <= APP_LIMIT_ENCRYPTION) { - $key = App::getEnv('_APP_OPENSSL_KEY_V1'); - $iv = OpenSSL::randomPseudoBytes(OpenSSL::cipherIVLength(OpenSSL::CIPHER_AES_128_GCM)); - $data = OpenSSL::encrypt($data, OpenSSL::CIPHER_AES_128_GCM, $key, 0, $iv, $tag); - } - - if (!$device->write($path, $data, $mimeType)) { - throw new Exception('Failed to save file', 500); - } $sizeActual = $device->getFileSize($path); @@ -975,6 +977,8 @@ App::delete('/v1/storage/buckets/:bucketId/files/:fileId') if (!$dbForInternal->deleteDocument('files', $fileId)) { throw new Exception('Failed to remove file from DB', 500); } + } else { + throw new Exception('Failed to delete file from device', 500); } $audits