← All changes
|
includes/sdk/s3/Aws/S3/BucketEndpointMiddleware.php
+34
-8
1.2.11
→
1.4.2
View file →
| @@ -2,8 +2,9 @@ | ||
| 2 | 2 | |
| 3 | 3 | namespace Dudlewebs\WPMCS\s3\Aws\S3; |
| 4 | 4 | |
| 5 | 5 | use Dudlewebs\WPMCS\s3\Aws\CommandInterface; |
| 6 | +use Dudlewebs\WPMCS\s3\GuzzleHttp\Psr7\Uri; | |
| 6 | 7 | use Dudlewebs\WPMCS\s3\Psr\Http\Message\RequestInterface; |
| 7 | 8 | /** |
| 8 | 9 | * Used to update the host used for S3 requests in the case of using a |
| 9 | 10 | * "bucket endpoint" or CNAME bucket. |
| @@ -15,22 +16,29 @@ | ||
| 15 | 16 | class BucketEndpointMiddleware |
| 16 | 17 | { |
| 17 | 18 | private static $exclusions = ['GetBucketLocation' => \true]; |
| 18 | 19 | private $nextHandler; |
| 20 | + private bool $useEndpointV2; | |
| 21 | + private ?string $endpoint; | |
| 19 | 22 | /** |
| 20 | 23 | * Create a middleware wrapper function. |
| 21 | 24 | * |
| 25 | + * @param bool $useEndpointV2 | |
| 26 | + * @param string|null $endpoint | |
| 27 | + * | |
| 22 | 28 | * @return callable |
| 23 | 29 | */ |
| 24 | - public static function wrap() | |
| 30 | + public static function wrap(bool $useEndpointV2 = \false, ?string $endpoint = null) : callable | |
| 25 | 31 | { |
| 26 | - return function (callable $handler) { | |
| 27 | - return new self($handler); | |
| 32 | + return function (callable $handler) use($useEndpointV2, $endpoint) { | |
| 33 | + return new self($handler, $useEndpointV2, $endpoint); | |
| 28 | 34 | }; |
| 29 | 35 | } |
| 30 | - public function __construct(callable $nextHandler) | |
| 36 | + public function __construct(callable $nextHandler, bool $useEndpointV2, ?string $endpoint = null) | |
| 31 | 37 | { |
| 32 | 38 | $this->nextHandler = $nextHandler; |
| 39 | + $this->useEndpointV2 = $useEndpointV2; | |
| 40 | + $this->endpoint = $endpoint; | |
| 33 | 41 | } |
| 34 | 42 | public function __invoke(CommandInterface $command, RequestInterface $request) |
| 35 | 43 | { |
| 36 | 44 | $nextHandler = $this->nextHandler; |
| @@ -39,25 +47,43 @@ | ||
| 39 | 47 | $request = $this->modifyRequest($request, $command); |
| 40 | 48 | } |
| 41 | 49 | return $nextHandler($command, $request); |
| 42 | 50 | } |
| 43 | - private function removeBucketFromPath($path, $bucket) | |
| 51 | + /** | |
| 52 | + * @param string $path | |
| 53 | + * @param string $bucket | |
| 54 | + * | |
| 55 | + * @return string | |
| 56 | + */ | |
| 57 | + private function removeBucketFromPath(string $path, string $bucket) : string | |
| 44 | 58 | { |
| 45 | 59 | $len = \strlen($bucket) + 1; |
| 46 | - if (\substr($path, 0, $len) === "/{$bucket}") { | |
| 60 | + if (\str_starts_with($path, "/{$bucket}")) { | |
| 47 | 61 | $path = \substr($path, $len); |
| 48 | 62 | } |
| 49 | 63 | return $path ?: '/'; |
| 50 | 64 | } |
| 51 | - private function modifyRequest(RequestInterface $request, CommandInterface $command) | |
| 65 | + /** | |
| 66 | + * @param RequestInterface $request | |
| 67 | + * @param CommandInterface $command | |
| 68 | + * | |
| 69 | + * @return RequestInterface | |
| 70 | + */ | |
| 71 | + private function modifyRequest(RequestInterface $request, CommandInterface $command) : RequestInterface | |
| 52 | 72 | { |
| 53 | 73 | $uri = $request->getUri(); |
| 54 | 74 | $path = $uri->getPath(); |
| 75 | + $host = $uri->getHost(); | |
| 55 | 76 | $bucket = $command['Bucket']; |
| 77 | + if ($this->useEndpointV2 && !empty($this->endpoint)) { | |
| 78 | + // V2 provider adds bucket name to host by default | |
| 79 | + // preserve original host | |
| 80 | + $host = (new Uri($this->endpoint))->getHost(); | |
| 81 | + } | |
| 56 | 82 | $path = $this->removeBucketFromPath($path, $bucket); |
| 57 | 83 | // Modify the Key to make sure the key is encoded, but slashes are not. |
| 58 | 84 | if ($command['Key']) { |
| 59 | 85 | $path = S3Client::encodeKey(\rawurldecode($path)); |
| 60 | 86 | } |
| 61 | - return $request->withUri($uri->withPath($path)); | |
| 87 | + return $request->withUri($uri->withPath($path)->withHost($host)); | |
| 62 | 88 | } |
| 63 | 89 | } |