1
0
Fork 0
mirror of synced 2024-06-02 10:54:44 +12:00

download with range header

This commit is contained in:
Damodar Lohani 2021-09-12 12:48:25 +05:45
parent 8caa06ef88
commit 40898ae313

View file

@ -1017,10 +1017,12 @@ App::get('/v1/storage/buckets/:bucketId/files/:fileId/download')
->label('sdk.methodType', 'location')
->param('bucketId', null, new UID(), 'Storage bucket unique ID. You can create a new storage bucket using the Storage service [server integration](/docs/server/storage#createBucket).')
->param('fileId', '', new UID(), 'File unique ID.')
->inject('request')
->inject('response')
->inject('dbForInternal')
->inject('usage')
->action(function ($bucketId, $fileId, $response, $dbForInternal, $usage) {
->action(function ($bucketId, $fileId, $request, $response, $dbForInternal, $usage) {
/** @var Utopia\Swoole\Request $request */
/** @var Appwrite\Utopia\Response $response */
/** @var Utopia\Database\Database $dbForInternal */
/** @var Appwrite\Stats\Stats $usage */
@ -1045,6 +1047,11 @@ App::get('/v1/storage/buckets/:bucketId/files/:fileId/download')
throw new Exception('File not found in ' . $path, 404);
}
$usage
->setParam('storage.files.read', 1)
->setParam('bucketId', $bucketId)
;
$response
->setContentType($file->getAttribute('mimeType'))
->addHeader('Expires', \date('D, d M Y H:i:s', \time() + (60 * 60 * 24 * 45)) . ' GMT') // 45 days cache
@ -1052,6 +1059,24 @@ App::get('/v1/storage/buckets/:bucketId/files/:fileId/download')
->addHeader('Content-Disposition', 'attachment; filename="' . $file->getAttribute('name', '') . '"')
;
$size = $device->getFileSize($path);
// Range header
list($unit, $range) = explode('=', $request->getHeader('Range'));
if($unit == 'bytes' && !empty($range)) {
list($start, $end) = explode('-', $range);
$start = (int) $start;
$end = (int) $end;
if(($start > $end) || $end > $size) {
throw new Exception('Invalid Range', 400);
}
$response->addHeader('Accept-Ranges', 'bytes');
$response->addHeader('Content-Range', 'bytes ' . $start . '-' . $end . '/' . $size);
$response->addHeader('Content-Length', $end - $start + 1);
$response->send($device->read($path, $start, $end));
}
$source = '';
if (!empty($file->getAttribute('openSSLCipher'))) { // Decrypt
$source = $device->read($path);
@ -1073,16 +1098,10 @@ App::get('/v1/storage/buckets/:bucketId/files/:fileId/download')
$source = $compressor->decompress($source);
}
$usage
->setParam('storage.files.read', 1)
->setParam('bucketId', $bucketId)
;
if(!empty($source)) {
$response->send($source);
}
$size = $device->getFileSize($path);
if ($size > APP_STORAGE_READ_BUFFER) {
$response->addHeader('Content-Length', $device->getFileSize($path));
$chunk = 2000000; // Max chunk of 2 mb