From 09bbc291f4703053edbd5cbc7b9ff417d0478e40 Mon Sep 17 00:00:00 2001 From: Khushboo Verma <43381712+vermakhushboo@users.noreply.github.com> Date: Thu, 18 Apr 2024 15:45:05 +0530 Subject: [PATCH] Add comments --- app/controllers/api/storage.php | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/app/controllers/api/storage.php b/app/controllers/api/storage.php index 7158d04eff..81273c0fa2 100644 --- a/app/controllers/api/storage.php +++ b/app/controllers/api/storage.php @@ -1111,20 +1111,30 @@ App::get('/v1/storage/buckets/:bucketId/files/:fileId/download') if ($size > APP_STORAGE_READ_BUFFER) { $buffer = ''; $bytesRead = 0; + while ($bytesRead < $size) { + // Determine how many bytes to read in this iteration $bytesToRead = min(APP_STORAGE_READ_BUFFER, $size - $bytesRead); + + // Read data from the device and append to the buffer $buffer .= $deviceFiles->read($path, $bytesRead, $bytesToRead); $bytesRead += $bytesToRead; + // Process the buffer in chunks of output size while (strlen($buffer) >= MAX_OUTPUT_CHUNK_SIZE) { + // Send an output chunk of specified size $response->chunk(substr($buffer, 0, MAX_OUTPUT_CHUNK_SIZE), false); + // Remove the sent chunk from the buffer $buffer = substr($buffer, MAX_OUTPUT_CHUNK_SIZE); } } + + // Send any remaining data as a final chunk if (strlen($buffer) > 0) { $response->chunk($buffer, true); } } else { + // If the file is smaller than the buffer size, read and send the whole file $response->send($deviceFiles->read($path)); } });