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

create bucket fixing with new db

This commit is contained in:
Damodar Lohani 2021-06-15 14:19:49 +05:45
parent 5ff4781574
commit 6939002932
3 changed files with 142 additions and 21 deletions

View file

@ -967,6 +967,131 @@ $collections = [
]
],
],
'buckets' => [
'$collection' => Database::COLLECTIONS,
'$id' => 'buckets',
'$permissions' => ['read' => ['*']],
'name' => 'Buckets',
'structure' => true,
'attributes' => [
[
'$id' => 'dateCreated',
'type' => Database::VAR_INTEGER,
'format' => '',
'signed' => false,
'size' => 0,
'required' => false,
'array' => false,
'filters' => [],
],
[
'$id' => 'dateUpdated',
'type' => Database::VAR_INTEGER,
'size' => 0,
'format' => '',
'signed' => true,
'required' => false,
'array' => false,
'filters' => [],
],
[
'$id' => 'enabled',
'type' => Database::VAR_BOOLEAN,
'signed' => true,
'size' => 0,
'format' => '',
'filters' => [],
'required' => true,
'array' => false,
],
[
'$id' => 'name',
'type' => Database::VAR_STRING,
'signed' => true,
'size' => 128,
'format' => '',
'filters' => [],
'required' => true,
'array' => false,
],
[
'label' => 'Adapter',
'$id' => 'adapter',
'type' => Database::VAR_STRING,
'signed' => true,
'size' => 64,
'format' => '',
'filters' => [],
'required' => true,
'array' => false,
],
[
'$id' => 'adapterCredentials',
'type' => Database::VAR_STRING,
'signed' => true,
'size' => 16384,
'format' => '',
'required' => false,
'array' => false,
'filters' => ['json']
],
[
'$id' => 'maximumFileSize',
'type' => Database::VAR_INTEGER,
'signed' => true,
'size' => 0,
'format' => '',
'filters' => [],
'required' => true,
'array' => false,
],
[
'$id' => 'allowedFileExtensions',
'type' => Database::VAR_STRING,
'signed' => true,
'size' => 64,
'format' => '',
'filters' => [],
'required' => true,
'array' => true,
],
[
'label' => 'Encryption',
'$id' => 'encryption',
'type' => Database::VAR_BOOLEAN,
'signed' => true,
'size' => 0,
'format' => '',
'filters' => [],
'required' => true,
'array' => false,
],
[
'label' => 'Virus Scan',
'$id' => 'antiVirus',
'type' => Database::VAR_BOOLEAN,
'signed' => true,
'size' => 0,
'format' => '',
'filters' => [],
'required' => true,
'array' => false,
],
],
'indexes' => [
[
'$id' => '_fulltext_name',
'type' => Database::INDEX_FULLTEXT,
'attributes' => ['name'],
'lengths' => [1024],
'orders' => [Database::ORDER_ASC],
]
]
]
];
/*

View file

@ -26,7 +26,6 @@ use Utopia\Validator\Integer;
use Appwrite\Database\Exception\Authorization as AuthorizationException;
use Appwrite\Database\Exception\Structure as StructureException;
use Utopia\Database\Query;
use Utopia\Validator\Numeric;
App::post('/v1/storage/buckets')
->desc('Create storage bucket')
@ -50,20 +49,21 @@ App::post('/v1/storage/buckets')
->param('read', null, new ArrayList(new Text(64)), 'An array of strings with read permissions. By default only the current user is granted with read permissions. [learn more about permissions](/docs/permissions) and get a full list of available permissions.', true)
->param('write', null, new ArrayList(new Text(64)), 'An array of strings with write permissions. By default only the current user is granted with write permissions. [learn more about permissions](/docs/permissions) and get a full list of available permissions.', true)
->inject('response')
->inject('projectDB')
->inject('dbForInternal')
->inject('user')
->inject('audits')
->action(function ($name, $maximumFileSize, $allowedFileExtensions, $enabled, $adapter, $encryption, $antiVirus, $read, $write, $response, $projectDB, $user, $audits) {
->action(function ($name, $maximumFileSize, $allowedFileExtensions, $enabled, $adapter, $encryption, $antiVirus, $read, $write, $response, $dbForInternal, $user, $audits) {
/** @var Utopia\Swoole\Request $request */
/** @var Appwrite\Utopia\Response $response */
/** @var Appwrite\Database\Database $projectDB */
/** @var Utopia\Database\Database $dbForInternal */
/** @var Appwrite\Database\Document $user */
/** @var Appwrite\Event\Event $audits */
try {
$data = [
'$collection' => Database::SYSTEM_COLLECTION_BUCKETS,
'$collection' => 'buckets',
'dateCreated' => \time(),
'dateUpdated' => \time(),
'name' => $name,
'maximumFileSize' => $maximumFileSize,
'allowedFileExtensions' => $allowedFileExtensions,
@ -72,30 +72,26 @@ App::post('/v1/storage/buckets')
'encryption' => $encryption,
'antiVirus' => $antiVirus,
];
$data['$permissions'] = [
'read' => (is_null($read) && !$user->isEmpty()) ? ['user:'.$user->getId()] : $read ?? [], // By default set read permissions for user
'write' => (is_null($write) && !$user->isEmpty()) ? ['user:'.$user->getId()] : $write ?? [], // By default set write permissions for user
];
$data = $projectDB->createDocument($data);
$data['$read'] = (is_null($read) && !$user->isEmpty()) ? ['user:' . $user->getId()] : $read ?? []; // By default set read permissions for user
$data['$write'] = (is_null($write) && !$user->isEmpty()) ? ['user:' . $user->getId()] : $write ?? []; // By default set write permissions for user
$data = $dbForInternal->createDocument('buckets', new Document($data));
} catch (AuthorizationException $exception) {
throw new Exception('Unauthorized permissions', 401);
} catch (StructureException $exception) {
throw new Exception('Bad structure. '.$exception->getMessage(), 400);
} catch (\Exception $exception) {
throw new Exception('Bad structure. ' . $exception->getMessage(), 400);
} catch (\Exception$exception) {
throw new Exception('Failed saving document to DB', 500);
}
$audits
->setParam('event', 'database.collections.create')
->setParam('resource', 'database/collection/'.$data->getId())
->setParam('resource', 'database/collection/' . $data->getId())
->setParam('data', $data->getArrayCopy())
;
$response
->setStatusCode(Response::STATUS_CODE_CREATED)
->dynamic($data, Response::MODEL_BUCKET)
;
$response->setStatusCode(Response::STATUS_CODE_CREATED);
$response->dynamic2($data, Response::MODEL_BUCKET);
});
App::post('/v1/storage/files')

6
composer.lock generated
View file

@ -1704,7 +1704,7 @@
"source": {
"type": "git",
"url": "https://github.com/lohanidamodar/audit",
"reference": "b3ca9fa928fdec8c966596cbd85ee1141c79b6eb"
"reference": "d5591321161b81043c45be3c53ce9dcfa2d81d72"
},
"require": {
"ext-pdo": "*",
@ -1738,7 +1738,7 @@
"upf",
"utopia"
],
"time": "2021-06-13T07:41:15+00:00"
"time": "2021-06-14T07:38:18+00:00"
},
{
"name": "utopia-php/cache",
@ -6195,5 +6195,5 @@
"platform-overrides": {
"php": "8.0"
},
"plugin-api-version": "2.0.0"
"plugin-api-version": "2.1.0"
}