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 / MultipartUploader.php

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

140 lines 6.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\Aws\HashingStream;
6 use Dudlewebs\WPMCS\s3\Aws\Multipart\AbstractUploader;
7 use Dudlewebs\WPMCS\s3\Aws\PhpHash;
8 use Dudlewebs\WPMCS\s3\Aws\ResultInterface;
9 use Dudlewebs\WPMCS\s3\GuzzleHttp\Psr7;
10 use Dudlewebs\WPMCS\s3\Psr\Http\Message\StreamInterface as Stream;
11 use Dudlewebs\WPMCS\s3\Aws\S3\Exception\S3MultipartUploadException;
12 /**
13 * Encapsulates the execution of a multipart upload to S3 or Glacier.
14 */
15 class MultipartUploader extends AbstractUploader
16 {
17 use MultipartUploadingTrait;
18 const PART_MIN_SIZE = 5242880;
19 const PART_MAX_SIZE = 5368709120;
20 const PART_MAX_NUM = 10000;
21 /**
22 * Creates a multipart upload for an S3 object.
23 *
24 * The valid configuration options are as follows:
25 *
26 * - acl: (string) ACL to set on the object being upload. Objects are
27 * private by default.
28 * - before_complete: (callable) Callback to invoke before the
29 * `CompleteMultipartUpload` operation. The callback should have a
30 * function signature like `function (Aws\Command $command) {...}`.
31 * - before_initiate: (callable) Callback to invoke before the
32 * `CreateMultipartUpload` operation. The callback should have a function
33 * signature like `function (Aws\Command $command) {...}`.
34 * - before_upload: (callable) Callback to invoke before any `UploadPart`
35 * operations. The callback should have a function signature like
36 * `function (Aws\Command $command) {...}`.
37 * - bucket: (string, required) Name of the bucket to which the object is
38 * being uploaded, or an S3 access point ARN.
39 * - concurrency: (int, default=int(5)) Maximum number of concurrent
40 * `UploadPart` operations allowed during the multipart upload.
41 * - key: (string, required) Key to use for the object being uploaded.
42 * - params: (array) An array of key/value parameters that will be applied
43 * to each of the sub-commands run by the uploader as a base.
44 * Auto-calculated options will override these parameters. If you need
45 * more granularity over parameters to each sub-command, use the before_*
46 * options detailed above to update the commands directly.
47 * - part_size: (int, default=int(5242880)) Part size, in bytes, to use when
48 * doing a multipart upload. This must between 5 MB and 5 GB, inclusive.
49 * - prepare_data_source: (callable) Callback to invoke before starting the
50 * multipart upload workflow. The callback should have a function
51 * signature like `function () {...}`.
52 * - state: (Aws\Multipart\UploadState) An object that represents the state
53 * of the multipart upload and that is used to resume a previous upload.
54 * When this option is provided, the `bucket`, `key`, and `part_size`
55 * options are ignored.
56 * - track_upload: (boolean) Set true to track status in 1/8th increments
57 * for upload.
58 *
59 * @param S3ClientInterface $client Client used for the upload.
60 * @param mixed $source Source of the data to upload.
61 * @param array $config Configuration used to perform the upload.
62 */
63 public function __construct(S3ClientInterface $client, $source, array $config = [])
64 {
65 parent::__construct($client, $source, \array_change_key_case($config) + ['bucket' => null, 'key' => null, 'exception_class' => S3MultipartUploadException::class]);
66 if ($this->displayProgress) {
67 $this->getState()->setProgressThresholds($this->source->getSize());
68 }
69 }
70 protected function loadUploadWorkflowInfo()
71 {
72 return ['command' => ['initiate' => 'CreateMultipartUpload', 'upload' => 'UploadPart', 'complete' => 'CompleteMultipartUpload'], 'id' => ['bucket' => 'Bucket', 'key' => 'Key', 'upload_id' => 'UploadId'], 'part_num' => 'PartNumber'];
73 }
74 protected function createPart($seekable, $number)
75 {
76 // Initialize the array of part data that will be returned.
77 $data = [];
78 // Apply custom params to UploadPart data
79 $config = $this->getConfig();
80 $params = isset($config['params']) ? $config['params'] : [];
81 foreach ($params as $k => $v) {
82 $data[$k] = $v;
83 }
84 $data['PartNumber'] = $number;
85 // Read from the source to create the body stream.
86 if ($seekable) {
87 // Case 1: Source is seekable, use lazy stream to defer work.
88 $body = $this->limitPartStream(new Psr7\LazyOpenStream($this->source->getMetadata('uri'), 'r'));
89 } else {
90 // Case 2: Stream is not seekable; must store in temp stream.
91 $source = $this->limitPartStream($this->source);
92 $source = $this->decorateWithHashes($source, $data);
93 $body = Psr7\Utils::streamFor();
94 Psr7\Utils::copyToStream($source, $body);
95 }
96 $contentLength = $body->getSize();
97 // Do not create a part if the body size is zero.
98 if ($contentLength === 0) {
99 return \false;
100 }
101 $body->seek(0);
102 $data['Body'] = $body;
103 if (isset($config['add_content_md5']) && $config['add_content_md5'] === \true) {
104 $data['AddContentMD5'] = \true;
105 }
106 $data['ContentLength'] = $contentLength;
107 return $data;
108 }
109 protected function extractETag(ResultInterface $result)
110 {
111 return $result['ETag'];
112 }
113 protected function getSourceMimeType()
114 {
115 if ($uri = $this->source->getMetadata('uri')) {
116 return Psr7\MimeType::fromFilename($uri) ?: 'application/octet-stream';
117 }
118 }
119 protected function getSourceSize()
120 {
121 return $this->source->getSize();
122 }
123 /**
124 * Decorates a stream with a sha256 linear hashing stream.
125 *
126 * @param Stream $stream Stream to decorate.
127 * @param array $data Part data to augment with the hash result.
128 *
129 * @return Stream
130 */
131 private function decorateWithHashes(Stream $stream, array &$data)
132 {
133 // Decorate source with a hashing stream
134 $hash = new PhpHash('sha256');
135 return new HashingStream($stream, $hash, function ($result) use(&$data) {
136 $data['ContentSHA256'] = \bin2hex($result);
137 });
138 }
139 }
140