From 69390029323177dd65c9f7a9c091e4dc9dc1f52a Mon Sep 17 00:00:00 2001 From: Damodar Lohani Date: Tue, 15 Jun 2021 14:19:49 +0545 Subject: [PATCH] create bucket fixing with new db --- app/config/collections2.php | 125 ++++++++++++++++++++++++++++++++ app/controllers/api/storage.php | 32 ++++---- composer.lock | 6 +- 3 files changed, 142 insertions(+), 21 deletions(-) diff --git a/app/config/collections2.php b/app/config/collections2.php index b130092886..e485bbd444 100644 --- a/app/config/collections2.php +++ b/app/config/collections2.php @@ -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], + ] + ] + ] ]; /* diff --git a/app/controllers/api/storage.php b/app/controllers/api/storage.php index 7fa14f8d3b..935d76e05d 100644 --- a/app/controllers/api/storage.php +++ b/app/controllers/api/storage.php @@ -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') diff --git a/composer.lock b/composer.lock index 20e5ea98ae..9d12880f2c 100644 --- a/composer.lock +++ b/composer.lock @@ -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" }