1
0
Fork 0
mirror of synced 2024-06-02 19:04:49 +12:00

use external db for files storage

This commit is contained in:
Damodar Lohani 2021-11-11 12:05:52 +05:45
parent 777e9de8bc
commit 2de39155b6

View file

@ -58,17 +58,19 @@ App::post('/v1/storage/buckets')
->param('antiVirus', true, new Boolean(), 'Is virus scanning enabled? For file size above ' . Storage::human(APP_LIMIT_ANTIVIRUS) . ' AntiVirus scanning is skipped even if it\'s enabled', true)
->inject('response')
->inject('dbForInternal')
->inject('dbForExternal')
->inject('audits')
->inject('usage')
->action(function ($bucketId, $name, $permission, $read, $write, $maximumFileSize, $allowedFileExtensions, $enabled, $adapter, $encryption, $antiVirus, $response, $dbForInternal, $audits, $usage) {
->action(function ($bucketId, $name, $permission, $read, $write, $maximumFileSize, $allowedFileExtensions, $enabled, $adapter, $encryption, $antiVirus, $response, $dbForInternal, $dbForExternal, $audits, $usage) {
/** @var Appwrite\Utopia\Response $response */
/** @var Utopia\Database\Database $dbForInternal */
/** @var Utopia\Database\Database $dbForExternal */
/** @var Appwrite\Event\Event $audits */
/** @var Appwrite\Stats\Stats $usage */
$bucketId = $bucketId === 'unique()' ? $dbForInternal->getId() : $bucketId;
try {
$dbForInternal->createCollection('bucket_' . $bucketId, [
$dbForExternal->createCollection('bucket_' . $bucketId, [
new Document([
'$id' => 'dateCreated',
'type' => Database::VAR_INTEGER,
@ -257,7 +259,7 @@ App::post('/v1/storage/buckets')
'$collection' => 'buckets',
'dateCreated' => \time(),
'dateUpdated' => \time(),
'name' => $name,
'name' => $name,
'permission' => $permission,
'maximumFileSize' => $maximumFileSize,
'allowedFileExtensions' => $allowedFileExtensions,
@ -507,13 +509,15 @@ App::post('/v1/storage/buckets/:bucketId/files')
->inject('request')
->inject('response')
->inject('dbForInternal')
->inject('dbForExternal')
->inject('user')
->inject('audits')
->inject('usage')
->action(function ($bucketId, $fileId, $file, $read, $write, $request, $response, $dbForInternal, $user, $audits, $usage) {
->action(function ($bucketId, $fileId, $file, $read, $write, $request, $response, $dbForInternal, $dbForExternal, $user, $audits, $usage) {
/** @var Utopia\Swoole\Request $request */
/** @var Appwrite\Utopia\Response $response */
/** @var Utopia\Database\Database $dbForInternal */
/** @var Utopia\Database\Database $dbForExternal */
/** @var Utopia\Database\Document $user */
/** @var Appwrite\Event\Event $audits */
/** @var Appwrite\Stats\Stats $usage */
@ -638,11 +642,11 @@ App::post('/v1/storage/buckets/:bucketId/files')
try {
if($bucket->getAttribute('permission') === 'bucket') {
$file = Authorization::skip(function() use ($dbForInternal, $bucket, $data) {
return $dbForInternal->createDocument('bucket_' . $bucket->getId(), new Document($data));
$file = Authorization::skip(function() use ($dbForExternal, $bucket, $data) {
return $dbForExternal->createDocument('bucket_' . $bucket->getId(), new Document($data));
});
} else {
$file = $dbForInternal->createDocument('bucket_' . $bucket->getId(), new Document($data));
$file = $dbForExternal->createDocument('bucket_' . $bucket->getId(), new Document($data));
}
}
@ -691,10 +695,12 @@ App::get('/v1/storage/buckets/:bucketId/files')
->param('orderType', 'ASC', new WhiteList(['ASC', 'DESC'], true), 'Order result by ASC or DESC order.', true)
->inject('response')
->inject('dbForInternal')
->inject('dbForExternal')
->inject('usage')
->action(function ($bucketId, $search, $limit, $offset, $cursor, $cursorDirection, $orderType, $response, $dbForInternal, $usage) {
->action(function ($bucketId, $search, $limit, $offset, $cursor, $cursorDirection, $orderType, $response, $dbForInternal, $dbForExternal, $usage) {
/** @var Appwrite\Utopia\Response $response */
/** @var Utopia\Database\Database $dbForInternal */
/** @var Utopia\Database\Database $dbForExternal */
/** @var Appwrite\Stats\Stats $usage */
$bucket = $dbForInternal->getDocument('buckets', $bucketId);
@ -719,11 +725,11 @@ App::get('/v1/storage/buckets/:bucketId/files')
if (!empty($cursor)) {
if($bucket->getAttribute('permission') ==='bucket') {
$cursorFile = Authorization::skip(function() use ($dbForInternal, $bucket, $cursor) {
return $dbForInternal->getDocument('bucket_' . $bucket->getId(), $cursor);
$cursorFile = Authorization::skip(function() use ($dbForExternal, $bucket, $cursor) {
return $dbForExternal->getDocument('bucket_' . $bucket->getId(), $cursor);
});
} else {
$cursorFile = $dbForInternal->getDocument('bucket_' . $bucket->getId(), $cursor);
$cursorFile = $dbForExternal->getDocument('bucket_' . $bucket->getId(), $cursor);
}
if ($cursorFile->isEmpty()) {
@ -738,11 +744,11 @@ App::get('/v1/storage/buckets/:bucketId/files')
}
if($bucket->getAttribute('permission') === 'bucket') {
$files = Authorization::skip(function() use ($dbForInternal, $bucketId, $queries, $limit, $offset, $cursor, $cursorDirection, $orderType) {
return $dbForInternal->find('bucket_' . $bucketId, $queries, $limit, $offset, [], [$orderType], $cursorFile ?? null, $cursorDirection);
$files = Authorization::skip(function() use ($dbForExternal, $bucketId, $queries, $limit, $offset, $cursor, $cursorDirection, $orderType) {
return $dbForExternal->find('bucket_' . $bucketId, $queries, $limit, $offset, [], [$orderType], $cursorFile ?? null, $cursorDirection);
});
} else {
$files = $dbForInternal->find('bucket_' . $bucketId, $queries, $limit, $offset, [], [$orderType], $cursorFile ?? null, $cursorDirection);
$files = $dbForExternal->find('bucket_' . $bucketId, $queries, $limit, $offset, [], [$orderType], $cursorFile ?? null, $cursorDirection);
}
$usage
@ -752,7 +758,7 @@ App::get('/v1/storage/buckets/:bucketId/files')
$response->dynamic(new Document([
'files' => $files,
'sum' => $dbForInternal->count('bucket_' . $bucketId, $queries, APP_LIMIT_COUNT),
'sum' => $dbForExternal->count('bucket_' . $bucketId, $queries, APP_LIMIT_COUNT),
]), Response::MODEL_FILE_LIST);
});
@ -772,10 +778,12 @@ App::get('/v1/storage/buckets/:bucketId/files/:fileId')
->param('fileId', '', new UID(), 'File unique ID.')
->inject('response')
->inject('dbForInternal')
->inject('dbForExternal')
->inject('usage')
->action(function ($bucketId, $fileId, $response, $dbForInternal, $usage) {
->action(function ($bucketId, $fileId, $response, $dbForInternal, $dbForExternal, $usage) {
/** @var Appwrite\Utopia\Response $response */
/** @var Utopia\Database\Database $dbForInternal */
/** @var Utopia\Database\Database $dbForExternal */
/** @var Appwrite\Stats\Stats $usage */
$bucket = $dbForInternal->getDocument('buckets', $bucketId);
@ -793,11 +801,11 @@ App::get('/v1/storage/buckets/:bucketId/files/:fileId')
}
if($bucket->getAttribute('permission') === 'bucket') {
$file = Authorization::skip(function() use ($dbForInternal, $bucketId, $fileId) {
return $dbForInternal->getDocument('bucket_' . $bucketId, $fileId);
$file = Authorization::skip(function() use ($dbForExternal, $bucketId, $fileId) {
return $dbForExternal->getDocument('bucket_' . $bucketId, $fileId);
});
} else {
$file = $dbForInternal->getDocument('bucket_' . $bucketId, $fileId);
$file = $dbForExternal->getDocument('bucket_' . $bucketId, $fileId);
}
if ($file->isEmpty() || $file->getAttribute('bucketId') !== $bucketId) {
@ -839,12 +847,14 @@ App::get('/v1/storage/buckets/:bucketId/files/:fileId/preview')
->inject('response')
->inject('project')
->inject('dbForInternal')
->inject('dbForExternal')
->inject('usage')
->action(function ($bucketId, $fileId, $width, $height, $gravity, $quality, $borderWidth, $borderColor, $borderRadius, $opacity, $rotation, $background, $output, $request, $response, $project, $dbForInternal, $usage) {
->action(function ($bucketId, $fileId, $width, $height, $gravity, $quality, $borderWidth, $borderColor, $borderRadius, $opacity, $rotation, $background, $output, $request, $response, $project, $dbForInternal, $dbForExternal, $usage) {
/** @var Utopia\Swoole\Request $request */
/** @var Appwrite\Utopia\Response $response */
/** @var Utopia\Database\Document $project */
/** @var Utopia\Database\Database $dbForInternal */
/** @var Utopia\Database\Database $dbForExternal */
/** @var Appwrite\Stats\Stats $usage */
$storage = 'files';
@ -883,11 +893,11 @@ App::get('/v1/storage/buckets/:bucketId/files/:fileId/preview')
if($bucket->getAttribute('permission')==='bucket') {
// skip authorization
$file = Authorization::skip(function () use ($dbForInternal, $bucketId, $fileId) {
return $dbForInternal->getDocument('bucket_' . $bucketId, $fileId);
$file = Authorization::skip(function () use ($dbForExternal, $bucketId, $fileId) {
return $dbForExternal->getDocument('bucket_' . $bucketId, $fileId);
});
} else {
$file = $dbForInternal->getDocument('bucket_' . $bucketId, $fileId);
$file = $dbForExternal->getDocument('bucket_' . $bucketId, $fileId);
}
if ($file->isEmpty() || $file->getAttribute('bucketId') !== $bucketId) {
@ -1008,10 +1018,12 @@ App::get('/v1/storage/buckets/:bucketId/files/:fileId/download')
->param('fileId', '', new UID(), 'File unique ID.')
->inject('response')
->inject('dbForInternal')
->inject('dbForExternal')
->inject('usage')
->action(function ($bucketId, $fileId, $response, $dbForInternal, $usage) {
->action(function ($bucketId, $fileId, $response, $dbForInternal, $dbForExternal, $usage) {
/** @var Appwrite\Utopia\Response $response */
/** @var Utopia\Database\Database $dbForInternal */
/** @var Utopia\Database\Database $dbForExternal */
/** @var Appwrite\Stats\Stats $usage */
$bucket = $dbForInternal->getDocument('buckets', $bucketId);
@ -1029,11 +1041,11 @@ App::get('/v1/storage/buckets/:bucketId/files/:fileId/download')
}
if($bucket->getAttribute('permission') === 'bucket') {
$file = Authorization::skip(function() use ($dbForInternal, $fileId, $bucketId) {
return $dbForInternal->getDocument('bucket_' . $bucketId, $fileId);
$file = Authorization::skip(function() use ($dbForExternal, $fileId, $bucketId) {
return $dbForExternal->getDocument('bucket_' . $bucketId, $fileId);
});
} else {
$file = $dbForInternal->getDocument('bucket_' . $bucketId, $fileId);
$file = $dbForExternal->getDocument('bucket_' . $bucketId, $fileId);
}
if ($file->isEmpty() || $file->getAttribute('bucketId') !== $bucketId) {
@ -1095,8 +1107,9 @@ App::get('/v1/storage/buckets/:bucketId/files/:fileId/view')
->param('fileId', '', new UID(), 'File unique ID.')
->inject('response')
->inject('dbForInternal')
->inject('dbForExternal')
->inject('usage')
->action(function ($bucketId, $fileId, $response, $dbForInternal, $usage) {
->action(function ($bucketId, $fileId, $response, $dbForInternal, $dbForExternal, $usage) {
/** @var Appwrite\Utopia\Response $response */
/** @var Utopia\Database\Database $dbForInternal */
/** @var Appwrite\Stats\Stats $usage */
@ -1116,11 +1129,11 @@ App::get('/v1/storage/buckets/:bucketId/files/:fileId/view')
}
if($bucket->getAttribute('permission') === 'bucket') {
$file = Authorization::skip(function() use ($dbForInternal, $fileId, $bucketId) {
return $dbForInternal->getDocument('bucket_' . $bucketId, $fileId);
$file = Authorization::skip(function() use ($dbForExternal, $fileId, $bucketId) {
return $dbForExternal->getDocument('bucket_' . $bucketId, $fileId);
});
} else {
$file = $dbForInternal->getDocument('bucket_' . $bucketId, $fileId);
$file = $dbForExternal->getDocument('bucket_' . $bucketId, $fileId);
}
$mimes = Config::getParam('storage-mimes');
@ -1195,9 +1208,10 @@ App::put('/v1/storage/buckets/:bucketId/files/:fileId')
->param('write', null, new Permissions(), 'An array of strings with write permissions. By default no user is granted with any write permissions. [learn more about permissions](/docs/permissions) and get a full list of available permissions.')
->inject('response')
->inject('dbForInternal')
->inject('dbForExternal')
->inject('audits')
->inject('usage')
->action(function ($bucketId, $fileId, $read, $write, $response, $dbForInternal, $audits, $usage) {
->action(function ($bucketId, $fileId, $read, $write, $response, $dbForInternal, $dbForExternal, $audits, $usage) {
/** @var Appwrite\Utopia\Response $response */
/** @var Utopia\Database\Database $dbForInternal */
/** @var Appwrite\Event\Event $audits */
@ -1218,11 +1232,11 @@ App::put('/v1/storage/buckets/:bucketId/files/:fileId')
}
if($bucket->getAttribute('permission') === 'bucket') {
$file = Authorization::skip(function() use ($dbForInternal, $fileId, $bucketId) {
return $dbForInternal->getDocument('bucket_' . $bucketId, $fileId);
$file = Authorization::skip(function() use ($dbForExternal, $fileId, $bucketId) {
return $dbForExternal->getDocument('bucket_' . $bucketId, $fileId);
});
} else {
$file = $dbForInternal->getDocument('bucket_' . $bucketId, $fileId);
$file = $dbForExternal->getDocument('bucket_' . $bucketId, $fileId);
}
if ($file->isEmpty() || $file->getAttribute('bucketId') !== $bucketId) {
@ -1230,14 +1244,14 @@ App::put('/v1/storage/buckets/:bucketId/files/:fileId')
}
if($bucket->getAttribute('permission') === 'bucket') {
$file = Authorization::skip(function() use ($dbForInternal, $fileId, $bucketId, $file, $read, $write) {
return $dbForInternal->updateDocument('bucket_' . $bucketId, $fileId, $file
$file = Authorization::skip(function() use ($dbForExternal, $fileId, $bucketId, $file, $read, $write) {
return $dbForExternal->updateDocument('bucket_' . $bucketId, $fileId, $file
->setAttribute('$read', $read)
->setAttribute('$write', $write)
);
});
} else {
$file = $dbForInternal->updateDocument('bucket_' . $bucketId, $fileId, $file
$file = $dbForExternal->updateDocument('bucket_' . $bucketId, $fileId, $file
->setAttribute('$read', $read)
->setAttribute('$write', $write)
);
@ -1272,12 +1286,14 @@ App::delete('/v1/storage/buckets/:bucketId/files/:fileId')
->param('fileId', '', new UID(), 'File unique ID.')
->inject('response')
->inject('dbForInternal')
->inject('dbForExternal')
->inject('events')
->inject('audits')
->inject('usage')
->action(function ($bucketId, $fileId, $response, $dbForInternal, $events, $audits, $usage) {
->action(function ($bucketId, $fileId, $response, $dbForInternal, $dbForExternal, $events, $audits, $usage) {
/** @var Appwrite\Utopia\Response $response */
/** @var Utopia\Database\Database $dbForInternal */
/** @var Utopia\Database\Database $dbForExternal */
/** @var Appwrite\Event\Event $events */
/** @var Appwrite\Event\Event $audits */
/** @var Appwrite\Stats\Stats $usage */
@ -1297,11 +1313,11 @@ App::delete('/v1/storage/buckets/:bucketId/files/:fileId')
}
if($bucket->getAttribute('permission') === 'bucket') {
$file = Authorization::skip(function() use ($dbForInternal, $fileId, $bucketId) {
return $dbForInternal->getDocument('bucket_' . $bucketId, $fileId);
$file = Authorization::skip(function() use ($dbForExternal, $fileId, $bucketId) {
return $dbForExternal->getDocument('bucket_' . $bucketId, $fileId);
});
} else {
$file = $dbForInternal->getDocument('bucket_' . $bucketId, $fileId);
$file = $dbForExternal->getDocument('bucket_' . $bucketId, $fileId);
}
if ($file->isEmpty() || $file->getAttribute('bucketId') !== $bucketId) {
@ -1312,11 +1328,11 @@ App::delete('/v1/storage/buckets/:bucketId/files/:fileId')
if ($device->delete($file->getAttribute('path', ''))) {
if($bucket->getAttribute('permission') === 'bucket') {
$deleted = Authorization::skip(function() use ($dbForInternal, $fileId, $bucketId) {
return $dbForInternal->deleteDocument('bucket_' . $bucketId, $fileId);
$deleted = Authorization::skip(function() use ($dbForExternal, $fileId, $bucketId) {
return $dbForExternal->deleteDocument('bucket_' . $bucketId, $fileId);
});
} else {
$deleted = $dbForInternal->deleteDocument('bucket_' . $bucketId, $fileId);
$deleted = $dbForExternal->deleteDocument('bucket_' . $bucketId, $fileId);
}
if (!$deleted) {
throw new Exception('Failed to remove file from DB', 500);