-
Notifications
You must be signed in to change notification settings - Fork 356
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ab3604b
commit ed22632
Showing
1 changed file
with
30 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,37 +10,26 @@ | |
/** | ||
* Amazon S3 adapter using the AsyncAws. | ||
* | ||
* @author Michael Dowling <[email protected]> | ||
* @author Michael Dowling <[email protected]> | ||
* @author Tobias Nyholm <[email protected]> | ||
*/ | ||
class AsyncAwsS3 implements Adapter, MetadataSupporter, ListKeysAware, SizeCalculator, MimeTypeProvider | ||
{ | ||
/** @var SimpleS3Client */ | ||
protected $service; | ||
/** @var string */ | ||
protected $bucket; | ||
/** @var array */ | ||
protected $options; | ||
/** @var bool */ | ||
protected $bucketExists; | ||
/** @var array */ | ||
protected $metadata = []; | ||
/** @var bool */ | ||
protected $detectContentType; | ||
|
||
/** | ||
* @param SimpleS3Client $service | ||
* @param string $bucket | ||
* @param array $options | ||
* @param bool $detectContentType | ||
*/ | ||
public function __construct(SimpleS3Client $service, $bucket, array $options = [], $detectContentType = false) | ||
{ | ||
protected SimpleS3Client $service; | ||
protected array $options; | ||
protected bool $bucketExists; | ||
protected array $metadata = []; | ||
|
||
public function __construct( | ||
SimpleS3Client $service, | ||
private readonly string $bucket, | ||
array $options = [], | ||
private readonly bool $detectContentType = false | ||
) { | ||
if (!class_exists(SimpleS3Client::class)) { | ||
throw new \LogicException('You need to install package "async-aws/simple-s3" to use this adapter'); | ||
} | ||
$this->service = $service; | ||
$this->bucket = $bucket; | ||
$this->options = array_replace( | ||
[ | ||
'create' => false, | ||
|
@@ -49,14 +38,12 @@ public function __construct(SimpleS3Client $service, $bucket, array $options = [ | |
], | ||
$options | ||
); | ||
|
||
$this->detectContentType = $detectContentType; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function setMetadata($key, $content) | ||
public function setMetadata(string $key, array $content) | ||
{ | ||
// BC with AmazonS3 adapter | ||
if (isset($content['contentType'])) { | ||
|
@@ -70,15 +57,15 @@ public function setMetadata($key, $content) | |
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getMetadata($key) | ||
public function getMetadata(string $key): array | ||
{ | ||
return $this->metadata[$key] ?? []; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function read($key) | ||
public function read(string $key): string|bool | ||
{ | ||
$this->ensureBucketExists(); | ||
$options = $this->getOptions($key); | ||
|
@@ -102,7 +89,7 @@ public function read($key) | |
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function rename($sourceKey, $targetKey) | ||
public function rename(string $sourceKey, string $targetKey): bool | ||
{ | ||
$this->ensureBucketExists(); | ||
$options = $this->getOptions( | ||
|
@@ -121,9 +108,8 @@ public function rename($sourceKey, $targetKey) | |
|
||
/** | ||
* {@inheritdoc} | ||
* @param string|resource $content | ||
*/ | ||
public function write($key, $content) | ||
public function write(string $key, mixed $content): int|bool | ||
{ | ||
$this->ensureBucketExists(); | ||
$options = $this->getOptions($key); | ||
|
@@ -153,15 +139,15 @@ public function write($key, $content) | |
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function exists($key) | ||
public function exists(string $key): bool | ||
{ | ||
return $this->service->has($this->bucket, $this->computePath($key)); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function mtime($key) | ||
public function mtime(string $key): int|bool | ||
{ | ||
try { | ||
$result = $this->service->headObject($this->getOptions($key)); | ||
|
@@ -175,14 +161,14 @@ public function mtime($key) | |
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function size($key) | ||
public function size(string $key): int | ||
{ | ||
$result = $this->service->headObject($this->getOptions($key)); | ||
|
||
return (int) $result->getContentLength(); | ||
} | ||
|
||
public function mimeType($key) | ||
public function mimeType(string $key): string | ||
{ | ||
$result = $this->service->headObject($this->getOptions($key)); | ||
|
||
|
@@ -192,15 +178,15 @@ public function mimeType($key) | |
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function keys() | ||
public function keys(): array | ||
{ | ||
return $this->listKeys(); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function listKeys($prefix = '') | ||
public function listKeys(string $prefix = ''): array | ||
{ | ||
$this->ensureBucketExists(); | ||
|
||
|
@@ -223,7 +209,7 @@ public function listKeys($prefix = '') | |
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function delete($key) | ||
public function delete(string $key): bool | ||
{ | ||
try { | ||
$this->service->deleteObject($this->getOptions($key)); | ||
|
@@ -237,7 +223,7 @@ public function delete($key) | |
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function isDirectory($key) | ||
public function isDirectory(string $key): bool | ||
{ | ||
$result = $this->service->listObjectsV2([ | ||
'Bucket' => $this->bucket, | ||
|
@@ -261,7 +247,7 @@ public function isDirectory($key) | |
* @throws \RuntimeException if the bucket does not exists or could not be | ||
* created | ||
*/ | ||
protected function ensureBucketExists() | ||
protected function ensureBucketExists(): bool | ||
{ | ||
if ($this->bucketExists) { | ||
return true; | ||
|
@@ -288,7 +274,7 @@ protected function ensureBucketExists() | |
return true; | ||
} | ||
|
||
protected function getOptions($key, array $options = []) | ||
protected function getOptions(string $key, array $options = []): array | ||
{ | ||
$options['ACL'] = $this->options['acl']; | ||
$options['Bucket'] = $this->bucket; | ||
|
@@ -303,7 +289,7 @@ protected function getOptions($key, array $options = []) | |
return $options; | ||
} | ||
|
||
protected function computePath($key) | ||
protected function computePath(string $key): string | ||
{ | ||
if (empty($this->options['directory'])) { | ||
return $key; | ||
|
@@ -314,22 +300,13 @@ protected function computePath($key) | |
|
||
/** | ||
* Computes the key from the specified path. | ||
* | ||
* @param string $path | ||
* | ||
* return string | ||
*/ | ||
protected function computeKey($path) | ||
protected function computeKey(string $path): string | ||
{ | ||
return ltrim(substr($path, strlen($this->options['directory'])), '/'); | ||
} | ||
|
||
/** | ||
* @param string|resource $content | ||
* | ||
* @return string | ||
*/ | ||
private function guessContentType($content) | ||
private function guessContentType(mixed $content): false|string | ||
{ | ||
$fileInfo = new \finfo(FILEINFO_MIME_TYPE); | ||
|
||
|