Merge pull request #7351 from appwrite/pea-33-improve-large-files-handling

Improve large file handling
This commit is contained in:
Eldad A. Fux 2023-12-31 08:48:32 +01:00 committed by GitHub
commit f5705df02c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -551,6 +551,11 @@ App::post('/v1/storage/buckets/:bucketId/files')
break; break;
} }
$data = $compressor->compress($data); $data = $compressor->compress($data);
} else {
// reset the algorithm to none as we do not compress the file
// if file size exceedes the APP_STORAGE_READ_BUFFER
// regardless the bucket compression algoorithm
$algorithm = COMPRESSION_TYPE_NONE;
} }
if ($bucket->getAttribute('encryption', true) && $fileSize <= APP_STORAGE_READ_BUFFER) { if ($bucket->getAttribute('encryption', true) && $fileSize <= APP_STORAGE_READ_BUFFER) {
@ -872,14 +877,6 @@ App::get('/v1/storage/buckets/:bucketId/files/:fileId/preview')
throw new Exception(Exception::USER_UNAUTHORIZED); throw new Exception(Exception::USER_UNAUTHORIZED);
} }
if ((\strpos($request->getAccept(), 'image/webp') === false) && ('webp' === $output)) { // Fallback webp to jpeg when no browser support
$output = 'jpg';
}
$inputs = Config::getParam('storage-inputs');
$outputs = Config::getParam('storage-outputs');
$fileLogos = Config::getParam('storage-logos');
if ($fileSecurity && !$valid) { if ($fileSecurity && !$valid) {
$file = $dbForProject->getDocument('bucket_' . $bucket->getInternalId(), $fileId); $file = $dbForProject->getDocument('bucket_' . $bucket->getInternalId(), $fileId);
} else { } else {
@ -890,9 +887,17 @@ App::get('/v1/storage/buckets/:bucketId/files/:fileId/preview')
throw new Exception(Exception::STORAGE_FILE_NOT_FOUND); throw new Exception(Exception::STORAGE_FILE_NOT_FOUND);
} }
if ((\strpos($request->getAccept(), 'image/webp') === false) && ('webp' === $output)) { // Fallback webp to jpeg when no browser support
$output = 'jpg';
}
$inputs = Config::getParam('storage-inputs');
$outputs = Config::getParam('storage-outputs');
$fileLogos = Config::getParam('storage-logos');
$path = $file->getAttribute('path'); $path = $file->getAttribute('path');
$type = \strtolower(\pathinfo($path, PATHINFO_EXTENSION)); $type = \strtolower(\pathinfo($path, PATHINFO_EXTENSION));
$algorithm = $file->getAttribute('algorithm', 'none'); $algorithm = $file->getAttribute('algorithm', COMPRESSION_TYPE_NONE);
$cipher = $file->getAttribute('openSSLCipher'); $cipher = $file->getAttribute('openSSLCipher');
$mime = $file->getAttribute('mimeType'); $mime = $file->getAttribute('mimeType');
if (!\in_array($mime, $inputs) || $file->getAttribute('sizeActual') > (int) App::getEnv('_APP_STORAGE_PREVIEW_LIMIT', 20000000)) { if (!\in_array($mime, $inputs) || $file->getAttribute('sizeActual') > (int) App::getEnv('_APP_STORAGE_PREVIEW_LIMIT', 20000000)) {
@ -903,7 +908,7 @@ App::get('/v1/storage/buckets/:bucketId/files/:fileId/preview')
$path = $fileLogos['default_image']; $path = $fileLogos['default_image'];
} }
$algorithm = 'none'; $algorithm = COMPRESSION_TYPE_NONE;
$cipher = null; $cipher = null;
$background = (empty($background)) ? 'eceff1' : $background; $background = (empty($background)) ? 'eceff1' : $background;
$type = \strtolower(\pathinfo($path, PATHINFO_EXTENSION)); $type = \strtolower(\pathinfo($path, PATHINFO_EXTENSION));
@ -926,7 +931,6 @@ App::get('/v1/storage/buckets/:bucketId/files/:fileId/preview')
$output = empty($type) ? (array_search($mime, $outputs) ?? 'jpg') : $type; $output = empty($type) ? (array_search($mime, $outputs) ?? 'jpg') : $type;
} }
$source = $deviceFiles->read($path); $source = $deviceFiles->read($path);
if (!empty($cipher)) { // Decrypt if (!empty($cipher)) { // Decrypt
@ -941,11 +945,11 @@ App::get('/v1/storage/buckets/:bucketId/files/:fileId/preview')
} }
switch ($algorithm) { switch ($algorithm) {
case 'zstd': case COMPRESSION_TYPE_ZSTD:
$compressor = new Zstd(); $compressor = new Zstd();
$source = $compressor->decompress($source); $source = $compressor->decompress($source);
break; break;
case 'gzip': case COMPRESSION_TYPE_GZIP:
$compressor = new GZIP(); $compressor = new GZIP();
$source = $compressor->decompress($source); $source = $compressor->decompress($source);
break; break;
@ -1086,15 +1090,15 @@ App::get('/v1/storage/buckets/:bucketId/files/:fileId/download')
); );
} }
switch ($file->getAttribute('algorithm', 'none')) { switch ($file->getAttribute('algorithm', COMPRESSION_TYPE_NONE)) {
case 'zstd': case COMPRESSION_TYPE_ZSTD:
if (empty($source)) { if (empty($source)) {
$source = $deviceFiles->read($path); $source = $deviceFiles->read($path);
} }
$compressor = new Zstd(); $compressor = new Zstd();
$source = $compressor->decompress($source); $source = $compressor->decompress($source);
break; break;
case 'gzip': case COMPRESSION_TYPE_GZIP:
if (empty($source)) { if (empty($source)) {
$source = $deviceFiles->read($path); $source = $deviceFiles->read($path);
} }
@ -1237,15 +1241,15 @@ App::get('/v1/storage/buckets/:bucketId/files/:fileId/view')
); );
} }
switch ($file->getAttribute('algorithm', 'none')) { switch ($file->getAttribute('algorithm', COMPRESSION_TYPE_NONE)) {
case 'zstd': case COMPRESSION_TYPE_ZSTD:
if (empty($source)) { if (empty($source)) {
$source = $deviceFiles->read($path); $source = $deviceFiles->read($path);
} }
$compressor = new Zstd(); $compressor = new Zstd();
$source = $compressor->decompress($source); $source = $compressor->decompress($source);
break; break;
case 'gzip': case COMPRESSION_TYPE_GZIP:
if (empty($source)) { if (empty($source)) {
$source = $deviceFiles->read($path); $source = $deviceFiles->read($path);
} }
@ -1259,10 +1263,12 @@ App::get('/v1/storage/buckets/:bucketId/files/:fileId/view')
$response->send(substr($source, $start, ($end - $start + 1))); $response->send(substr($source, $start, ($end - $start + 1)));
} }
$response->send($source); $response->send($source);
return;
} }
if (!empty($rangeHeader)) { if (!empty($rangeHeader)) {
$response->send($deviceFiles->read($path, $start, ($end - $start + 1))); $response->send($deviceFiles->read($path, $start, ($end - $start + 1)));
return;
} }
$size = $deviceFiles->getFileSize($path); $size = $deviceFiles->getFileSize($path);