PluginProbe
Media Cloud Sync / 1.2.4
Media Cloud Sync v1.2.4
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.4, at includes/sdk/s3/Aws/S3/MultipartCopy.php

188 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 $data['CopySource'] = '/' . $this->source['source_bucket'] . '/' . $key;
108 } else {
109 list($bucket, $key) = \explode('/', \ltrim($this->source, '/'), 2);
110 $data['CopySource'] = '/' . $bucket . '/' . \implode('/', \array_map('urlencode', \explode('/', \rawurldecode($key))));
111 }
112 $data['PartNumber'] = $partNumber;
113 if (!empty($this->sourceVersionId)) {
114 $data['CopySource'] .= "?versionId=" . $this->sourceVersionId;
115 }
116 $defaultPartSize = $this->determinePartSize();
117 $startByte = $defaultPartSize * ($partNumber - 1);
118 $data['ContentLength'] = $partNumber < $partsCount ? $defaultPartSize : $this->getSourceSize() - $defaultPartSize * ($partsCount - 1);
119 $endByte = $startByte + $data['ContentLength'] - 1;
120 $data['CopySourceRange'] = "bytes={$startByte}-{$endByte}";
121 return $data;
122 }
123 protected function extractETag(ResultInterface $result)
124 {
125 return $result->search('CopyPartResult.ETag');
126 }
127 protected function getSourceMimeType()
128 {
129 return $this->getSourceMetadata()['ContentType'];
130 }
131 protected function getSourceSize()
132 {
133 return $this->getSourceMetadata()['ContentLength'];
134 }
135 private function getSourceMetadata()
136 {
137 if (empty($this->sourceMetadata)) {
138 $this->sourceMetadata = $this->fetchSourceMetadata();
139 }
140 return $this->sourceMetadata;
141 }
142 private function fetchSourceMetadata()
143 {
144 if ($this->config['source_metadata'] instanceof ResultInterface) {
145 return $this->config['source_metadata'];
146 }
147 //if the source variable was overloaded with an array, use the inputs for key and bucket
148 if (\is_array($this->source)) {
149 $headParams = ['Key' => $this->source['source_key'], 'Bucket' => $this->source['source_bucket']];
150 if (isset($this->source['source_version_id'])) {
151 $this->sourceVersionId = $this->source['source_version_id'];
152 $headParams['VersionId'] = $this->sourceVersionId;
153 }
154 //otherwise, use the default source parsing behavior
155 } else {
156 list($bucket, $key) = \explode('/', \ltrim($this->source, '/'), 2);
157 $headParams = ['Bucket' => $bucket, 'Key' => $key];
158 if (\strpos($key, '?')) {
159 list($key, $query) = \explode('?', $key, 2);
160 $headParams['Key'] = $key;
161 $query = Psr7\Query::parse($query, \false);
162 if (isset($query['versionId'])) {
163 $this->sourceVersionId = $query['versionId'];
164 $headParams['VersionId'] = $this->sourceVersionId;
165 }
166 }
167 }
168 return $this->client->headObject($headParams);
169 }
170 /**
171 * Get the url decoded input source, starting with a slash if it is not an
172 * ARN to standardize the source location syntax.
173 *
174 * @param string $inputSource The source that was passed to the constructor
175 * @return string The source, starting with a slash if it's not an arn
176 */
177 private function getInputSource($inputSource)
178 {
179 if (ArnParser::isArn($inputSource)) {
180 $sourceBuilder = '';
181 } else {
182 $sourceBuilder = "/";
183 }
184 $sourceBuilder .= \ltrim(\rawurldecode($inputSource), '/');
185 return $sourceBuilder;
186 }
187 }
188