From 68361364b3d3b5a3ecd933390d0c26d04a5f8633 Mon Sep 17 00:00:00 2001 From: Damodar Lohani Date: Sun, 17 Oct 2021 12:29:35 +0545 Subject: [PATCH 1/4] file search attribute --- app/controllers/api/storage.php | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/app/controllers/api/storage.php b/app/controllers/api/storage.php index 63c687c856..45f2f85c89 100644 --- a/app/controllers/api/storage.php +++ b/app/controllers/api/storage.php @@ -222,6 +222,17 @@ App::post('/v1/storage/buckets') 'array' => false, 'filters' => [], ]), + new Document([ + '$id' => 'search', + 'type' => Database::VAR_STRING, + 'format' => '', + 'size' => 16384, + 'signed' => true, + 'required' => false, + 'default' => null, + 'array' => false, + 'filters' => [], + ]), ], [ new Document([ '$id' => '_key_bucket', @@ -231,12 +242,12 @@ App::post('/v1/storage/buckets') 'orders' => [Database::ORDER_ASC], ]), new Document([ - '$id' => '_fulltext_name', + '$id' => '_key_search', 'type' => Database::INDEX_FULLTEXT, - 'attributes' => ['name'], - 'lengths' => [1024], + 'attributes' => ['search'], + 'lengths' => [2048], 'orders' => [Database::ORDER_ASC], - ]), + ],), ]); $bucket = $dbForInternal->createDocument('buckets', new Document([ From fdfb3b9493f530b17760ecb2874c3e9e46fee2ba Mon Sep 17 00:00:00 2001 From: Damodar Lohani Date: Sun, 17 Oct 2021 12:35:08 +0545 Subject: [PATCH 2/4] fix list files --- app/controllers/api/storage.php | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/app/controllers/api/storage.php b/app/controllers/api/storage.php index 45f2f85c89..25c33f8c81 100644 --- a/app/controllers/api/storage.php +++ b/app/controllers/api/storage.php @@ -665,7 +665,7 @@ App::get('/v1/storage/buckets/:bucketId/files') ->inject('response') ->inject('dbForInternal') ->inject('usage') - ->action(function ($bucketId, $search, $limit, $offset, $after, $orderType, $response, $dbForInternal, $usage) { + ->action(function ($bucketId, $search, $limit, $offset, $cursor, $cursorDirection, $orderType, $response, $dbForInternal, $usage) { /** @var Appwrite\Utopia\Response $response */ /** @var Utopia\Database\Database $dbForInternal */ /** @var Appwrite\Stats\Stats $usage */ @@ -682,11 +682,11 @@ App::get('/v1/storage/buckets/:bucketId/files') $queries[] = [new Query('name', Query::TYPE_SEARCH, [$search])]; } - if (!empty($after)) { - $afterFile = $dbForInternal->getDocument('bucket_' . $bucketId, $after); + if (!empty($cursor)) { + $cursorFile = $dbForInternal->getDocument('bucket_' . $bucketId, $cursor); - if ($afterFile->isEmpty()) { - throw new Exception("File '{$after}' for the 'after' value not found.", 400); + if ($cursorFile->isEmpty()) { + throw new Exception("File '{$cursor}' for the 'cursor' value not found.", 400); } } @@ -702,7 +702,7 @@ App::get('/v1/storage/buckets/:bucketId/files') ; $response->dynamic(new Document([ - 'files' => $dbForInternal->find('bucket_' . $bucketId, $queries, $limit, $offset, [], [$orderType], $afterFile ?? null), + 'files' => $dbForInternal->find('bucket_' . $bucketId, $queries, $limit, $offset, [], [$orderType], $cursorFile ?? null, $cursorDirection), 'sum' => $dbForInternal->count('bucket_' . $bucketId, $queries, APP_LIMIT_COUNT), ]), Response::MODEL_FILE_LIST); }); From 89d9feaa995d03a781370e2a2d4e603edc4429a4 Mon Sep 17 00:00:00 2001 From: Damodar Lohani Date: Sun, 17 Oct 2021 12:39:34 +0545 Subject: [PATCH 3/4] fir list storage after pagination --- app/controllers/api/storage.php | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/app/controllers/api/storage.php b/app/controllers/api/storage.php index 25c33f8c81..4b52116f9c 100644 --- a/app/controllers/api/storage.php +++ b/app/controllers/api/storage.php @@ -297,30 +297,31 @@ App::get('/v1/storage/buckets') ->param('search', '', new Text(256), 'Search term to filter your list results. Max length: 256 chars.', true) ->param('limit', 25, new Range(0, 100), 'Results limit value. By default will return maximum 25 results. Maximum of 100 results allowed per request.', true) ->param('offset', 0, new Range(0, 2000), 'Results offset. The default value is 0. Use this param to manage pagination.', true) - ->param('after', '', new UID(), 'ID of the bucket used as the starting point for the query, excluding the bucket itself. Should be used for efficient pagination when working with large sets of data.', true) + ->param('cursor', '', new UID(), 'ID of the bucket used as the starting point for the query, excluding the bucket itself. Should be used for efficient pagination when working with large sets of data.', true) + ->param('cursorDirection', Database::CURSOR_AFTER, new WhiteList([Database::CURSOR_AFTER, Database::CURSOR_BEFORE]), 'Direction of the cursor.', true) ->param('orderType', 'ASC', new WhiteList(['ASC', 'DESC'], true), 'Order result by ASC or DESC order.', true) ->inject('response') ->inject('dbForInternal') ->inject('usage') - ->action(function ($search, $limit, $offset, $after, $orderType, $response, $dbForInternal, $usage) { + ->action(function ($search, $limit, $offset, $cursor, $cursorDirection, $orderType, $response, $dbForInternal, $usage) { /** @var Appwrite\Utopia\Response $response */ /** @var Utopia\Database\Database $dbForInternal */ /** @var Appwrite\Stats\Stats $usage */ $queries = ($search) ? [new Query('name', Query::TYPE_SEARCH, $search)] : []; - if (!empty($after)) { - $afterBucket = $dbForInternal->getDocument('buckets', $after); + if (!empty($cursor)) { + $cursorBucket = $dbForInternal->getDocument('buckets', $cursor); - if ($afterBucket->isEmpty()) { - throw new Exception("Bucket '{$after}' for the 'after' value not found.", 400); + if ($cursorBucket->isEmpty()) { + throw new Exception("Bucket '{$cursor}' for the 'cursor' value not found.", 400); } } $usage->setParam('storage.buckets.read', 1); $response->dynamic(new Document([ - 'buckets' => $dbForInternal->find('buckets', $queries, $limit, $offset, [], [$orderType], $afterBucket ?? null), + 'buckets' => $dbForInternal->find('buckets', $queries, $limit, $offset, [], [$orderType], $cursorBucket ?? null, $cursorDirection), 'sum' => $dbForInternal->count('buckets', $queries, APP_LIMIT_COUNT), ]), Response::MODEL_BUCKET_LIST); }); From cecc86371750b64446df1b836c7996435c5dae7b Mon Sep 17 00:00:00 2001 From: Damodar Lohani Date: Sun, 17 Oct 2021 12:57:59 +0545 Subject: [PATCH 4/4] fix storage tests --- app/controllers/api/storage.php | 6 +++--- .../Storage/StorageConsoleClientTest.php | 1 + .../Services/Storage/StorageCustomClientTest.php | 16 +++++++++++++++- .../Services/Storage/StorageCustomServerTest.php | 2 +- 4 files changed, 20 insertions(+), 5 deletions(-) diff --git a/app/controllers/api/storage.php b/app/controllers/api/storage.php index 4b52116f9c..6c94542408 100644 --- a/app/controllers/api/storage.php +++ b/app/controllers/api/storage.php @@ -255,7 +255,7 @@ App::post('/v1/storage/buckets') '$collection' => 'buckets', 'dateCreated' => \time(), 'dateUpdated' => \time(), - 'name' => $name, + 'name' => $name, 'permission' => $permission, 'maximumFileSize' => $maximumFileSize, 'allowedFileExtensions' => $allowedFileExtensions, @@ -263,8 +263,8 @@ App::post('/v1/storage/buckets') 'adapter' => $adapter, 'encryption' => $encryption, 'antiVirus' => $antiVirus, - '$read' => $read, - '$write' => $write, + '$read' => $read ?? [], + '$write' => $write ?? [], 'search' => implode(' ', [$bucketId, $name]), ])); } catch (Duplicate $th) { diff --git a/tests/e2e/Services/Storage/StorageConsoleClientTest.php b/tests/e2e/Services/Storage/StorageConsoleClientTest.php index dd40ed1572..8c61417113 100644 --- a/tests/e2e/Services/Storage/StorageConsoleClientTest.php +++ b/tests/e2e/Services/Storage/StorageConsoleClientTest.php @@ -53,6 +53,7 @@ class StorageConsoleClientTest extends Scope ], $this->getHeaders()), [ 'bucketId' => 'unique()', 'name' => 'Test Bucket', + 'permission' => 'file' ]); $this->assertEquals(201, $bucket['headers']['status-code']); $bucketId = $bucket['body']['$id']; diff --git a/tests/e2e/Services/Storage/StorageCustomClientTest.php b/tests/e2e/Services/Storage/StorageCustomClientTest.php index 2a0ead59f6..7d5c49cbdd 100644 --- a/tests/e2e/Services/Storage/StorageCustomClientTest.php +++ b/tests/e2e/Services/Storage/StorageCustomClientTest.php @@ -19,7 +19,21 @@ class StorageCustomClientTest extends Scope /** * Test for SUCCESS */ - $file = $this->client->call(Client::METHOD_POST, '/storage/files', array_merge([ + $bucket = $this->client->call(Client::METHOD_POST, '/storage/buckets', array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + 'x-appwrite-key' => $this->getProject()['apiKey'], + ], $this->getHeaders()), [ + 'bucketId' => 'unique()', + 'name' => 'Test Bucket', + 'permission' => 'file', + 'read' => ['role:all'], + 'write' => ['role:all'], + ]); + $this->assertEquals(201, $bucket['headers']['status-code']); + $this->assertNotEmpty($bucket['body']['$id']); + + $file = $this->client->call(Client::METHOD_POST, '/storage/buckets/'. $bucket['body']['$id'] . '/files', array_merge([ 'content-type' => 'multipart/form-data', 'x-appwrite-project' => $this->getProject()['$id'], ], $this->getHeaders()), [ diff --git a/tests/e2e/Services/Storage/StorageCustomServerTest.php b/tests/e2e/Services/Storage/StorageCustomServerTest.php index 72d1f2dd0e..fa8f9ba50e 100644 --- a/tests/e2e/Services/Storage/StorageCustomServerTest.php +++ b/tests/e2e/Services/Storage/StorageCustomServerTest.php @@ -92,7 +92,7 @@ class StorageCustomServerTest extends Scope 'content-type' => 'application/json', 'x-appwrite-project' => $this->getProject()['$id'], ], $this->getHeaders()), [ - 'after' => $response['body']['buckets'][0]['$id'] + 'cursor' => $response['body']['buckets'][0]['$id'] ]); $this->assertEquals(200, $response['headers']['status-code']);