diff --git a/SpacesAPI/File.php b/SpacesAPI/File.php index f824b8a..6917d3e 100644 --- a/SpacesAPI/File.php +++ b/SpacesAPI/File.php @@ -46,20 +46,21 @@ class File private $_content_length; /** - * @param \SpacesAPI\Space $space - * @param string $filename - * @param array $info + * @param \SpacesAPI\Space $space An instance of `\SpacesAPI\Space` + * @param string $filename The filename of a file + * @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_name = $space->getName(); $this->s3 = $space->getS3Client(); $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"); } diff --git a/SpacesAPI/Space.php b/SpacesAPI/Space.php index 0b55f8a..217fa67 100644 --- a/SpacesAPI/Space.php +++ b/SpacesAPI/Space.php @@ -36,15 +36,16 @@ class Space * * @param \Aws\S3\S3Client $s3 An authenticated S3Client instance * @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->name = $name; - if (!$this->s3->doesBucketExist($name)) { + if ($validate && !$this->s3->doesBucketExist($name)) { throw new SpaceDoesntExistException("Space '$this->name' does not exist"); } } @@ -233,13 +234,29 @@ class Space /** * 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|null $continuationToken Used internally to work around request limits (1000 files per request) * * @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( $this->s3->listObjectsV2([ @@ -252,20 +269,14 @@ class Space ]) ); - $files = [ - 'files' => [], - ]; - if (!isset($data['Contents'])) { - return $files; + return []; } - foreach ($data['Contents'] as $fileInfo) { - $files['files'][$fileInfo['Key']] = new File($this, $fileInfo['Key'], $fileInfo); - } + $files = $data['Contents']; 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; @@ -283,7 +294,7 @@ class Space public function uploadText(string $text, string $filename, array $params = []): File { $this->s3->upload($this->name, $filename, $text, 'private', $params); - return new File($this, $filename); + return new File($this, $filename, [], false); } /** @@ -302,7 +313,7 @@ class Space 'SourceFile' => $filepath, ]); - return new File($this, ($filename) ?: basename($filepath)); + return new File($this, ($filename) ?: basename($filepath), [], false); } /** diff --git a/SpacesAPI/Spaces.php b/SpacesAPI/Spaces.php index 3f90470..d822e8a 100644 --- a/SpacesAPI/Spaces.php +++ b/SpacesAPI/Spaces.php @@ -61,7 +61,7 @@ class Spaces $spaces = []; 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; @@ -87,7 +87,7 @@ class Spaces throw new SpaceExistsException($e->getAwsErrorMessage()); } - return new Space($this->s3, $name); + return new Space($this->s3, $name, false); } /** diff --git a/docs/File.md b/docs/File.md index 4fe1804..f347f44 100644 --- a/docs/File.md +++ b/docs/File.md @@ -39,18 +39,15 @@ Rather obtain an instance from `\SpacesAPI\Space::list()`, `\SpacesAPI\Spaces::f **Description** ```php - __construct (\SpacesAPI\Space $space, string $filename, array $info = []) + __construct (\SpacesAPI\Space $space, string $filename, array $info = [], bool $validate = true) ``` - - - - **Parameters** * `(\SpacesAPI\Space) $space` : An instance of `\SpacesAPI\Space` * `(string) $filename` : The filename of a file * `(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** @@ -58,7 +55,7 @@ Rather obtain an instance from `\SpacesAPI\Space::list()`, `\SpacesAPI\Spaces::f **Throws Exceptions** -`\SpacesAPI\Exceptions\FileDoesntExistException` : If the file doesn't exist +`\SpacesAPI\Exceptions\FileDoesntExistException` : If validation is `true` and the file doesn't exist
diff --git a/docs/Space.md b/docs/Space.md index ef23140..19c5155 100644 --- a/docs/Space.md +++ b/docs/Space.md @@ -37,7 +37,7 @@ Rather obtain an instance from `\SpacesAPI\Spaces::space()` or `\SpacesAPI\Space **Description** ```php -public __construct (\Aws\S3\S3Client $s3, string $name) +public __construct (\Aws\S3\S3Client $s3, string $name, bool $validate = true) ``` Load a space @@ -51,6 +51,8 @@ rather obtain an instance from `\SpacesAPI\Spaces::space()` or `\SpacesAPI\Space : An authenticated S3Client instance * `(string) $name` : Space name +* `(bool) $validate` +: Check that the space exists. Default `true` **Return Values** @@ -60,7 +62,7 @@ rather obtain an instance from `\SpacesAPI\Spaces::space()` or `\SpacesAPI\Space **Throws Exceptions** -`\SpacesAPI\Exceptions\SpaceDoesntExistException` : If the space doesn't exist +`\SpacesAPI\Exceptions\SpaceDoesntExistException` : If validation is `true` and the space doesn't exist
@@ -314,7 +316,7 @@ Is file listing enabled? **Description** ```php -public listFiles (string $directory, string|null $continuationToken = null) +public listFiles (string $directory = "") ``` List all files in the space (recursively) @@ -325,8 +327,6 @@ List all files in the space (recursively) * `(string) $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**