From cfea1967ab7ea0d8021e7e8138303957a9f32387 Mon Sep 17 00:00:00 2001 From: David Wakelin Date: Mon, 16 Aug 2021 16:00:08 +0100 Subject: [PATCH] Added array keys for space and file listing --- README.md | 10 +++++++++- SpacesAPI/File.php | 1 + SpacesAPI/Space.php | 14 +++++++------- SpacesAPI/Spaces.php | 2 +- composer.json | 2 +- composer.lock | 14 +++++++------- docs/Space.md | 1 + docs/Spaces.md | 2 +- tests/FileTest.php | 4 ++-- tests/SpaceTest.php | 10 +++++++--- tests/SpacesTest.php | 8 ++++---- 11 files changed, 41 insertions(+), 27 deletions(-) diff --git a/README.md b/README.md index e53c7f8..588a067 100644 --- a/README.md +++ b/README.md @@ -15,16 +15,24 @@ Obtain API keys from the [Digital Ocean Applications & API dashboard](https://cl ```php use SpacesAPI\Spaces; +// Connect to a space $spaces = new Spaces('api-key', 'api-secret'); $space = $spaces->space('space-name'); +// Download a file $file = $space->file('remote-file-1.txt'); $file->download('local/file/path/file.txt'); +// Upload text to a file $file2 = $space->uploadText("Lorem ipsum","remote-file-2.txt"); + +// Get a signed public link, valid for 2 hours $file2url = $file2->getSignedURL("2 hours"); +// Make a copy $file3 = $file2->copy('remote-file-3.txt'); + +// Make a file public and get the URL $file3->makePublic(); $file3url = $file3->getURL(); ``` @@ -32,7 +40,7 @@ $file3url = $file3->getURL(); See more examples in [docs/Examples.md](docs/Examples.md) ## Upgrading? -Version 3 has many changes over verison 2, so we have written a [migration guide](docs/Upgrade2-3.md) +Version 3 has many changes over version 2, so we have written a [migration guide](docs/Upgrade2-3.md) ## API reference * [\SpacesAPI\Spaces](docs/Spaces.md) diff --git a/SpacesAPI/File.php b/SpacesAPI/File.php index 82719d2..f824b8a 100644 --- a/SpacesAPI/File.php +++ b/SpacesAPI/File.php @@ -10,6 +10,7 @@ * You wouldn't normally instantiate this class directly, * Rather obtain an instance from `\SpacesAPI\Space::list()`, `\SpacesAPI\Spaces::file()`, `\SpacesAPI\Spaces::uploadText()` or `\SpacesAPI\Spaces::uploadFile()` * + * @property string $filename * @property string $expiration * @property string $e_tag * @property int $last_modified diff --git a/SpacesAPI/Space.php b/SpacesAPI/Space.php index 1ac856f..0b55f8a 100644 --- a/SpacesAPI/Space.php +++ b/SpacesAPI/Space.php @@ -252,16 +252,16 @@ public function listFiles(string $directory = "", ?string $continuationToken = n ]) ); - if (!isset($data['Contents'])) { - return ['files' => []]; - } - $files = [ - 'files' => $data['Contents'], + 'files' => [], ]; - foreach ($files['files'] as $index => $fileInfo) { - $files['files'][$index] = new File($this, $fileInfo['Key'], $fileInfo); + if (!isset($data['Contents'])) { + return $files; + } + + foreach ($data['Contents'] as $fileInfo) { + $files['files'][$fileInfo['Key']] = new File($this, $fileInfo['Key'], $fileInfo); } if (isset($data["NextContinuationToken"]) && $data["NextContinuationToken"] != "") { diff --git a/SpacesAPI/Spaces.php b/SpacesAPI/Spaces.php index 928ee2d..3f90470 100644 --- a/SpacesAPI/Spaces.php +++ b/SpacesAPI/Spaces.php @@ -61,7 +61,7 @@ public function list(): array $spaces = []; foreach (Result::parse($this->s3->listBuckets()['Buckets']) as $bucket) { - $spaces[] = new Space($this->s3, $bucket['Name']); + $spaces[$bucket['Name']] = new Space($this->s3, $bucket['Name']); } return $spaces; diff --git a/composer.json b/composer.json index 087a7ba..715d5ff 100644 --- a/composer.json +++ b/composer.json @@ -1,7 +1,7 @@ { "name": "sociallydev/spaces-api", "description": "Library for accessing Digital Ocean spaces", - "version":"3.0.0", + "version":"3.1.0", "type": "library", "license": "MIT", "authors": [ diff --git a/composer.lock b/composer.lock index 3c21eb8..f42afa8 100644 --- a/composer.lock +++ b/composer.lock @@ -4,20 +4,20 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "c76b703f4b3b49f8421bcb693ece4aa8", + "content-hash": "77328f11e2ce84c590e531213fe931c6", "packages": [ { "name": "aws/aws-sdk-php", - "version": "3.187.2", + "version": "3.190.2", "source": { "type": "git", "url": "https://github.com/aws/aws-sdk-php.git", - "reference": "0ec4ae120cfae758efa3c283dc56eb20602f094c" + "reference": "8ca6a5f9834de3eb3fb84b28fc12c9088bc01293" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/aws/aws-sdk-php/zipball/0ec4ae120cfae758efa3c283dc56eb20602f094c", - "reference": "0ec4ae120cfae758efa3c283dc56eb20602f094c", + "url": "https://api.github.com/repos/aws/aws-sdk-php/zipball/8ca6a5f9834de3eb3fb84b28fc12c9088bc01293", + "reference": "8ca6a5f9834de3eb3fb84b28fc12c9088bc01293", "shasum": "" }, "require": { @@ -92,9 +92,9 @@ "support": { "forum": "https://forums.aws.amazon.com/forum.jspa?forumID=80", "issues": "https://github.com/aws/aws-sdk-php/issues", - "source": "https://github.com/aws/aws-sdk-php/tree/3.187.2" + "source": "https://github.com/aws/aws-sdk-php/tree/3.190.2" }, - "time": "2021-08-04T18:12:21+00:00" + "time": "2021-08-13T18:12:28+00:00" }, { "name": "guzzlehttp/guzzle", diff --git a/docs/Space.md b/docs/Space.md index 9d4e6fe..ef23140 100644 --- a/docs/Space.md +++ b/docs/Space.md @@ -332,6 +332,7 @@ List all files in the space (recursively) `array` +> An array of `\SpacesAPI\File` instances indexed by the file name
diff --git a/docs/Spaces.md b/docs/Spaces.md index cd84a8a..8e8f2f5 100644 --- a/docs/Spaces.md +++ b/docs/Spaces.md @@ -116,7 +116,7 @@ List all your spaces `array` -> An array of \SpacesAPI\Space instances +> An array of `\SpacesAPI\Space` instances indexed by the space name
diff --git a/tests/FileTest.php b/tests/FileTest.php index 4c4e3e2..bf7516f 100644 --- a/tests/FileTest.php +++ b/tests/FileTest.php @@ -20,7 +20,7 @@ public static function setUpBeforeClass(): void $spaces = new Spaces($_ENV['SPACES_KEY'], $_ENV['SPACES_SECRET']); try { - $spaces->space('spaces-api-test')->destroySpace(); + $spaces->space('spaces-api-test')->destroy(); } catch (SpaceDoesntExistException $e) { } @@ -30,7 +30,7 @@ public static function setUpBeforeClass(): void public static function tearDownAfterClass(): void { - (new Spaces($_ENV['SPACES_KEY'], $_ENV['SPACES_SECRET']))->space('spaces-api-test')->destroySpace(); + (new Spaces($_ENV['SPACES_KEY'], $_ENV['SPACES_SECRET']))->space('spaces-api-test')->destroy(); } public function testCanUpdatePrivacy() diff --git a/tests/SpaceTest.php b/tests/SpaceTest.php index ad09335..6c1177f 100644 --- a/tests/SpaceTest.php +++ b/tests/SpaceTest.php @@ -20,7 +20,7 @@ public static function setUpBeforeClass(): void $spaces = new Spaces($_ENV['SPACES_KEY'], $_ENV['SPACES_SECRET']); try { - $spaces->space('spaces-api-test')->destroySpace(); + $spaces->space('spaces-api-test')->destroy(); } catch (SpaceDoesntExistException $e) { } @@ -29,7 +29,7 @@ public static function setUpBeforeClass(): void public static function tearDownAfterClass(): void { -// (new Spaces($_ENV['SPACES_KEY'], $_ENV['SPACES_SECRET']))->space('spaces-api-test')->destroySpace(); +// (new Spaces($_ENV['SPACES_KEY'], $_ENV['SPACES_SECRET']))->space('spaces-api-test')->destroy(); } public function testCanUpdateSpacePrivacy() @@ -102,7 +102,11 @@ public function testCanListFiles() $files = self::$space->listFiles()['files']; $this->assertIsArray($files); $this->assertCount(2, $files); - $this->assertInstanceOf(File::class, $files[0]); + $this->assertInstanceOf(File::class, $files[array_key_first($files)]); + + foreach ($files as $filename => $file) { + $this->assertEquals($file->filename, $filename); + } foreach ($files as $file) { $file->delete(); diff --git a/tests/SpacesTest.php b/tests/SpacesTest.php index e987e3f..765d85f 100644 --- a/tests/SpacesTest.php +++ b/tests/SpacesTest.php @@ -17,14 +17,14 @@ public static function setUpBeforeClass(): void $dotenv->required(['SPACES_KEY', 'SPACES_SECRET']); try { - (new Spaces($_ENV['SPACES_KEY'], $_ENV['SPACES_SECRET']))->space('spaces-api-test')->destroySpace(); + (new Spaces($_ENV['SPACES_KEY'], $_ENV['SPACES_SECRET']))->space('spaces-api-test')->destroy(); } catch (SpaceDoesntExistException $e) { } } public static function tearDownAfterClass(): void { - (new Spaces($_ENV['SPACES_KEY'], $_ENV['SPACES_SECRET']))->space('spaces-api-test')->destroySpace(); + (new Spaces($_ENV['SPACES_KEY'], $_ENV['SPACES_SECRET']))->space('spaces-api-test')->destroy(); } public function testAuthenticationCanFail() @@ -68,8 +68,8 @@ public function testCanListSpaces(Spaces $spaces) $this->assertIsArray($list); $spaceFound = false; - foreach ($list as $space) { - if ($space->getName() == 'spaces-api-test') { + foreach ($list as $name => $space) { + if ($name == 'spaces-api-test' && $space->getName() == 'spaces-api-test') { $spaceFound = true; } }