PluginProbe
Media Cloud Sync / 1.4.1
Media Cloud Sync v1.4.1
1.4.1 1.4.0 1.3.12 1.3.11 1.3.10 trunk 1.0.0 1.0.1 1.0.2 1.0.3 1.1.0 1.1.1 1.2.0 1.2.10 1.2.11 1.2.12 1.2.13 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.2.7 1.2.8 1.2.9 All 35 releases
media-cloud-sync / includes / sdk / s3 / Aws / S3 / ObjectUploader.php

ObjectUploader.php in Media Cloud Sync 1.4.1, at includes/sdk/s3/Aws/S3/ObjectUploader.php

118 lines 5.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Dudlewebs\WPMCS\s3\Aws\S3;
4
5 use Dudlewebs\WPMCS\s3\GuzzleHttp\Promise\PromiseInterface;
6 use Dudlewebs\WPMCS\s3\GuzzleHttp\Promise\PromisorInterface;
7 use Dudlewebs\WPMCS\s3\GuzzleHttp\Psr7;
8 use Dudlewebs\WPMCS\s3\Psr\Http\Message\StreamInterface;
9 /**
10 * Uploads an object to S3, using a PutObject command or a multipart upload as
11 * appropriate.
12 */
13 class ObjectUploader implements PromisorInterface
14 {
15 const DEFAULT_MULTIPART_THRESHOLD = 16777216;
16 private $client;
17 private $bucket;
18 private $key;
19 private $body;
20 private $acl;
21 private $options;
22 private static $defaults = ['before_upload' => null, 'concurrency' => 3, 'mup_threshold' => self::DEFAULT_MULTIPART_THRESHOLD, 'params' => [], 'part_size' => null];
23 private $addContentMD5;
24 /**
25 * @param S3ClientInterface $client The S3 Client used to execute
26 * the upload command(s).
27 * @param string $bucket Bucket to upload the object, or
28 * an S3 access point ARN.
29 * @param string $key Key of the object.
30 * @param mixed $body Object data to upload. Can be a
31 * StreamInterface, PHP stream
32 * resource, or a string of data to
33 * upload.
34 * @param string $acl ACL to apply to the copy
35 * (default: private).
36 * @param array $options Options used to configure the
37 * copy process. Options passed in
38 * through 'params' are added to
39 * the sub command(s).
40 */
41 public function __construct(S3ClientInterface $client, $bucket, $key, $body, $acl = 'private', array $options = [])
42 {
43 $this->client = $client;
44 $this->bucket = $bucket;
45 $this->key = $key;
46 $this->body = Psr7\Utils::streamFor($body);
47 $this->acl = $acl;
48 $this->options = $options + self::$defaults;
49 // Handle "add_content_md5" option.
50 $this->addContentMD5 = isset($options['add_content_md5']) && $options['add_content_md5'] === \true;
51 }
52 /**
53 * @return PromiseInterface
54 */
55 public function promise() : PromiseInterface
56 {
57 /** @var int $mup_threshold */
58 $mup_threshold = $this->options['mup_threshold'];
59 if ($this->requiresMultipart($this->body, $mup_threshold)) {
60 // Perform a multipart upload.
61 return (new MultipartUploader($this->client, $this->body, ['bucket' => $this->bucket, 'key' => $this->key, 'acl' => $this->acl] + $this->options))->promise();
62 }
63 // Perform a regular PutObject operation.
64 $command = $this->client->getCommand('PutObject', ['Bucket' => $this->bucket, 'Key' => $this->key, 'Body' => $this->body, 'ACL' => $this->acl, 'AddContentMD5' => $this->addContentMD5] + $this->options['params']);
65 if (\is_callable($this->options['before_upload'])) {
66 $this->options['before_upload']($command);
67 }
68 return $this->client->executeAsync($command);
69 }
70 public function upload()
71 {
72 return $this->promise()->wait();
73 }
74 /**
75 * Determines if the body should be uploaded using PutObject or the
76 * Multipart Upload System. It also modifies the passed-in $body as needed
77 * to support the upload.
78 *
79 * @param StreamInterface $body Stream representing the body.
80 * @param integer $threshold Minimum bytes before using Multipart.
81 *
82 * @return bool
83 */
84 private function requiresMultipart(StreamInterface &$body, $threshold)
85 {
86 // If body size known, compare to threshold to determine if Multipart.
87 if ($body->getSize() !== null) {
88 return $body->getSize() >= $threshold;
89 }
90 /**
91 * Handle the situation where the body size is unknown.
92 * Read up to 5MB into a buffer to determine how to upload the body.
93 * @var StreamInterface $buffer
94 */
95 $buffer = Psr7\Utils::streamFor();
96 Psr7\Utils::copyToStream($body, $buffer, MultipartUploader::PART_MIN_SIZE);
97 // If body < 5MB, use PutObject with the buffer.
98 if ($buffer->getSize() < MultipartUploader::PART_MIN_SIZE) {
99 $buffer->seek(0);
100 $body = $buffer;
101 return \false;
102 }
103 // If body >= 5 MB, then use multipart. [YES]
104 if ($body->isSeekable() && $body->getMetadata('uri') !== 'php://input') {
105 // If the body is seekable, just rewind the body.
106 $body->seek(0);
107 } else {
108 // If the body is non-seekable, stitch the rewind the buffer and
109 // the partially read body together into one stream. This avoids
110 // unnecessary disc usage and does not require seeking on the
111 // original stream.
112 $buffer->seek(0);
113 $body = new Psr7\AppendStream([$buffer, $body]);
114 }
115 return \true;
116 }
117 }
118