Skip to content

Commit

Permalink
Added array keys for space and file listing
Browse files Browse the repository at this point in the history
  • Loading branch information
David Wakelin committed Aug 16, 2021
1 parent f9b4900 commit cfea196
Show file tree
Hide file tree
Showing 11 changed files with 41 additions and 27 deletions.
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,32 @@ 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();
```

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)
Expand Down
1 change: 1 addition & 0 deletions SpacesAPI/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 7 additions & 7 deletions SpacesAPI/Space.php
Original file line number Diff line number Diff line change
Expand Up @@ -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"] != "") {
Expand Down
2 changes: 1 addition & 1 deletion SpacesAPI/Spaces.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -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": [
Expand Down
14 changes: 7 additions & 7 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions docs/Space.md
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,7 @@ List all files in the space (recursively)

`array`

> An array of `\SpacesAPI\File` instances indexed by the file name
<hr />

Expand Down
2 changes: 1 addition & 1 deletion docs/Spaces.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

<hr />
Expand Down
4 changes: 2 additions & 2 deletions tests/FileTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
}

Expand All @@ -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()
Expand Down
10 changes: 7 additions & 3 deletions tests/SpaceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
}

Expand All @@ -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()
Expand Down Expand Up @@ -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();
Expand Down
8 changes: 4 additions & 4 deletions tests/SpacesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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;
}
}
Expand Down

0 comments on commit cfea196

Please sign in to comment.