Compare commits

...

23 commits

Author SHA1 Message Date
David Wakelin
6b0f8f5e03 Added deprecation notice 2023-12-15 11:07:08 +00:00
David Wakelin
8ac092ea96 Version bump from bugfix 2023-12-15 11:06:58 +00:00
Aleksandar Kolakov
4f4b6148d2 Fixed hardcoded region in Spaces constructor 2023-12-15 10:30:35 +00:00
David Wakelin
6bc100cf90 Added move example to readme 2023-02-01 11:19:59 +00:00
David Wakelin
c52d22a70e Allowed passing validation flag to Space::file() 2022-11-11 09:55:47 +00:00
dependabot[bot]
3a32727c28 Bump guzzlehttp/guzzle from 7.4.3 to 7.4.5
Bumps [guzzlehttp/guzzle](https://github.com/guzzle/guzzle) from 7.4.3 to 7.4.5.
- [Release notes](https://github.com/guzzle/guzzle/releases)
- [Changelog](https://github.com/guzzle/guzzle/blob/master/CHANGELOG.md)
- [Commits](https://github.com/guzzle/guzzle/compare/7.4.3...7.4.5)

---
updated-dependencies:
- dependency-name: guzzlehttp/guzzle
  dependency-type: indirect
...

Signed-off-by: dependabot[bot] <support@github.com>
2022-06-28 22:02:32 +01:00
dependabot[bot]
e9cdacc684 Bump guzzlehttp/guzzle from 7.4.2 to 7.4.3
Bumps [guzzlehttp/guzzle](https://github.com/guzzle/guzzle) from 7.4.2 to 7.4.3.
- [Release notes](https://github.com/guzzle/guzzle/releases)
- [Changelog](https://github.com/guzzle/guzzle/blob/master/CHANGELOG.md)
- [Commits](https://github.com/guzzle/guzzle/compare/7.4.2...7.4.3)

---
updated-dependencies:
- dependency-name: guzzlehttp/guzzle
  dependency-type: indirect
...

Signed-off-by: dependabot[bot] <support@github.com>
2022-05-26 07:30:22 +01:00
David Wakelin
26d6b83356 Updated version numbers in github bug report template 2022-03-30 11:27:01 +01:00
David Wakelin
6d365b0f6a Bumped dependencies and released new minor version 3.5.0 up from 3.4.0 2022-03-30 11:25:19 +01:00
David Wakelin
de48ddb481 Added extra test assertion for Space::uploadFile mimeType argument
Added extra parameters for `Space::uploadFile` to docs
2022-03-30 11:21:42 +01:00
David Wakelin
f1adeb7c07
Merge pull request #74 from juhchamp/master
Add `mimetype` and `privacy` option to `uploadFile` method of `Space`
2022-03-30 11:13:53 +01:00
Juh Champ
dc5dc27ff3 uploadFile method of Space class updated and testCanUploadFileWithMimeType method added to SpaceTest. 2022-03-23 16:58:37 -03:00
Juh Champ
b7d1a40e70 The uploadFile method of Space class updated 2022-03-05 01:52:04 -03:00
David Wakelin
d7f0b36dc1 Moved files now retain the current Object 2022-02-09 09:01:21 +00:00
David Wakelin
a2bb0f4129 Minor version bump 2022-02-08 18:17:19 +00:00
David Wakelin
591d0be758 Public perms on file copy/move will now retain the current value 2022-02-08 18:16:12 +00:00
David Wakelin
6b438e0b9a Added a file move method 2022-02-08 18:13:17 +00:00
David Wakelin
bdd06aa0b7 Updated tests to use random (md5 of unixtime) space names 2022-02-08 18:07:13 +00:00
David Wakelin
c0b1a43141 Dependency updates (minor versions only) 2022-02-08 17:26:51 +00:00
David Wakelin
7c915822bf Added privacy setting to text upload 2021-10-04 09:35:42 +01:00
David Wakelin
8ff8498a22 Version bump 2021-08-17 12:43:20 +01:00
David Wakelin
aa5450538b Made file and space existence validation optional 2021-08-17 12:43:07 +01:00
David Wakelin
de7d7c7722 Doc corrections 2021-08-16 16:16:01 +01:00
13 changed files with 756 additions and 437 deletions

View file

@ -16,10 +16,14 @@ body:
id: version id: version
attributes: attributes:
label: Version label: Version
description: What version of Spaces-API is this occuring on? description: What version of Spaces-API is this occuring on? Versions below 3 will not be fixed. Please upgrade.
options: options:
- 2.0.1 - 3.5.0
- 2.0.0 - 3.4.0
- 3.3.0
- 3.2.0
- 3.1.0
- 3.0.0
validations: validations:
required: true required: true
- type: input - type: input

View file

@ -1,6 +1,7 @@
![Spaces-API](https://imgur.com/NYNsQyl.png "Devang Srivastava's Spaces-API") # This library is deprecated
We recommend using the [official SDK](https://github.com/DigitalOceanPHP/Client) or the [Laravel package](https://github.com/GrahamCampbell/Laravel-DigitalOcean)
PHP library for accessing Digital Ocean spaces All issues will be closed and new PRs will not be accepted
## Installation ## Installation
Install via composer Install via composer
@ -32,6 +33,9 @@ $file2url = $file2->getSignedURL("2 hours");
// Make a copy // Make a copy
$file3 = $file2->copy('remote-file-3.txt'); $file3 = $file2->copy('remote-file-3.txt');
// Move or rename a file
$file2->move('new-filename.txt')
// Make a file public and get the URL // Make a file public and get the URL
$file3->makePublic(); $file3->makePublic();
$file3url = $file3->getURL(); $file3url = $file3->getURL();

View file

@ -4,6 +4,8 @@ namespace SpacesAPI;
use SpacesAPI\Exceptions\FileDoesntExistException; use SpacesAPI\Exceptions\FileDoesntExistException;
use function PHPUnit\Framework\isNull;
/** /**
* Represents a single file * Represents a single file
* *
@ -46,20 +48,21 @@ class File
private $_content_length; private $_content_length;
/** /**
* @param \SpacesAPI\Space $space * @param \SpacesAPI\Space $space An instance of `\SpacesAPI\Space`
* @param string $filename * @param string $filename The filename of a file
* @param array $info * @param array $info Any information already known about the file (eg content_length, content_type, etc). Default `[]`
* @param bool $validate Check that the file exists
* *
* @throws \SpacesAPI\Exceptions\FileDoesntExistException * @throws \SpacesAPI\Exceptions\FileDoesntExistException If validation is `true` and the file doesn't exist
*/ */
public function __construct(Space $space, string $filename, array $info = []) public function __construct(Space $space, string $filename, array $info = [], bool $validate = true)
{ {
$this->space = $space; $this->space = $space;
$this->space_name = $space->getName(); $this->space_name = $space->getName();
$this->s3 = $space->getS3Client(); $this->s3 = $space->getS3Client();
$this->_filename = $filename; $this->_filename = $filename;
if (!$this->s3->doesObjectExist($this->space_name, $filename)) { if ($validate && !$this->s3->doesObjectExist($this->space_name, $filename)) {
throw new FileDoesntExistException("File $filename doesn't exist"); throw new FileDoesntExistException("File $filename doesn't exist");
} }
@ -210,19 +213,34 @@ class File
* *
* @return \SpacesAPI\File * @return \SpacesAPI\File
*/ */
public function copy(string $newFilename, bool $public = false): File public function copy(string $newFilename): File
{ {
$this->s3->copy( $this->s3->copy(
$this->space_name, $this->space_name,
$this->_filename, $this->_filename,
$this->space_name, $this->space_name,
$newFilename, $newFilename,
($public) ? 'public-read' : 'private' ($this->isPublic()) ? 'public-read' : 'private'
); );
return new self($this->space, $newFilename); return new self($this->space, $newFilename);
} }
/**
* @param string $newFilename
*
* @return \SpacesAPI\File
*/
public function move(string $newFilename): File
{
$this->copy($newFilename);
$this->delete();
$this->_filename = $newFilename;
$this->fetchFileInfo();
return $this;
}
/** /**
* Get the public URL * Get the public URL
* This URL will not work if the file is private * This URL will not work if the file is private

View file

@ -36,15 +36,16 @@ class Space
* *
* @param \Aws\S3\S3Client $s3 An authenticated S3Client instance * @param \Aws\S3\S3Client $s3 An authenticated S3Client instance
* @param string $name Space name * @param string $name Space name
* @param bool $validate Check that the space exists
* *
* @throws \SpacesAPI\Exceptions\SpaceDoesntExistException * @throws \SpacesAPI\Exceptions\SpaceDoesntExistException If validation is `true` and the space doesn't exist
*/ */
public function __construct(S3Client $s3, string $name) public function __construct(S3Client $s3, string $name, bool $validate = true)
{ {
$this->s3 = $s3; $this->s3 = $s3;
$this->name = $name; $this->name = $name;
if (!$this->s3->doesBucketExist($name)) { if ($validate && !$this->s3->doesBucketExist($name)) {
throw new SpaceDoesntExistException("Space '$this->name' does not exist"); throw new SpaceDoesntExistException("Space '$this->name' does not exist");
} }
} }
@ -233,13 +234,29 @@ class Space
/** /**
* List all files in the space (recursively) * List all files in the space (recursively)
* *
* @param string $directory The directory to list files in. Empty string for root directory
*
* @return array
*/
public function listFiles(string $directory = ""): array
{
$rawFiles = $this->rawListFiles($directory);
$files = [];
foreach ($rawFiles as $fileInfo) {
$files[$fileInfo['Key']] = new File($this, $fileInfo['Key'], $fileInfo, false);
}
return ['files' => $files];
}
/**
* @param string $directory The directory to list files in. Empty string for root directory * @param string $directory The directory to list files in. Empty string for root directory
* @param string|null $continuationToken Used internally to work around request limits (1000 files per request) * @param string|null $continuationToken Used internally to work around request limits (1000 files per request)
* *
* @return array * @return array
* @throws \SpacesAPI\Exceptions\FileDoesntExistException
*/ */
public function listFiles(string $directory = "", ?string $continuationToken = null): array private function rawListFiles(string $directory = "", ?string $continuationToken = null): array
{ {
$data = Result::parse( $data = Result::parse(
$this->s3->listObjectsV2([ $this->s3->listObjectsV2([
@ -252,20 +269,14 @@ class Space
]) ])
); );
$files = [
'files' => [],
];
if (!isset($data['Contents'])) { if (!isset($data['Contents'])) {
return $files; return [];
} }
foreach ($data['Contents'] as $fileInfo) { $files = $data['Contents'];
$files['files'][$fileInfo['Key']] = new File($this, $fileInfo['Key'], $fileInfo);
}
if (isset($data["NextContinuationToken"]) && $data["NextContinuationToken"] != "") { if (isset($data["NextContinuationToken"]) && $data["NextContinuationToken"] != "") {
$files = array_merge($files['files'], $this->listFiles($directory, $data["NextContinuationToken"])['files']); $files = array_merge($files, $this->rawListFiles($directory, $data["NextContinuationToken"]));
} }
return $files; return $files;
@ -277,13 +288,14 @@ class Space
* @param string $text The text to upload * @param string $text The text to upload
* @param string $filename The filepath/name to save to * @param string $filename The filepath/name to save to
* @param array $params Any extra parameters. [See here](https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/S3.html#upload-property) * @param array $params Any extra parameters. [See here](https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/S3.html#upload-property)
* @param bool $private True for the file to be private, false to allow public access
* *
* @return \SpacesAPI\File * @return \SpacesAPI\File
*/ */
public function uploadText(string $text, string $filename, array $params = []): File public function uploadText(string $text, string $filename, array $params = [], bool $private = true): File
{ {
$this->s3->upload($this->name, $filename, $text, 'private', $params); $this->s3->upload($this->name, $filename, $text, ($private) ? 'private' : 'public-read', $params);
return new File($this, $filename); return new File($this, $filename, [], false);
} }
/** /**
@ -291,31 +303,36 @@ class Space
* *
* @param string $filepath The path to the file, including the filename. Relative and absolute paths are accepted. * @param string $filepath The path to the file, including the filename. Relative and absolute paths are accepted.
* @param string|null $filename The remote filename. If `null`, the local filename will be used. * @param string|null $filename The remote filename. If `null`, the local filename will be used.
* @param string|null $mimeType The file mime type to pass as ContentType for the file (e.g. 'image/jpeg').
* @param bool $private True for the file to be private, false to allow public access.
* *
* @return \SpacesAPI\File * @return \SpacesAPI\File
*/ */
public function uploadFile(string $filepath, ?string $filename = null): File public function uploadFile(string $filepath, ?string $filename = null, ?string $mimeType = null, bool $private = true): File
{ {
$this->s3->putObject([ $this->s3->putObject([
'Bucket' => $this->name, 'Bucket' => $this->name,
'Key' => ($filename) ?: basename($filepath), 'Key' => ($filename) ?: basename($filepath),
'SourceFile' => $filepath, 'SourceFile' => $filepath,
'ContentType' => $mimeType,
'ACL' => ($private) ? 'private' : 'public-read'
]); ]);
return new File($this, ($filename) ?: basename($filepath)); return new File($this, ($filename) ?: basename($filepath), [], false);
} }
/** /**
* Get an instance of \SpacesAPI\File for a given filename * Get an instance of \SpacesAPI\File for a given filename
* *
* @param string $filename * @param string $filename
* @package bool $validate
* *
* @return \SpacesAPI\File * @return \SpacesAPI\File
* @throws \SpacesAPI\Exceptions\FileDoesntExistException Thrown if the file doesn't exist * @throws \SpacesAPI\Exceptions\FileDoesntExistException Thrown if the file doesn't exist
*/ */
public function file(string $filename): File public function file(string $filename, bool $validate = true): File
{ {
return new File($this, $filename); return new File($this, $filename, [], $validate);
} }
/** /**

View file

@ -36,7 +36,7 @@ class Spaces
{ {
$this->s3 = new S3Client([ $this->s3 = new S3Client([
"version" => "latest", "version" => "latest",
"region" => "us-east-1", "region" => $region,
"endpoint" => "https://$region.$host", "endpoint" => "https://$region.$host",
"credentials" => ["key" => $accessKey, "secret" => $secretKey], "credentials" => ["key" => $accessKey, "secret" => $secretKey],
"ua_append" => "SociallyDev-Spaces-API/2", "ua_append" => "SociallyDev-Spaces-API/2",
@ -61,7 +61,7 @@ class Spaces
$spaces = []; $spaces = [];
foreach (Result::parse($this->s3->listBuckets()['Buckets']) as $bucket) { foreach (Result::parse($this->s3->listBuckets()['Buckets']) as $bucket) {
$spaces[$bucket['Name']] = new Space($this->s3, $bucket['Name']); $spaces[$bucket['Name']] = new Space($this->s3, $bucket['Name'], false);
} }
return $spaces; return $spaces;
@ -87,7 +87,7 @@ class Spaces
throw new SpaceExistsException($e->getAwsErrorMessage()); throw new SpaceExistsException($e->getAwsErrorMessage());
} }
return new Space($this->s3, $name); return new Space($this->s3, $name, false);
} }
/** /**

View file

@ -1,7 +1,7 @@
{ {
"name": "sociallydev/spaces-api", "name": "sociallydev/spaces-api",
"description": "Library for accessing Digital Ocean spaces", "description": "Library for accessing Digital Ocean spaces",
"version":"3.1.0", "version":"3.6.1",
"type": "library", "type": "library",
"license": "MIT", "license": "MIT",
"authors": [ "authors": [
@ -27,7 +27,7 @@
}, },
"require-dev": { "require-dev": {
"clean/phpdoc-md": "^0.19.1", "clean/phpdoc-md": "^0.19.1",
"phpunit/phpunit": "^9.5", "phpunit/phpunit": "^9.5.13",
"vlucas/phpdotenv": "^5.3" "vlucas/phpdotenv": "^v5.4.1"
} }
} }

945
composer.lock generated

File diff suppressed because it is too large Load diff

View file

@ -33,7 +33,7 @@ foreach ($files['files'] as $file) {
```php ```php
$space = new Spaces('api-key', 'api-secret')->space('my-space-name'); $space = new Spaces('api-key', 'api-secret')->space('my-space-name');
$file = $space->uploadFile('./localfile.txt'); $file = $space->uploadFile('./localfile.txt', 'remote-filename.txt');
``` ```
[API docs for uploading files](Space.md#spaceuploadfile) [API docs for uploading files](Space.md#spaceuploadfile)

View file

@ -21,18 +21,16 @@ Rather obtain an instance from `\SpacesAPI\Space::list()`, `\SpacesAPI\Spaces::f
| Name | Description | | Name | Description |
|------|-------------| |------|-------------|
|[__construct](#file__construct)|| |[__construct](#file__construct)||
|[__get](#file__get)|Magic getter to make the properties read-only|
|[copy](#filecopy)|Copy the file on the space| |[copy](#filecopy)|Copy the file on the space|
|[delete](#filedelete)|Permanently delete this file| |[delete](#filedelete)|Permanently delete this file|
|[download](#filedownload)|Download the file to a local location| |[download](#filedownload)|Download the file to a local location|
|[getContents](#filegetcontents)|Get the file contents as a string| |[getContents](#filegetcontents)|Get the file contents as a string|
|[getSignedURL](#filegetsignedurl)|Get a signed URL, which will work for private files| |[getSignedURL](#filegetsignedurl)|Get a signed URL, which will work for private files|
|[getURL](#filegeturl)|Get the public URL |[getURL](#filegeturl)|Get the public URL. This URL will not work if the file is private|
This URL will not work if the file is private|
|[isPublic](#fileispublic)|Is this file publicly accessible| |[isPublic](#fileispublic)|Is this file publicly accessible|
|[makePrivate](#filemakeprivate)|Make file non-publicly accessible| |[makePrivate](#filemakeprivate)|Make file non-publicly accessible|
|[makePublic](#filemakepublic)|Make file publicly accessible| |[makePublic](#filemakepublic)|Make file publicly accessible|
|[pascalCaseToCamelCase](#filepascalcasetocamelcase)|| |[move](#filemove)|Move and/or rename file|
@ -42,18 +40,15 @@ This URL will not work if the file is private|
**Description** **Description**
```php ```php
__construct (\SpacesAPI\Space $space, string $filename, array $info = []) __construct (\SpacesAPI\Space $space, string $filename, array $info = [], bool $validate = true)
``` ```
**Parameters** **Parameters**
* `(\SpacesAPI\Space) $space` : An instance of `\SpacesAPI\Space` * `(\SpacesAPI\Space) $space` : An instance of `\SpacesAPI\Space`
* `(string) $filename` : The filename of a file * `(string) $filename` : The filename of a file
* `(array) $info` : Any information already known about the file (eg content_length, content_type, etc). Default `[]` * `(array) $info` : Any information already known about the file (eg content_length, content_type, etc). Default `[]`
* `(bool) $validate` : Check that the file exists. Default `true`
**Return Values** **Return Values**
@ -61,7 +56,7 @@ This URL will not work if the file is private|
**Throws Exceptions** **Throws Exceptions**
`\SpacesAPI\Exceptions\FileDoesntExistException` : If the file doesn't exist `\SpacesAPI\Exceptions\FileDoesntExistException` : If validation is `true` and the file doesn't exist
<hr /> <hr />
@ -294,4 +289,27 @@ Make file publicly accessible
`void` `void`
<hr />
### File::move
**Description**
```php
public move (string $newFilename)
```
Move or rename a file
The instance of `File` is now the moved object
**Parameters**
* `(string) $newFilename`
**Return Values**
`\SpacesAPI\File` : An instance for the new file
<hr /> <hr />

View file

@ -37,7 +37,7 @@ Rather obtain an instance from `\SpacesAPI\Spaces::space()` or `\SpacesAPI\Space
**Description** **Description**
```php ```php
public __construct (\Aws\S3\S3Client $s3, string $name) public __construct (\Aws\S3\S3Client $s3, string $name, bool $validate = true)
``` ```
Load a space Load a space
@ -51,6 +51,8 @@ rather obtain an instance from `\SpacesAPI\Spaces::space()` or `\SpacesAPI\Space
: An authenticated S3Client instance : An authenticated S3Client instance
* `(string) $name` * `(string) $name`
: Space name : Space name
* `(bool) $validate`
: Check that the space exists. Default `true`
**Return Values** **Return Values**
@ -60,7 +62,7 @@ rather obtain an instance from `\SpacesAPI\Spaces::space()` or `\SpacesAPI\Space
**Throws Exceptions** **Throws Exceptions**
`\SpacesAPI\Exceptions\SpaceDoesntExistException` : If the space doesn't exist `\SpacesAPI\Exceptions\SpaceDoesntExistException` : If validation is `true` and the space doesn't exist
<hr /> <hr />
@ -178,7 +180,7 @@ Recursively download an entire directory.
**Description** **Description**
```php ```php
public file (string $filename) public file (string $filename, bool $validate = true)
``` ```
Get an instance of \SpacesAPI\File for a given filename Get an instance of \SpacesAPI\File for a given filename
@ -188,6 +190,8 @@ Get an instance of \SpacesAPI\File for a given filename
**Parameters** **Parameters**
* `(string) $filename` * `(string) $filename`
* `(bool) $validate`
: Whether to validate the file exits. Defaults to `true`
**Return Values** **Return Values**
@ -314,7 +318,7 @@ Is file listing enabled?
**Description** **Description**
```php ```php
public listFiles (string $directory, string|null $continuationToken = null) public listFiles (string $directory = "")
``` ```
List all files in the space (recursively) List all files in the space (recursively)
@ -325,8 +329,6 @@ List all files in the space (recursively)
* `(string) $directory` * `(string) $directory`
: The directory to list files in. Empty string for root directory : The directory to list files in. Empty string for root directory
* `(string|null) $continuationToken`
: Used internally to work around request limits (1000 files per request). Leave this value `null`
**Return Values** **Return Values**
@ -463,7 +465,7 @@ Recursively upload an entire directory
**Description** **Description**
```php ```php
public uploadFile (string $filepath, string|null $filename = null) public uploadFile (string $filepath, string|null $filename = null, string|null $mimeType = null, bool $private = true)
``` ```
Upload a file Upload a file
@ -476,6 +478,10 @@ Upload a file
: The path to the file, including the filename. Relative and absolute paths are accepted. : The path to the file, including the filename. Relative and absolute paths are accepted.
* `(string|null) $filename` * `(string|null) $filename`
: The remote filename. If `null`, the local filename will be used. Default `null` : The remote filename. If `null`, the local filename will be used. Default `null`
* `(string|null) $mimeType`
: The files mimeType. If `null` the mimeType is inferred from the file by DigitalOcean Spaces. Default `null`
* `(bool)` $private
: If `true` then the file is private, if `false` the file is publicly available. Default `true`
**Return Values** **Return Values**
@ -492,7 +498,7 @@ Upload a file
**Description** **Description**
```php ```php
public uploadText (string $text, string $filename, array $params => []) public uploadText (string $text, string $filename, array $params = [], bool $private = true)
``` ```
Upload a string of text to file Upload a string of text to file
@ -507,6 +513,8 @@ Upload a string of text to file
: The filepath/name to save to : The filepath/name to save to
* `(array) $params` * `(array) $params`
: Any extra parameters. [See here](https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/S3.html#upload-property) : Any extra parameters. [See here](https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/S3.html#upload-property)
* `(bool) $private`
: True for the file to be private, false to allow public access
**Return Values** **Return Values**

View file

@ -8,6 +8,7 @@ use SpacesAPI\Spaces;
class FileTest extends TestCase class FileTest extends TestCase
{ {
private static $tempSpaceName;
private static $space; private static $space;
private static $file; private static $file;
@ -19,18 +20,15 @@ class FileTest extends TestCase
$spaces = new Spaces($_ENV['SPACES_KEY'], $_ENV['SPACES_SECRET']); $spaces = new Spaces($_ENV['SPACES_KEY'], $_ENV['SPACES_SECRET']);
try { self::$tempSpaceName = md5(time());
$spaces->space('spaces-api-test')->destroy();
} catch (SpaceDoesntExistException $e) {
}
self::$space = $spaces->create('spaces-api-test'); self::$space = $spaces->create(self::$tempSpaceName);
self::$file = self::$space->uploadText('Lorem ipsum', 'lorem-ipsum.txt'); self::$file = self::$space->uploadText('Lorem ipsum', 'lorem-ipsum.txt');
} }
public static function tearDownAfterClass(): void public static function tearDownAfterClass(): void
{ {
(new Spaces($_ENV['SPACES_KEY'], $_ENV['SPACES_SECRET']))->space('spaces-api-test')->destroy(); self::$space->destroy();
} }
public function testCanUpdatePrivacy() public function testCanUpdatePrivacy()
@ -64,6 +62,17 @@ class FileTest extends TestCase
self::$space->file('lorem-ipsum-2.txt'); self::$space->file('lorem-ipsum-2.txt');
} }
public function testCanMoveRenameFile()
{
$file = self::$file->copy('test.txt');
$file->move('renamed-file.txt');
$this->assertEquals("renamed-file.txt", $file->filename);
$this->expectException(FileDoesntExistException::class);
self::$space->file('test.txt');
}
public function testCanGetURL() public function testCanGetURL()
{ {
$this->assertStringContainsString('lorem-ipsum.txt', self::$file->getURL()); $this->assertStringContainsString('lorem-ipsum.txt', self::$file->getURL());

View file

@ -9,6 +9,7 @@ use SpacesAPI\Spaces;
class SpaceTest extends TestCase class SpaceTest extends TestCase
{ {
private static $tempSpaceName;
private static $space; private static $space;
public static function setUpBeforeClass(): void public static function setUpBeforeClass(): void
@ -19,17 +20,14 @@ class SpaceTest extends TestCase
$spaces = new Spaces($_ENV['SPACES_KEY'], $_ENV['SPACES_SECRET']); $spaces = new Spaces($_ENV['SPACES_KEY'], $_ENV['SPACES_SECRET']);
try { self::$tempSpaceName = md5(time());
$spaces->space('spaces-api-test')->destroy();
} catch (SpaceDoesntExistException $e) {
}
self::$space = $spaces->create('spaces-api-test'); self::$space = $spaces->create(self::$tempSpaceName);
} }
public static function tearDownAfterClass(): void public static function tearDownAfterClass(): void
{ {
// (new Spaces($_ENV['SPACES_KEY'], $_ENV['SPACES_SECRET']))->space('spaces-api-test')->destroy(); self::$space->destroy();
} }
public function testCanUpdateSpacePrivacy() public function testCanUpdateSpacePrivacy()
@ -71,6 +69,14 @@ class SpaceTest extends TestCase
{ {
$file = self::$space->uploadText("Lorem ipsum", "lorem-ipsum.txt"); $file = self::$space->uploadText("Lorem ipsum", "lorem-ipsum.txt");
$this->assertInstanceOf(File::class, $file); $this->assertInstanceOf(File::class, $file);
$this->assertFalse($file->isPublic());
}
public function testCanPublicUploadText()
{
$file = self::$space->uploadText("Lorem ipsum", "lorem-ipsum.txt", [], false);
$this->assertInstanceOf(File::class, $file);
$this->assertTrue($file->isPublic());
} }
public function testCanUploadFile() public function testCanUploadFile()
@ -80,6 +86,14 @@ class SpaceTest extends TestCase
$this->assertInstanceOf(File::class, $file); $this->assertInstanceOf(File::class, $file);
} }
public function testCanUploadFileWithMimeType()
{
$tmpFile = tempnam(sys_get_temp_dir(), 'spaces-test');
$file = self::$space->uploadFile($tmpFile, 'upload-test.txt', 'text/plain');
$this->assertInstanceOf(File::class, $file);
$this->assertEquals('text/plain', $file->content_type);
}
/** /**
* @depends testCanUploadText * @depends testCanUploadText
* @depends testCanUploadFile * @depends testCanUploadFile

View file

@ -10,21 +10,21 @@ use SpacesAPI\Spaces;
class SpacesTest extends TestCase class SpacesTest extends TestCase
{ {
private static $tempSpaceName;
public static function setUpBeforeClass(): void public static function setUpBeforeClass(): void
{ {
$dotenv = Dotenv::createImmutable(__DIR__ . "/.."); $dotenv = Dotenv::createImmutable(__DIR__ . "/..");
$dotenv->load(); $dotenv->load();
$dotenv->required(['SPACES_KEY', 'SPACES_SECRET']); $dotenv->required(['SPACES_KEY', 'SPACES_SECRET']);
try { // This should hopefully always be unique amongst all DO spaces
(new Spaces($_ENV['SPACES_KEY'], $_ENV['SPACES_SECRET']))->space('spaces-api-test')->destroy(); self::$tempSpaceName = md5(time());
} catch (SpaceDoesntExistException $e) {
}
} }
public static function tearDownAfterClass(): void public static function tearDownAfterClass(): void
{ {
(new Spaces($_ENV['SPACES_KEY'], $_ENV['SPACES_SECRET']))->space('spaces-api-test')->destroy(); (new Spaces($_ENV['SPACES_KEY'], $_ENV['SPACES_SECRET']))->space(self::$tempSpaceName)->destroy();
} }
public function testAuthenticationCanFail() public function testAuthenticationCanFail()
@ -53,7 +53,7 @@ class SpacesTest extends TestCase
*/ */
public function testCanCreateSpace(Spaces $spaces) public function testCanCreateSpace(Spaces $spaces)
{ {
$space = $spaces->create('spaces-api-test'); $space = $spaces->create(self::$tempSpaceName);
$this->assertInstanceOf(Space::class, $space); $this->assertInstanceOf(Space::class, $space);
return $space; return $space;
@ -69,7 +69,7 @@ class SpacesTest extends TestCase
$spaceFound = false; $spaceFound = false;
foreach ($list as $name => $space) { foreach ($list as $name => $space) {
if ($name == 'spaces-api-test' && $space->getName() == 'spaces-api-test') { if ($name == self::$tempSpaceName && $space->getName() == self::$tempSpaceName) {
$spaceFound = true; $spaceFound = true;
} }
} }