diff --git a/app/controllers/api/storage.php b/app/controllers/api/storage.php index 63c687c856..6c94542408 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([ @@ -244,7 +255,7 @@ App::post('/v1/storage/buckets') '$collection' => 'buckets', 'dateCreated' => \time(), 'dateUpdated' => \time(), - 'name' => $name, + 'name' => $name, 'permission' => $permission, 'maximumFileSize' => $maximumFileSize, 'allowedFileExtensions' => $allowedFileExtensions, @@ -252,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) { @@ -286,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); }); @@ -654,7 +666,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 */ @@ -671,11 +683,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); } } @@ -691,7 +703,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); }); 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']);