| 1 |
<?php |
| 2 |
|
| 3 |
namespace Dudlewebs\WPMCS\s3\Aws\S3; |
| 4 |
|
| 5 |
use Dudlewebs\WPMCS\s3\Aws\CommandInterface; |
| 6 |
use Dudlewebs\WPMCS\s3\Aws\ResultInterface; |
| 7 |
use Dudlewebs\WPMCS\s3\Psr\Http\Message\RequestInterface; |
| 8 |
/** |
| 9 |
* Injects ObjectURL into the result of the PutObject operation. |
| 10 |
* |
| 11 |
* @internal |
| 12 |
*/ |
| 13 |
class PutObjectUrlMiddleware |
| 14 |
{ |
| 15 |
/** @var callable */ |
| 16 |
private $nextHandler; |
| 17 |
/** |
| 18 |
* Create a middleware wrapper function. |
| 19 |
* |
| 20 |
* @return callable |
| 21 |
*/ |
| 22 |
public static function wrap() |
| 23 |
{ |
| 24 |
return function (callable $handler) { |
| 25 |
return new self($handler); |
| 26 |
}; |
| 27 |
} |
| 28 |
/** |
| 29 |
* @param callable $nextHandler Next handler to invoke. |
| 30 |
*/ |
| 31 |
public function __construct(callable $nextHandler) |
| 32 |
{ |
| 33 |
$this->nextHandler = $nextHandler; |
| 34 |
} |
| 35 |
public function __invoke(CommandInterface $command, ?RequestInterface $request = null) |
| 36 |
{ |
| 37 |
$next = $this->nextHandler; |
| 38 |
return $next($command, $request)->then(function (ResultInterface $result) use($command) { |
| 39 |
$name = $command->getName(); |
| 40 |
switch ($name) { |
| 41 |
case 'PutObject': |
| 42 |
case 'CopyObject': |
| 43 |
$result['ObjectURL'] = isset($result['@metadata']['effectiveUri']) ? $result['@metadata']['effectiveUri'] : null; |
| 44 |
break; |
| 45 |
case 'CompleteMultipartUpload': |
| 46 |
$result['ObjectURL'] = \urldecode($result['Location'] ?? ''); |
| 47 |
break; |
| 48 |
} |
| 49 |
return $result; |
| 50 |
}); |
| 51 |
} |
| 52 |
} |
| 53 |
|