PutObjectUrlMiddleware.php 1.5 KB

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