PluginProbe
Media Cloud Sync / 1.2.13
Media Cloud Sync v1.2.13
1.4.2 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 All 36 releases
media-cloud-sync / includes / sdk / s3 / Aws / S3 / MultipartCopy.php

MultipartCopy.php in Media Cloud Sync 1.2.13, at includes/sdk/s3/Aws/S3/MultipartCopy.php

187 lines 8.4 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\Aws\Arn\ArnParser;
6 use Dudlewebs\WPMCS\s3\Aws\Multipart\AbstractUploadManager;
7 use Dudlewebs\WPMCS\s3\Aws\ResultInterface;
8 use Dudlewebs\WPMCS\s3\GuzzleHttp\Psr7;
9 class MultipartCopy extends AbstractUploadManager
10 {
11 use MultipartUploadingTrait;
12 /** @var string|array */
13 private $source;
14 /** @var string */
15 private $sourceVersionId;
16 /** @var ResultInterface */
17 private $sourceMetadata;
18 /**
19 * Creates a multipart upload for copying an S3 object.
20 *
21 * The valid configuration options are as follows:
22 *
23 * - acl: (string) ACL to set on the object being upload. Objects are
24 * private by default.
25 * - before_complete: (callable) Callback to invoke before the
26 * `CompleteMultipartUpload` operation. The callback should have a
27 * function signature like `function (Aws\Command $command) {...}`.
28 * - before_initiate: (callable) Callback to invoke before the
29 * `CreateMultipartUpload` operation. The callback should have a function
30 * signature like `function (Aws\Command $command) {...}`.
31 * - before_upload: (callable) Callback to invoke before `UploadPartCopy`
32 * operations. The callback should have a function signature like
33 * `function (Aws\Command $command) {...}`.
34 * - bucket: (string, required) Name of the bucket to which the object is
35 * being uploaded.
36 * - concurrency: (int, default=int(5)) Maximum number of concurrent
37 * `UploadPart` operations allowed during the multipart upload.
38 * - key: (string, required) Key to use for the object being uploaded.
39 * - params: (array) An array of key/value parameters that will be applied
40 * to each of the sub-commands run by the uploader as a base.
41 * Auto-calculated options will override these parameters. If you need
42 * more granularity over parameters to each sub-command, use the before_*
43 * options detailed above to update the commands directly.
44 * - part_size: (int, default=int(5242880)) Part size, in bytes, to use when
45 * doing a multipart upload. This must between 5 MB and 5 GB, inclusive.
46 * - state: (Aws\Multipart\UploadState) An object that represents the state
47 * of the multipart upload and that is used to resume a previous upload.
48 * When this option is provided, the `bucket`, `key`, and `part_size`
49 * options are ignored.
50 * - source_metadata: (Aws\ResultInterface) An object that represents the
51 * result of executing a HeadObject command on the copy source.
52 *
53 * @param S3ClientInterface $client Client used for the upload.
54 * @param string|array $source Location of the data to be copied (in the
55 * form /<bucket>/<key>). If the key contains a '?'
56 * character, instead pass an array of source_key,
57 * source_bucket, and source_version_id.
58 * @param array $config Configuration used to perform the upload.
59 */
60 public function __construct(S3ClientInterface $client, $source, array $config = [])
61 {
62 if (\is_array($source)) {
63 $this->source = $source;
64 } else {
65 $this->source = $this->getInputSource($source);
66 }
67 parent::__construct($client, \array_change_key_case($config) + ['source_metadata' => null]);
68 }
69 /**
70 * An alias of the self::upload method.
71 *
72 * @see self::upload
73 */
74 public function copy()
75 {
76 return $this->upload();
77 }
78 protected function loadUploadWorkflowInfo()
79 {
80 return ['command' => ['initiate' => 'CreateMultipartUpload', 'upload' => 'UploadPartCopy', 'complete' => 'CompleteMultipartUpload'], 'id' => ['bucket' => 'Bucket', 'key' => 'Key', 'upload_id' => 'UploadId'], 'part_num' => 'PartNumber'];
81 }
82 protected function getUploadCommands(callable $resultHandler)
83 {
84 $parts = \ceil($this->getSourceSize() / $this->determinePartSize());
85 for ($partNumber = 1; $partNumber <= $parts; $partNumber++) {
86 // If we haven't already uploaded this part, yield a new part.
87 if (!$this->state->hasPartBeenUploaded($partNumber)) {
88 $command = $this->client->getCommand($this->info['command']['upload'], $this->createPart($partNumber, $parts) + $this->getState()->getId());
89 $command->getHandlerList()->appendSign($resultHandler, 'mup');
90 (yield $command);
91 }
92 }
93 }
94 private function createPart($partNumber, $partsCount)
95 {
96 $data = [];
97 // Apply custom params to UploadPartCopy data
98 $config = $this->getConfig();
99 $params = isset($config['params']) ? $config['params'] : [];
100 foreach ($params as $k => $v) {
101 $data[$k] = $v;
102 }
103 // The source parameter here is usually a string, but can be overloaded as an array
104 // if the key contains a '?' character to specify where the query parameters start
105 if (\is_array($this->source)) {
106 $key = \str_replace('%2F', '/', \rawurlencode($this->source['source_key']));
107 $bucket = $this->source['source_bucket'];
108 } else {
109 list($bucket, $key) = \explode('/', \ltrim($this->source, '/'), 2);
110 $key = \implode('/', \array_map('urlencode', \explode('/', \rawurldecode($key))));
111 }
112 $uri = ArnParser::isArn($bucket) ? '' : '/';
113 $uri .= $bucket . '/' . $key;
114 $data['CopySource'] = $uri;
115 $data['PartNumber'] = $partNumber;
116 if (!empty($this->sourceVersionId)) {
117 $data['CopySource'] .= "?versionId=" . $this->sourceVersionId;
118 }
119 $defaultPartSize = $this->determinePartSize();
120 $startByte = $defaultPartSize * ($partNumber - 1);
121 $data['ContentLength'] = $partNumber < $partsCount ? $defaultPartSize : $this->getSourceSize() - $defaultPartSize * ($partsCount - 1);
122 $endByte = $startByte + $data['ContentLength'] - 1;
123 $data['CopySourceRange'] = "bytes={$startByte}-{$endByte}";
124 return $data;
125 }
126 protected function extractETag(ResultInterface $result)
127 {
128 return $result->search('CopyPartResult.ETag');
129 }
130 protected function getSourceMimeType()
131 {
132 return $this->getSourceMetadata()['ContentType'];
133 }
134 protected function getSourceSize()
135 {
136 return $this->getSourceMetadata()['ContentLength'];
137 }
138 private function getSourceMetadata()
139 {
140 if (empty($this->sourceMetadata)) {
141 $this->sourceMetadata = $this->fetchSourceMetadata();
142 }
143 return $this->sourceMetadata;
144 }
145 private function fetchSourceMetadata()
146 {
147 if ($this->config['source_metadata'] instanceof ResultInterface) {
148 return $this->config['source_metadata'];
149 }
150 //if the source variable was overloaded with an array, use the inputs for key and bucket
151 if (\is_array($this->source)) {
152 $headParams = ['Key' => $this->source['source_key'], 'Bucket' => $this->source['source_bucket']];
153 if (isset($this->source['source_version_id'])) {
154 $this->sourceVersionId = $this->source['source_version_id'];
155 $headParams['VersionId'] = $this->sourceVersionId;
156 }
157 //otherwise, use the default source parsing behavior
158 } else {
159 list($bucket, $key) = \explode('/', \ltrim($this->source, '/'), 2);
160 $headParams = ['Bucket' => $bucket, 'Key' => $key];
161 if (\strpos($key, '?')) {
162 list($key, $query) = \explode('?', $key, 2);
163 $headParams['Key'] = $key;
164 $query = Psr7\Query::parse($query, \false);
165 if (isset($query['versionId'])) {
166 $this->sourceVersionId = $query['versionId'];
167 $headParams['VersionId'] = $this->sourceVersionId;
168 }
169 }
170 }
171 return $this->client->headObject($headParams);
172 }
173 /**
174 * Get the url decoded input source, starting with a slash if it is not an
175 * ARN to standardize the source location syntax.
176 *
177 * @param string $inputSource The source that was passed to the constructor
178 * @return string The source, starting with a slash if it's not an arn
179 */
180 private function getInputSource($inputSource)
181 {
182 $sourceBuilder = ArnParser::isArn($inputSource) ? '' : '/';
183 $sourceBuilder .= \ltrim(\rawurldecode($inputSource), '/');
184 return $sourceBuilder;
185 }
186 }
187